Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reflections doesn't find object subtypes

I am trying to get all the classes in a package by using Reflections. When I use the code of a concrete class (A in this example) it works and prints the subclasses information (B extends A so it prints B information), but when I use it with the Object class it doesn't work. How can I fix it?

This code works:

Reflections reflections = new Reflections(REFLECTION_PACKAGE);
Set<Class<? extends A>> allClasses = reflections.getSubTypesOf(A.class);

System.out.println("numberOfLCasses: " + allClasses.size());
System.out.println("classes: " + allClasses.toString());

This code doesn't:

Reflections reflections = new Reflections(REFLECTION_PACKAGE);
Set<Class<? extends Object>> allClasses = reflections.getSubTypesOf(Object.class);

System.out.println("numberOfLCasses: " + allClasses.size());
System.out.println("classes: " + allClasses.toString());
like image 533
David Marciel Avatar asked Feb 16 '26 12:02

David Marciel


1 Answers

This is documented behavior.

public SubTypesScanner()

created new SubTypesScanner. will exclude direct Object subtypes

public SubTypesScanner(boolean excludeObjectClass)

created new SubTypesScanner.

Parameters: excludeObjectClass - if false, include direct Object subtypes in results.

The below should return subtypes of Object.class

Reflections reflections = new Reflections(REFLECTION_PACKAGE,new SubTypesScanner(false));
Set<Class<? extends Object>> allClasses = reflections.getSubTypesOf(Object.class);

System.out.println("numberOfLCasses: " + allClasses.size());
System.out.println("classes: " + allClasses.toString());
like image 110
Adwait Kumar Avatar answered Feb 18 '26 02:02

Adwait Kumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!