Get all the classes of a plugin

Is there a way to get the classes of a plugin when Sponge load the plugin?

You cannot deterministically obtain a list of classes with Java because Java does not have reified containers. You can only obtain a rough idea of classes by either crawling a container (a jar) or using reflection to walk around the class tree. Both of these approaches have major drawbacks.

Why on earth would you want to do this anyway? It seems like you may have an X/Y problem here.

Jar files are just zip files which contains compiled classes, you can simply loop thru all class files if you know name of the jar file.

Or you can use guava’s ClassPath if you know package name

Whats wrong with it?

X/Y issue. It seems like a weird thing to want to do. It’s extremely unperformant and unreliable to walk through a jar and plugins shouldn’t be doing this. Without the reasoning the question cannot be answered in an appropriate way.

Why is it inapropriate? Many java frameworks works on exactly same approach eg jee EJB “autoscan” or whatever you call it, Hibernate & JPA entities, Spring.

It seems like a weird thing to want to do.

eg what about some automatic initialization of commands/listeners/… in specific package.
reasonable enough

I am aware, and I didn’t say “inappropriate”, I said “weird”. It’s a weird thing for a plugin to want to inspect other plugins and my suspicion is that if someone wants to do this then they likely have an X which is actually already supported through a better mechanism, but since X isn’t defined the question needs to be asked.

Forge already does all the scanning to load classes, so dependencies, API, etc (eg. plausible reasons to want to do this) are already provisioned for, plus there’s DI of course.

The way the question was phrased implies that there is something missing from the scenario. Plugins shouldn’t need to “scan” other plugins.

1 Like

Stop guessing, it was reasonable to ask what the use-case is, in order to suggest a better approach if one exists. Stop defending a dumb assumption with dumb reasons.

I’m currently working on a plugin that supports multiples scripts languages. I don’t want to recreate each methods on my scripts to interact with the Sponge API. I’m using relflection for this. I wanted to know if it was possible to get the classes loaded by Sponge to handle the static classes.

Example
Plugin A have a static class named Test and plugin B also have a class named Test. To access the Plugin A or B class named Test, I would like to do this in the script(JavaScript in this case)

plugin-A-ID.Test.functionToCall()
plugin-B-ID.Test.functionToCall()

I would like to do something like that, but if I can’t with Sponge, I wll try something else.

Try taking a look at: http://stackoverflow.com/a/15720973 and see if it helps you get any closer.

It is one of my options to iterate the classes inside a jar, but I don’t think it is the best way to do it.

Depends on how often you intend on doing it. Asynchronous operations could allow you to make it a lazy operation to be worked on in the background till it is done.

Only one time when the server start.

The larger the trees that must be walked the longer it will take. If it’s necessary to complete before the server is considered “ready” then there’s not much of a way around the wait time- bite it if you must. You could however have people specify classes rather than scan them, this would speed things up significantly.

I will add a function to retrieve a static class. So, the user could get the classes he wants.