4

I was trying to get all the classes from the "scala" package using the reflections library, but without success (the test is failing):

public static List<Class<?>> getClassesFromPackage(String packageName) {
    List<ClassLoader> classLoadersList = new LinkedList<ClassLoader>();
    classLoadersList.add(ClasspathHelper.contextClassLoader());
    classLoadersList.add(ClasspathHelper.staticClassLoader());
    classLoadersList.add(ClassLoader.getSystemClassLoader());

    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])))
            .filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix(packageName))));

    return reflections.getSubTypesOf(Object.class).stream().collect(Collectors.toList());
}

My test method is:

@Test
public void classFinderTest() {
    String packageName = "scala";

    List<Class<?>> listClasses = ClassFinder.getClassesFromPackage(packageName);

    assertFalse("the list is empty", listClasses.isEmpty());
}

The scala library dependency is declared in my pom.xml:

<dependency>
    <groupId>org.scala-lang</groupId>
    <artifactId>scala-library</artifactId>
    <version>2.11.6</version>
</dependency>

I need to do this using java code.

I want to programmatically get all the classes that are part of some specific package. For example, if the packageName is "scala", I want a list containing: scala.Int, scala.Double, scala.Enumeration, etc.

Can any one provide me some ideas to find the solution?

serv-inc
  • 35,772
  • 9
  • 166
  • 188
Pedro Hidalgo
  • 861
  • 10
  • 15
  • 1
    I'm not sure what exactly your looking for, check this http://stackoverflow.com/questions/15720822/how-to-get-names-of-classes-inside-a-jar-file – Krish Oct 20 '15 at 14:43
  • 1
    @user the Reflections and ClasspathHelper classes are from the reflections library: https://code.google.com/p/reflections/ as I specified in the question. – Pedro Hidalgo Oct 20 '15 at 14:48
  • 1
    @user2985467 I already see that page, but I don't know how to get access to the scala-library.jar because it is in my maven dependency, so I can't use the solution provide for the answer in that page. – Pedro Hidalgo Oct 20 '15 at 14:51

1 Answers1

4

A working Test class (Test.java) is

import java.util.Set;

import org.reflections.Reflections;
import org.reflections.scanners.SubTypesScanner ;

public class Test {
    public static Set<Class<?>> getClassesFromPackage(String packageName) {
        Reflections reflections = new Reflections(packageName, new SubTypesScanner(false));

        return reflections.getSubTypesOf(Object.class);
    }

    public static void main(String ... args) {
        for ( Class c: getClassesFromPackage("scala") ) {
            System.out.println(c.getName());
        }
    }
}

(with all necessary libs in the classpath).

Using Reflections(String prefix, Scanner... scanners) can simplify your code.

serv-inc
  • 35,772
  • 9
  • 166
  • 188