Closed GoogleCodeExporter closed 8 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
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
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
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
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
@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
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
Original issue reported on code.google.com by
ftar...@gmail.com
on 6 Jul 2012 at 10:12