Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (768)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (854)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Listing classes from a package  (Read 643 times)
0 Members and 1 Guest are viewing this topic.
Offline orange451

JGO Kernel


Medals: 438
Projects: 7
Exp: 7 years


Your face? Your ass? What's the difference?


« Posted 2018-09-08 03:42:29 »

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:
1  
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?

First Recon. A java made online first person shooter!
Offline nsigma
« Reply #1 - Posted 2018-09-08 08:41:48 »

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

https://gist.github.com/pal/110024/8a845866d3aad6865a4d2cad2b3eff112b61b1d5

Praxis LIVE - hybrid visual IDE for (live) creative coding
Offline ClaasJG

JGO Coder


Medals: 43



« Reply #2 - Posted 2018-09-08 12:09:19 »

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 Tongue )

My english has to be tweaked. Please show me my mistakes.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline SHC
« Reply #3 - Posted 2018-09-08 13:49:10 »

This might be of some help to you.

https://github.com/sriharshachilakapati/SilenceEngine/blob/development/backend-lwjgl/src/main/java/com/shc/silenceengine/backend/lwjgl/LwjglResourceFilePath.java#L166-L227

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

Offline CoDi^R
« Reply #4 - Posted 2018-09-08 13:56:03 »

Here's the one I use:
https://github.com/code-disaster/libgdx-snippets/blob/master/src/main/java/com/badlogic/gdx/lang/ClassFinder.java

Example of use:
https://github.com/code-disaster/libgdx-snippets/blob/master/src/main/java/com/badlogic/gdx/json/AnnotatedJson.java#L105-L121

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

Robotality - steamworks4j - @code_disaster - codi^r @ #java-gaming
Offline nsigma
« Reply #5 - Posted 2018-09-09 07:51:34 »

I would write an Annotation Processor which gathers the classes. It could even write a class out which builds an instance of each.

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.

Praxis LIVE - hybrid visual IDE for (live) creative coding
Offline orange451

JGO Kernel


Medals: 438
Projects: 7
Exp: 7 years


Your face? Your ass? What's the difference?


« Reply #6 - Posted 2018-09-10 02:36:53 »

I would write an Annotation Processor which gathers the classes. It could even write a class out which builds an instance of each.

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:
https://github.com/ronmamo/reflections

Perhaps it's also a viable solution. I'll check it out soon Smiley


First Recon. A java made online first person shooter!
Offline ClaasJG

JGO Coder


Medals: 43



« Reply #7 - Posted 2018-09-10 11:54:55 »

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

My english has to be tweaked. Please show me my mistakes.
Offline orange451

JGO Kernel


Medals: 438
Projects: 7
Exp: 7 years


Your face? Your ass? What's the difference?


« Reply #8 - Posted 2018-09-10 12:06:43 »

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).

First Recon. A java made online first person shooter!
Offline ClaasJG

JGO Coder


Medals: 43



« Reply #9 - Posted 2018-09-10 12:41:15 »

Then I like the ServiceProvider idea.

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

-ClaasJG

My english has to be tweaked. Please show me my mistakes.
Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (1337 views)
2018-06-10 19:43:48

EgonOlsen (1214 views)
2018-06-10 19:43:44

EgonOlsen (939 views)
2018-06-10 19:43:20

DesertCoockie (1380 views)
2018-05-13 18:23:11

nelsongames (1112 views)
2018-04-24 18:15:36

nelsongames (1361 views)
2018-04-24 18:14:32

ivj94 (2095 views)
2018-03-24 14:47:39

ivj94 (1253 views)
2018-03-24 14:46:31

ivj94 (2259 views)
2018-03-24 14:43:53

Solater (806 views)
2018-03-17 05:04:08
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!