Skip to content

Commit

Permalink
relax junit4 type checks for parameterized types (TNG#132)
Browse files Browse the repository at this point in the history
* also write test and remove so solved `rawType` constraint
* also this allows somewhat strange subtypes of lists with e.g. multiple type args

Signed-off-by: Andreas Schmid <[email protected]>
  • Loading branch information
aaschmid committed Jan 31, 2021
1 parent 6d729df commit 1dbc797
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public DataConverter() {
* </ul>
*
* Please note, that {@link Iterable} can be replaced by any valid subtype (checked via {@link Class#isAssignableFrom(Class)}). As well
* as an arbitrary inner type is also accepted. Only rawtypes are not supported currently.
* as an arbitrary inner type is also accepted.
*
* @param type to be checked for convertibility (use either {@link Method#getGenericReturnType()}, {@link Method#getReturnType()}, or
* simple {@link Class} if possible)
Expand All @@ -70,11 +70,7 @@ public boolean canConvert(Type type) {

if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) type;

Type rawType = parameterizedType.getRawType();
if (Iterable.class.isAssignableFrom((Class<?>) rawType)) {
return canConvertIterableOf(parameterizedType);
}
return Iterable.class.isAssignableFrom((Class<?>) parameterizedType.getRawType());
}
return false;
}
Expand Down Expand Up @@ -103,18 +99,6 @@ public List<Object[]> convert(Object data, boolean isVarargs, Class<?>[] paramet
return super.convert(data, isVarargs, parameterTypes, context);
}

private boolean canConvertIterableOf(ParameterizedType parameterizedType) {
if (parameterizedType.getActualTypeArguments().length == 1) {
Type innerType = parameterizedType.getActualTypeArguments()[0];
if (parameterizedType.getActualTypeArguments()[0] instanceof ParameterizedType) {
ParameterizedType innerType2 = (ParameterizedType) innerType;
return Iterable.class.isAssignableFrom((Class<?>) innerType2.getRawType());
}
return true;
}
return false;
}

public void setObjectArrayConverter(ObjectArrayConverter objectArrayConverter) {
this.objectArrayConverter = objectArrayConverter;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,30 @@ public void testCanConvertShouldReturnTrueIfTypeIsListOfObject() {
assertThat(result).isTrue();
}

@Test
public void testCanConvertShouldReturnTrueIfTypeIsListOfNonIterableParameterizedType() {
// Given:
Type type = getMethod("methodReturningListOfNonIterableParameterizedType").getGenericReturnType();

// When:
boolean result = underTest.canConvert(type);

// Then:
assertThat(result).isTrue();
}

@Test
public void testCanConvertShouldReturnTrueIfTypeIsListOfRawType() {
// Given:
Type type = getMethod("methodReturningListOfRawType").getGenericReturnType();

// When:
boolean result = underTest.canConvert(type);

// Then:
assertThat(result).isTrue();
}

@Test
public void testCanConvertShouldReturnTrueIfTypeIsIterableOfWildcard() {
// Given:
Expand Down Expand Up @@ -169,7 +193,7 @@ public void testCanConvertShouldReturnFalseIfTypeIsTwoArgList() {
boolean result = underTest.canConvert(type);

// Then:
assertThat(result).isFalse();
assertThat(result).isTrue();
}

@Test
Expand Down Expand Up @@ -533,6 +557,15 @@ public static List<Object> methodReturningListOfObject() {
return null;
}

@SuppressWarnings("rawtypes")
public static List<Comparable> methodReturningListOfRawType() {
return null;
}

public static List<Comparable<Object>> methodReturningListOfNonIterableParameterizedType() {
return null;
}

public static List<?> methodReturningIterableOfWildcard() {
return null;
}
Expand Down

0 comments on commit 1dbc797

Please sign in to comment.