I am trying to do Java Reflection using Class c = Class.forName(className)
I want to pass in the className
without specifying the package n
First, get all classes with the Reflections library
Reflections reflections = new Reflections();
Set> allClasses = reflections.getSubTypesOf(Object.class);
Next, build up a lookup-map.
// Doesn't handle collisions (you might want to use a multimap such as http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Multimap.html instead)
Map> classBySimpleName = new HashMap<>();
for(Class> c : allClasses) {
classBySimpleName.put(c.getSimpleName(), c);
}
When you need to lookup a class you'll do:
Class> clazz = classBySimpleName.get(className);