choigv / reflections

Automatically exported from code.google.com/p/reflections
Do What The F*ck You Want To Public License
0 stars 0 forks source link

getSUbTypes Returns empty list #122

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
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)

Original issue reported on code.google.com by ftar...@gmail.com on 6 Jul 2012 at 10:12

GoogleCodeExporter commented 9 years ago
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))));

Original comment by ronm...@gmail.com on 6 Jul 2012 at 12:05

GoogleCodeExporter commented 9 years ago
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

Original comment by ftar...@gmail.com on 9 Jul 2012 at 4:04

GoogleCodeExporter commented 9 years ago
Hi, 
 I have found a solution, this code success in acquire all the existing objects..:

 List<ClassLoader> classLoadersList = new LinkedList<ClassLoader>();
            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

Original comment by ftar...@gmail.com on 10 Jul 2012 at 12:58

GoogleCodeExporter commented 9 years ago
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?

Original comment by finals...@gmail.com on 19 Sep 2012 at 4:15

GoogleCodeExporter commented 9 years ago
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"))));

Original comment by MasudCs...@gmail.com on 13 Mar 2013 at 12:18

GoogleCodeExporter commented 9 years ago
@MasudCseKu: what about just

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

Original comment by ronm...@gmail.com on 16 Mar 2013 at 12:46

GoogleCodeExporter commented 9 years ago
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.

Original comment by ronm...@gmail.com on 22 Nov 2013 at 10:24