2

I want to be able to store an array of every class in my project. Is there a simple way to do it so it looks like this?

Class<?>[] classes = SomeClass.getClasses();

Thanks.

EDIT: Here's the code for the main class.

package game_engine.main;

public abstract class Main {
    public static void main(String[]args){
        //do something
    }
    protected abstract void start(GameSettings settings);
}
Saravana
  • 12,647
  • 2
  • 39
  • 57
Java Noob
  • 351
  • 1
  • 6
  • 15

2 Answers2

0

There is no simple way to do it, no. Doing this involves scanning the classpath, which is not doable in a portable way. Google's Guava has ClassPath, but it explicitly says to not use it for production-critial tasks.

diesieben07
  • 1,487
  • 1
  • 14
  • 25
0

I can't make it done in one-line code. But this may also help:

//scan urls that contain 'my.package', include inputs starting with 'my.package', use the default scanners
Reflections reflections = new Reflections("my.package");

//or using ConfigurationBuilder
new Reflections(new ConfigurationBuilder()
 .setUrls(ClasspathHelper.forPackage("my.project.prefix"))
 .setScanners(new SubTypesScanner(), 
              new TypeAnnotationsScanner().filterResultsBy(optionalFilter), ...),
 .filterInputsBy(new FilterBuilder().includePackage("my.project.prefix"))
 ...);

Detail:https://github.com/ronmamo/reflections

Dai Kaixian
  • 1,045
  • 2
  • 14
  • 24