Answer to the original question
There is no broad way. You can do this as far as I know. I mean the following code:
public static Object calleableFromAnywhere(Object o) throws IllegalAccessException, InstantiationException {
Object ret = null;
if(!Objects.isNull(o)){
ret = o.getClass().newInstance();
}
return ret;
}
, .
, :
Constructor<?> constructor = o.getClass().getConstructor();
Object something = o.getClass().newInstance();
, ... .
, Class.forName(...) String, .
, String, . - , .
TL.DR.: , . newInstance, , . , 100%.
/
a newInstance . . ( , newInstance, .) , - , , .
, :
public static Class getClass(String name) throws ClassNotFoundException {
return Class.forName(name);
}
name , . , .
public Class getClassExample1(String name) throws ClassNotFoundException {
return this.getClass().getClassLoader().loadClass(name);
}
JavaDoc ClassNotFoundException :
- @see java.lang.Class # forName (java.lang.String)
- @see java.lang.ClassLoader # findSystemClass (java.lang.String)
- @see java.lang.ClassLoader # loadClass (java.lang.String, boolean)
, , , someInstance.getClass(name).
NoClassDefFoundError. , , , maven, , .
TL.DR.V2: , , ( ), .