Open anton-erofeev opened 2 years ago
Same problem
Same here, my expectation is that new Reflections("java.util").getSubTypesOf(Object.class)
should not return an empty set, but it does. What am I missing?
Same here, tried System.out.println(new Reflections("java.util").getSubTypesOf(Object.class));
returns empty list.
Can't really get anything to work to be frank. im using 0.10.2
/edit:
Same with 0.10.1
Ive been at this for about 2 hours now and ive downgraded to 0.9.9.
Still no results, never - irregardless of input.
Even examples from https://www.baeldung.com/reflections-library do not work, everything is always an empty list
/edit:
Context that may cause the issue: I am running Reflections over a common / library jar, as opposed to using it in my target project directly. According to my stackoverflow research that may be the issue, as i've read of other doing just that.
I did finally have some success, the problem with doing basic testing looking for "Object" is that reflections filters "Object" out by default. As a basic starting point I had more success looking for "Scanner" in org.reflections, and that got me started. Some more basic examples on the main page would be nice to get people started, but I hesitate to be overly critical of anything where someone has so freely offered up what is clearly a huge investment of their time.
For anyone finding this, there's a workarround
Reflections reflections = new Reflections(new ConfigurationBuilder()
.setScanners(new SubTypesScanner(false),new ResourcesScanner())
.addUrls(ClasspathHelper.forJavaClassPath())
.filterInputsBy(new FilterBuilder()
.includePackage(packageName)));
var types = reflections.getSubTypesOf(Object.class);
However, this does not return inner classes 😢
I'm on a strange behavior.
This following code results on server environment with an empty list.
Reflections reflections = new Reflections("my.package");
Set<Class<?>> subTypes = reflections.get( SubTypes.of( Object.class).asClass());
I've also tried this one
reflections = new Reflections(new ConfigurationBuilder()
.setScanners(new SubTypesScanner(false), new ResourcesScanner())
.addUrls(ClasspathHelper.forJavaClassPath())
.filterInputsBy(new FilterBuilder()
.includePackage("my.package"))));
Depending on execution context:
Details:
My app is a Spring boot app with .jar packaging.
I've read Reflection lib is based on .class files.
Anyway, in my jar, I can also find this class files....
Any idea ?
@damiencuvillier , I have the exact same issue. Have you found a solution? What I did notice is that the scan is very fast when it doesn't give any results so it's like it's not even trying.
Update: Upgrading to 0.10.2 solved the issue. Just need to remember to change the default behavior of the scanner:
new Reflections(
new ConfigurationBuilder()
.addScanners(Scanners.SubTypes.filterResultsBy(s->true)/*Override the default behavior which exclude Object class*/)
.forPackages(...)
);
helped me, thanks spring boot + reflection 0.10.2
I tested that solutions of @damiencuvillier (on 0.9.x), @nnimrod (on 0.10.x) still work well on 24-04-05.
@damiencuvillier , I have the exact same issue. Have you found a solution? What I did notice is that the scan is very fast when it doesn't give any results so it's like it's not even trying.
Update: Upgrading to 0.10.2 solved the issue. Just need to remember to change the default behavior of the scanner:
new Reflections( new ConfigurationBuilder() .addScanners(Scanners.SubTypes.filterResultsBy(s->true)/*Override the default behavior which exclude Object class*/) .forPackages(...) );
solved my problem. but why the default filter doesn't work
this code returns an empty set, but i expect it to return a set of all classes version: 0.9.12