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,109
  • 2
  • 37
  • 52
Java Noob
  • 319
  • 1
  • 5
  • 14

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,232
  • 1
  • 11
  • 23
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
  • 967
  • 2
  • 13
  • 24