I was wondering if there was an easy way of determining the complete list of Types that a Java class extends or implements recursively?
for instance:
class Foo extends Bar implements I1, I2 {...}
class Bar implements I3 {...}
interface I1 extends I4, I5 {...}
interface I2 {...}
interface I3 {...}
interface I4 {...}
interface I5 {...}
class ClassUtil {
public static Set<Class<?>> getAllExtendedOrImplementedTypesRecursively(Class<?> clazz){
???
}
}
import static org.junit.Assert.*;
public class ClassUtilTest {
@Test
public void shouldEqualClasses(){
Set<Class<?>> types = ClassUtil.getAllExtendedOrImplementedTypesRecursively(Foo.class);
Set<Class<?>> checklist = new HashSet<>();
checklist.add(Foo.class);
checklist.add(Bar.class);
checklist.add(I1.class);
checklist.add(I2.class);
checklist.add(I3.class);
checklist.add(I4.class);
checklist.add(I5.class);
assertTrue(checklist.containsAll(types));
assertTrue(types.containsAll(checklist));
}
}
Think Arquillian ShrinkWrap creation helper.
UPDATE: due to the Class object not implementing Comparable> I also need to find a way of creating a Set (or similar class) without implementing the Comparable interface (for instance, solely relying on the hashcode of the class object).
UPDATE: changed the test to use hashset. derp.