reflections icon indicating copy to clipboard operation
reflections copied to clipboard

reflections.getSubTypesOf(Object.class) returns an empty list

Open anton-erofeev opened this issue 3 years ago • 11 comments

 Reflections reflections = new Reflections(packageName);
 return new HashSet<>(reflections.getSubTypesOf(Object.class));

this code returns an empty set, but i expect it to return a set of all classes version: 0.9.12

anton-erofeev avatar Feb 04 '22 14:02 anton-erofeev

Same problem

MAX-POLKOVNIK avatar Feb 22 '22 05:02 MAX-POLKOVNIK

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?

pgalbraith avatar Mar 21 '22 21:03 pgalbraith

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

jaylawl avatar Mar 25 '22 11:03 jaylawl

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.

jaylawl avatar Mar 25 '22 11:03 jaylawl

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.

pgalbraith avatar Mar 26 '22 15:03 pgalbraith

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 😢

abelsromero avatar Dec 17 '22 20:12 abelsromero

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:

  1. When executing inside my IDE (intelliJ), it works well, as I expect.
  2. If I execute via maven command-line, it works also
  3. But when I execute the application in standalone mode (from packaged .jar file execution), subtypes list is empty.

Details:

My app is a Spring boot app with .jar packaging.

  • If I try to scan an external library package, it works in third cases.
  • If I scan an internal package (in my own code), no sub class is found. As if my code would not be in loaded classpath.

I've read Reflection lib is based on .class files.
Anyway, in my jar, I can also find this class files....

Any idea ?

damiencuvillier avatar Jun 23 '23 14:06 damiencuvillier

@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(...)
        );

nnimrod avatar Jul 12 '23 09:07 nnimrod

helped me, thanks spring boot + reflection 0.10.2

TreF555 avatar Sep 20 '23 19:09 TreF555

I tested that solutions of @damiencuvillier (on 0.9.x), @nnimrod (on 0.10.x) still work well on 24-04-05.

psam1017 avatar Apr 05 '24 06:04 psam1017

@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

image

xdewx avatar May 08 '24 02:05 xdewx