I have a bunch of classes of various names and each has a performLogic function that accepts a number of preset parameters (always the same):
public final class DoSomeAction extends SetupAction {
public void performLogic(param1, param2...
I want a way where I can call it like this:
String actionName = "DoSomeAction";
actionName.performLogic(param1, param2...);
Hope this is clear what I am trying to do.
Thanks for feedback and pointing me towards class.forName() After some further research I was able to implement the following:
try {
Class actionClass = Class.forName(blockAction);
Object obj = actionClass.newInstance();
Class[] parameterTypes = new Class[2];
parameterTypes[0] = String.class;
parameterTypes[1] = String.class;
Method performLogic = actionClass.getDeclaredMethod("performLogic", parameterTypes);
performLogic.invoke(obj, param1, param2);
}
catch(ClassNotFoundException e) {
cat.error("Class not found: " + e.toString());
}