ronmamo / reflections

Java runtime metadata analysis
Do What The F*ck You Want To Public License
4.71k stars 699 forks source link

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

Open anton-erofeev opened 2 years ago

anton-erofeev commented 2 years ago
 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

MAX-POLKOVNIK commented 2 years ago

Same problem

pgalbraith commented 2 years ago

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?

jaylawl commented 2 years ago

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 commented 2 years ago

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.

pgalbraith commented 2 years ago

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.

abelsromero commented 1 year ago

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 😢

damiencuvillier commented 1 year ago

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.

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

Any idea ?

nnimrod commented 1 year ago

@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(...)
        );
TreF555 commented 11 months ago

helped me, thanks spring boot + reflection 0.10.2

psam1017 commented 5 months ago

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

xdewx commented 4 months ago

@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