I know how to using Reflection determine the super class of a class. So if I had a game, and I had a map editor, and I wanted it to have a panel with a button to select select any type of Solid that can be added to the game and add it, I could theoretically use reflection to look through all the classes in my game and see which ones have the super class "Solid", and then add a button to the panel. The only thing that prevents me from doing this is that I need a list of all classes to search through, is there a way I can get that?
Asked
Active
Viewed 3,276 times
1
-
The idea is that you can write plugins that extend `Solid` and add them to the program? – chrylis -cautiouslyoptimistic- Sep 02 '13 at 17:24
-
Take a look [here](http://stackoverflow.com/questions/205573/at-runtime-find-all-classes-in-a-java-application-that-extend-a-base-class). – Pshemo Sep 02 '13 at 17:28
-
@chrylis You know when you use the code "Main.class", and it returns a Class
? I want a Class – csga5000 Sep 02 '13 at 17:52[] that contains all classes I defined in my code.
1 Answers
2
You probably want the Java Service Provider Interface. It gets around this problem by having you list the implementing "plug-in" (provider) classes in the jar manifest. You then use ServiceLoader
to ask for all of the available implementations, and you only have to use Class.forName
. Here's another useful overview.

chrylis -cautiouslyoptimistic-
- 75,269
- 21
- 115
- 152
-
Thanks, I'm looking into this. If I get it to work, I'll mark as answer. Some quick example code would be really nice, and I'd give a 1up. – csga5000 Sep 02 '13 at 17:49
-
Just look at the docs on `ServiceLoader`. For all of the classes implementing your interface, you'll just want `ServiceLoader.load(MyInterface.class).iterator()`. – chrylis -cautiouslyoptimistic- Sep 03 '13 at 04:32