Skip to content

Commit

Permalink
#113: extract ParamValue type when injection target itself is ParamValue
Browse files Browse the repository at this point in the history
Fix potential NPE on firstValue
  • Loading branch information
Bauke Scholtz committed Jun 9, 2016
1 parent dd38cb3 commit 58dc46e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
6 changes: 2 additions & 4 deletions src/main/java/org/omnifaces/cdi/param/ParamValue.java
Expand Up @@ -40,7 +40,6 @@ public class ParamValue<V> implements Serializable {
private final Type type;

private transient List<V> values;
private transient V firstValue;
private transient boolean valuesSet;

private boolean valuesAreSerializable;
Expand All @@ -52,15 +51,14 @@ public ParamValue(String[] submittedValues, Param param, Type type, List<V> valu
this.type = type;
setValues(values);

if (firstValue == null || firstValue instanceof Serializable) {
if (isEmpty(values) || (values.get(0) instanceof Serializable)) {
valuesAreSerializable = true;
serializableValues = values;
}
}

private void setValues(List<V> values) {
this.values = values;
firstValue = isEmpty(values) ? null : values.get(0);
valuesSet = true;
}

Expand Down Expand Up @@ -89,7 +87,7 @@ public V getValue() {
}
}

return (V) RequestParameterProducer.coerceMultipleValues(values, firstValue, type);
return (V) RequestParameterProducer.coerceMultipleValues(values, type);
}

public String getSubmittedValue() {
Expand Down
Expand Up @@ -83,6 +83,10 @@ public <V> ParamValue<V> produce(InjectionPoint injectionPoint) {
String label = getLabel(param, injectionPoint);
Type type = injectionPoint.getType();

if (type instanceof ParameterizedType && ParamValue.class.isAssignableFrom((Class<?>) ((ParameterizedType) type).getRawType())) {
type = ((ParameterizedType) type).getActualTypeArguments()[0];
}

FacesContext context = FacesContext.getCurrentInstance();
String[] submittedValues = getRequestParameterValues(context, name);
List<V> convertedValues = getConvertedValues(context, param, label, submittedValues, type);
Expand Down Expand Up @@ -136,12 +140,12 @@ static <V> List<V> getConvertedValues(FacesContext context, Param param, String
return convertedValues;
}

static Object coerceMultipleValues(List<?> values, Object first, Type type) {
static Object coerceMultipleValues(List<?> values, Type type) {
if (values == null) {
return null;
}
else if (type instanceof ParameterizedType) {
return coerceMultipleValues(values, first, ((ParameterizedType) type).getRawType());
return coerceMultipleValues(values, ((ParameterizedType) type).getRawType());
}
if (type instanceof Class) {
Class<?> cls = (Class<?>) type;
Expand All @@ -154,7 +158,7 @@ else if (List.class.isAssignableFrom(cls)) {
}
}

return first;
return values.isEmpty() ? null : values.get(0);
}

private static <V> boolean validateValues(FacesContext context, Param param, String label, String[] submittedValues, List<V> convertedValues, InjectionPoint injectionPoint) {
Expand Down Expand Up @@ -362,7 +366,7 @@ private static <V> Set<ConstraintViolation<?>> doBeanValidation(List<V> values,
// Check if the target property in which we are injecting in our special holder/wrapper type
// ParamValue or not. If it's the latter, pre-wrap our value (otherwise types for bean validation
// would not match)
Object valueToValidate = coerceMultipleValues(values, values.get(0), type);
Object valueToValidate = coerceMultipleValues(values, type);

if (type instanceof ParameterizedType) {
Type propertyRawType = ((ParameterizedType) type).getRawType();
Expand Down

0 comments on commit 58dc46e

Please sign in to comment.