Skip to content

Commit

Permalink
fix(factory): Handles null parameter in Type#createReference(Class) a…
Browse files Browse the repository at this point in the history
…nd isSubTypeOf. (Closes #825)
  • Loading branch information
GerardPaligot authored and monperrus committed Sep 21, 2016
1 parent e8c1073 commit 7214144
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/main/java/spoon/reflect/factory/TypeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,9 @@ public <T> CtArrayTypeReference<T> createArrayReference(String qualifiedName) {
* Creates a reference to a simple type
*/
public <T> CtTypeReference<T> createReference(Class<T> type) {
if (type == null) {
return null;
}
if (type.isArray()) {
CtArrayTypeReference<T> array = factory.Core().createArrayTypeReference();
array.setComponentType(createReference(type.getComponentType()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,7 @@ public boolean isSubtypeOf(CtTypeReference<?> type) {
}
}
CtTypeReference<?> superType = getFactory().Type().createReference(actualSubType.getSuperclass());
if (superType.equals(type)) {
return true;
} else {
return superType.isSubtypeOf(type);
}
return superType != null && (superType.equals(type) || superType.isSubtypeOf(type));
} catch (Exception e) {
Launcher.LOGGER.error("cannot determine runtime types for '" + this + "' and '" + type + "'", e);
return false;
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/spoon/test/reference/TypeReferenceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -546,4 +546,15 @@ public void testImproveAPIActualTypeReference() throws Exception {

assertEquals(1, typeReference.getActualTypeArguments().size());
}

@Test
public void testIsSubTypeSuperClassNull() throws Exception {
Factory factory = createFactory();

factory.Class().create("Tacos");
CtTypeReference<?> subRef = factory.Type().createReference(AutoCloseable.class);
CtTypeReference<?> superRef = factory.Type().createReference("Tacos");

assertFalse(subRef.isSubtypeOf(superRef));
}
}

0 comments on commit 7214144

Please sign in to comment.