Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get all classes from package #13

Closed
rodrigorodriguescosta opened this issue Feb 12, 2014 · 6 comments
Closed

Get all classes from package #13

rodrigorodriguescosta opened this issue Feb 12, 2014 · 6 comments

Comments

@rodrigorodriguescosta
Copy link

I have seen the example like
Set<Class<? extends Module>> modules = reflections.getSubTypesOf(com.google.inject.Module.class);

But, I'd like to get all classes from package wihout especified the class, something like

List clazz = reflections.getClassFromPackage("com.someproject.model");

is it possible? how can I do that with Reflection library?

@ronmamo
Copy link
Owner

ronmamo commented Feb 12, 2014

What would be the next step after you get these classes? How would you act on them?

@rodrigorodriguescosta
Copy link
Author

I need them (List of class) to call Class.forName("fullnameOfTheClass") cause there are many classes in many package and subpackage and I wouldn't manually write them.

@ronmamo
Copy link
Owner

ronmamo commented Feb 12, 2014

So you basically want to print them? That's not the right usage for Reflections, although you can do it - See http://stackoverflow.com/questions/520328/can-you-find-all-classes-in-a-package-using-reflection

The right usage would be, for example, to get all sub types of some interface - and than you can act on these using the common interface. That would be programmatically meaningful. See the difference?

@rodrigorodriguescosta
Copy link
Author

OK, I have fix it like this

//lista as classes do pacote "com.google", incluindo os subpacotes
Reflections r = new Reflections(
"com.google",
new SubTypesScanner(false),
ClasspathHelper.forClassLoader()
);
Set<Class<?>> classes = r.getSubTypesOf(Object.class);

This is exactly what I was looking for!

@allComputableThings
Copy link

@ronmamo I don't agree that acting on all class in a package is an invalid use case.
I have a package and I want to register all class in this package with a serialization framework. The classes have no common bases except Object and require no special annotations.

@ronmamo
Copy link
Owner

ronmamo commented Apr 27, 2016

Good point. I agree.
It is a legitimate metadata.
On Apr 27, 2016 11:21 PM, "Stuart Reynolds" notifications@github.com
wrote:

@ronmamo https://github.com/ronmamo I don't agree that acting on all
class in a package is an invalid use case.
I have a package and I want to register all class in this package with
a serialization framework. The classes have no common bases except Object
and require no special annotations.


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
#13 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants