Skip to content

Commit

Permalink
Fixed potential NullPointerExceptions for TypeDiscoverer.getComponent…
Browse files Browse the repository at this point in the history
…Type() and getMapValueType().
  • Loading branch information
odrotbohm committed Aug 19, 2011
1 parent c71f268 commit 307993a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,12 @@ public TypeInformation<?> getMapValueType() {
return createInfo(parameterizedType.getActualTypeArguments()[1]);
}

return createInfo(GenericTypeResolver.resolveTypeArguments(getType(), Map.class)[1]);
return getTypeArgument(getType(), Map.class, 1);
}

private TypeInformation<?> getTypeArgument(Class<?> type, Class<?> bound, int index) {
Class<?>[] arguments = GenericTypeResolver.resolveTypeArguments(type, bound);
return arguments == null ? null : createInfo(arguments[index]);
}

/* (non-Javadoc)
Expand Down Expand Up @@ -284,11 +289,11 @@ public TypeInformation<?> getComponentType() {
Class<S> rawType = getType();

if (isMap()) {
return createInfo(GenericTypeResolver.resolveTypeArguments(rawType, Map.class)[0]);
return getTypeArgument(rawType, Map.class, 0);
}

if (Iterable.class.isAssignableFrom(rawType)) {
return createInfo(GenericTypeResolver.resolveTypeArguments(rawType, Iterable.class)[0]);
return getTypeArgument(rawType, Iterable.class, 0);
}

if (rawType.isArray()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ public void discoveresConstructorParameterTypesCorrectly() throws NoSuchMethodEx
assertThat(types.get(0).getComponentType().getType(), is(equalTo((Class) String.class)));
}

@Test
@SuppressWarnings("rawtypes")
public void returnsNullForComponentAndValueTypesForRawMaps() {
TypeDiscoverer<Map> discoverer = new TypeDiscoverer<Map>(Map.class, null);
assertThat(discoverer.getComponentType(), is(nullValue()));
assertThat(discoverer.getMapValueType(), is(nullValue()));
}

class SelfReferencing {

Map<String, SelfReferencingMap> parent;
Expand Down

0 comments on commit 307993a

Please sign in to comment.