Export to GitHub

reflections - issue #122

getSUbTypes Returns empty list


Posted on Jul 6, 2012 by Quick Dog

What steps will reproduce the problem? I have Netbeans 7.0.1 added all the needed libraries from the uber package to Project libraries.

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

What is the expected output? What do you see instead? the list of classes in the package

What version of the product are you using? On what operating system? 0.9.8 windows7x64 java 1.6.0_23 Please provide any additional information below. NetBeans 7.0.1 copied the needed libraries out of the uber package and added to my project. It compiles right. I obtain the same result by useing getResources (with regexp) or by trying different packages (java.util too)

Comment #1

Posted on Jul 6, 2012 by Swift Rhino

the reason is that SubTypesScanner defaults is to exclude Object class. try instead

    new Reflections(new ConfigurationBuilder()
            .setScanners(new SubTypesScanner(false /* don't exclude Object.class */), new ResourcesScanner())
            .setUrls(ClasspathHelper.forPackage(basePackageName))
            /* and maybe */
            .filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix(basePackageName))));

Comment #2

Posted on Jul 9, 2012 by Quick Dog

Thanks for feedback, my application uses existing libraries and I like to use reflection in order to find out at runtime the full class names. By copying your code and removing the last filter I have noted that only the classes which are part of my own application are listed. The library's object types are not listed, I am lacking something, perhaps is the Reflections taking the wrong classpath. Have you any suggestion?

regards

Comment #3

Posted on Jul 10, 2012 by Quick Dog

Hi, I have found a solution, this code success in acquire all the existing objects..:

List classLoadersList = new LinkedList(); classLoadersList.add(ClasspathHelper.contextClassLoader()); classLoadersList.add(ClasspathHelper.staticClassLoader());

        Reflections reflections = new Reflections(new ConfigurationBuilder()
            .setScanners(new SubTypesScanner(false /* don't exclude Object.class */), new ResourcesScanner())
            .setUrls(ClasspathHelper.forClassLoader(classLoadersList.toArray(new ClassLoader[0])))); /*forPackage(basePackageName))); */
            /* and maybe */
            /*.filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix(basePackageName))));*/

        Set<Class<? extends Object>> classSet = reflections.getSubTypesOf(Object.class);

I suppose the problem is related with .forPackage(basePackageName) ... both used in this way and with ClassLoaders: .forPackage(basePackageName, classLoadersList[]); In my old implementation .forPackage method always returns a list containing only my own classes (which are located in a package different from basePackage)

About the value of basePackageName, considering a package it.libraries. with subpackages it.libraries.sub1 it.libraries.sub2 , I have assigned to basePackageName the value "it.libraries".

Perhaps the problem is related to .forPackage and the ClassLoader mess-tree?? I am quite interesting in understand how does the .forPackage works exactly. Perhaps it is only a local issue of mine but it may be a flaw. Thanks in advance Regards

Comment #4

Posted on Sep 19, 2012 by Swift Kangaroo

I'm having a similar problem, I think, so I wanted to post here to collect it in the same place, with Comment 3's issue.

I'm trying to use Reflections to get all classes defined within a package:

Reflections ref = new Reflections(new ConfigurationBuilder() .setScanners(new SubTypesScanner(false /* don't exclude Object.class */), new ResourcesScanner()) .setUrls(ClasspathHelper.forPackage("org.somepackage")) .filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix("org.somepackage"))));

However, this only finds classes that are either subclasses of Object, or subclasses of other types within org.somepackage. It won't find classes that are defined with org.somepackage, but inherit from types in org.someotherpackage. Is this a config problem on my end?

Comment #5

Posted on Mar 13, 2013 by Swift Elephant

The following code working fine for me for the external APIs. But when I am trying to get the classes of a built-in package like "java.util", nothing comes up. What to do? Any idea?

Reflections reflections = new Reflections(new ConfigurationBuilder() .setScanners(new SubTypesScanner(false /* don't exclude Object.class */), new ResourcesScanner()) .setUrls(ClasspathHelper.forClassLoader(classLoadersList.toArray(new ClassLoader[2]))) .filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix("org.reflections"))));

Comment #6

Posted on Mar 16, 2013 by Swift Rhino

@MasudCseKu: what about just

new Reflections("your.package", new SubTypesScanner(false)) or new Reflections("your.package", new SubTypesScanner(false), ClasspathHelper.forClassLoader())

Comment #7

Posted on Nov 22, 2013 by Swift Rhino

getting all classes of a package is not basically the idea of using Reflections.

though, you'd need to scan all urls that apriori might be relevant (ClasspathHelper.forClassLoader()), and filter all keys of SubTypesScanner:

    for (String s : reflections.getStore().get(SubTypesScanner.class).keySet()) {
        if (s.startsWith("java.util")) {
        }
    }

will give you all classes that have relevant subtypes and that are in java.util.

Status: WontFix

Labels:
Type-Defect Priority-Medium