Take all classes of a given Package that is in the classpath

Asked

Viewed 1,821 times

2

I’m needing to take all the classes of a certain package, I’ve seen some codes that do this, but it only takes the classes that are part of the project itself, basically doesn’t use Reflection and does a search for files .class in the directory, in my case, I need to pick up the .class of a jar that is part of my project.

Something like:

List<Class> clazz = ReflectionUtil.getClassFromPackage("org.springframework.util");

List<Class> clazz = ReflectionUtil.getClassFromPackage("com.meuprojeto.meupacotebase");

I’ve already tried:

http://mike.shannonandmike.net/2009/09/02/java-reflecting-to-get-all-classes-in-a-package/

https://stackoverflow.com/questions/15519626/how-to-get-all-classes-names-in-a-package

https://stackoverflow.com/questions/520328/can-you-find-all-classes-in-a-package-using-reflection

https://stackoverflow.com/questions/520328/can-you-find-all-classes-in-a-package-using-reflection

They spoke of that library http://github.com/ronmamo/reflections

but it has the following code:

 Reflections reflections = new Reflections("my.project");

 Set<Class<? extends SomeType>> subTypes = reflections.getSubTypesOf(SomeType.class);

Now, I need to get the list of classes, I don’t want to specify the type of class I need

I think this would be or should be basic with the Reflection java.

1 answer

4


You can use the library Reflections in an unusual way, but it is possible:

//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);

//exibe a lista classes
for (Class<?> c : classes) {
    System.out.println(c.getName());
}

The parameter new SubTypesScanner(false) permite a listagem de classes através deObject.class`, that is, classes that have no explicit inheritance. Otherwise these classes would be ignored.

The parameter ClasspathHelper.forClassLoader() lists all classes of Class Loader current, otherwise only the classes of the current project/jar would be listed.

Just be careful if some class that can’t be loaded (maybe for lack of a dependency), otherwise you end up with an exception like this:

Exception in thread "main" org.reflections.ReflectionsException: could not get type for name org.dom4j.xpath.DefaultNamespaceContext
    at org.reflections.ReflectionUtils.forName(ReflectionUtils.java:378)
    at org.reflections.ReflectionUtils.forNames(ReflectionUtils.java:387)
    at org.reflections.Reflections.getSubTypesOf(Reflections.java:338)
    at snippet.ListClasses.main(ListClasses.java:15)

Another option would be to do everything manually, that is, you can look at the classpath in the environment variable and go through all directories looking for files .class and jars that contain the classes you need.

Honestly, it’s not worth it. If you don’t want to use a library to make the job easier, add some restrictions to the project to make it easier to list classes.

  • Man, very good, this is exactly what I wanted, it worked out, I had not gotten by this detail that you said new Subtypesscanner(false), beautiful explanation, again, thank you so much for the help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.