I am trying to do Java Reflection using Class c = Class.forName(className)
I want to pass in the className
without specifying the package name as the String className
will contain classes from multiple sub-packages say com.A.*
,com.B.*
.... com.Z.*
Is it possible?
It will not be feasible to do a case switch to pre-pend the different sub-packages if I have many sub-packages in this case.
One simple option would be to have a list of candidate packages:
This won't be the nicest code in the world, admittedly...
You may be able to make it faster by looking for alternative calls (e.g.
ClassLoader.getResource
) which don't throw exceptions if the class isn't found.There's certainly nothing I'm aware of to allow you to find a class without specifying a name at all.
First, get all classes with the Reflections library
Next, build up a lookup-map.
When you need to lookup a class you'll do:
From the javadoc of java.lang.Class its not possible