Listing classes from a package

Hiah,

In my engine I am trying to get a list of classes within a package. I need this list because I need to create an instance of each class within the package to initialize it for later use.

For example, the list of instanceable classes is:

I need a way to get all of these classes by referencing the package name. I found this online:
https://pastebin.com/X5RYw32f

Usage:

Class<?>[] claz = ClassFinder.find("luaengine.type.object.insts");

This works perfectly, but only when running from eclipse. When I run from a jar, it cannot find any of the classes.

Not sure why a jar file yields different results? Could it be because it’s converting a URL to a File, then listing it’s sub-files?

Yes, you definitely can’t use File with the contents of a JAR unless you extract it, but quick Google found this (not tested! )

I would write an Annotation Processor which gathers the classes.
It could even write a class out which builds an instance of each. This way you don’t depend on any nasty reflection and have compile time validation.

-ClaasJG

(p.s. if you have any mature build system this integrates seamless :stuck_out_tongue: )

This might be of some help to you.

This uses a custom Promise implementation because of Async nature, but the base is same, you can use it.

Here’s the one I use:

Example of use:

It’s a rather slow operation, so I usually do this only once and cache the result.

Yes, this. If it’s your code don’t do the class listing thing. It’s horrible! I hadn’t quite twigged that when I replied.

If you only have that few classes you could even manage a registry class manually, but the use of an annotation processor to do this is a good idea.

The other standard way you could do this is via ServiceLoader (assuming they all subclass or implement the same type). This is great for allowing such types to be plugged in from other JARs too. There are pre-existing annotation processing libraries that can build the META-INF list automatically.

I’ve been digging around your guys responses. Interesting stuff! Like you suggested, I don’t think listing each class is the best way to go about this. It’s quite slow, and requires a lot of error checking when using against a lot of libraries (LWJGL, JBullet, ect). It actually throws some odd errors that refuse to be caught by a generic try catch Exception. I had to manually write each exception type specifically. In the end it just led to a JVM crash from LWJGL somewhere. I’m not going to report it as a LWJGL bug, it’s clearly not. This is just too messy!

All of those classes do extend a common super-type; an abstract class. I’ll see if I can implement something with the ServiceLoader.

I’ve also seen this library:

Perhaps it’s also a viable solution. I’ll check it out soon :slight_smile:

May I ask why you need an instance of all those classes?
From the package name I guess for some lua binding. But why?

-ClaasJG

In lua I want to be able to create instances of these classes.

For example the Vector3 class would be instanced with:
Vector3.new(16,8,4)

Lua has no concept of what “Vector3” is until I write some code in java defining it. Since I have a lot of different object types that all share the same defining process, I thought it’d be easier to create a system where the classes are automatically created once at the start of the application (the defining process is in the super type constructor they all share).

Then I like the ServiceProvider idea.

Which should be easy to implement and even easier using something like AutoService as build only dependency.

-ClaasJG