Skip to content

Commit

Permalink
ResolvedType.getAllTypes() returns parent, this, and interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysiekbielicki committed Oct 15, 2020
1 parent 473e70b commit 16ec167
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/main/java/com/fasterxml/classmate/ResolvedType.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,26 @@ public final boolean canCreateSubtype(Class<?> subtype) {
*/
public abstract List<ResolvedType> getImplementedInterfaces();

/**
* Returns Ordered list of parent, current and interfaces of this type.
*
* @return List of parent, current and interfaces of this type containing at least current type
*/
public List<ResolvedType> getAllTypes() {
List<ResolvedType> allTypes = new ArrayList<ResolvedType>();
ResolvedType parentClass = getParentClass();
if (parentClass != null) {
allTypes.add(parentClass);
}
allTypes.add(this);
List<ResolvedType> implementedInterfaces = getImplementedInterfaces();
if (implementedInterfaces != null) {
allTypes.addAll(implementedInterfaces);
}

return allTypes;
};

/**
* Returns list of generic type declarations for this type, in order they
* are declared in class description.
Expand Down
23 changes: 22 additions & 1 deletion src/test/java/com/fasterxml/classmate/ResolvedTypeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ private static class Foo16 extends Bar16 { }
private static class Bar16 extends Zen16<Bar16, Foo16> { }

private static class Zen16<A, B extends A> { }


private static class ClassWithInterfaces implements FirstInterface, SecondInterface { }

interface FirstInterface {}

interface SecondInterface {}

@Test
public void testCanCreateSubtype() {
ResolvedObjectType stringType = ResolvedObjectType.create(String.class, null, null, null);
Expand Down Expand Up @@ -140,4 +146,19 @@ public void testIssue16()
assertEquals(Bar16.class, params.get(0).getErasedType());
assertEquals(Foo16.class, params.get(1).getErasedType());
}

@Test
public void testFindAllClasses() {
TypeResolver resolver = new TypeResolver();
ResolvedType type = resolver.resolve(ClassWithInterfaces.class);

List<ResolvedType> allTypes = type.getAllTypes();

assertNotNull(allTypes);
assertEquals(4, allTypes.size());
assertEquals(Object.class, allTypes.get(0).getErasedType());
assertEquals(ClassWithInterfaces.class, allTypes.get(1).getErasedType());
assertEquals(FirstInterface.class, allTypes.get(2).getErasedType());
assertEquals(SecondInterface.class, allTypes.get(3).getErasedType());
}
}

0 comments on commit 16ec167

Please sign in to comment.