Somehow we need to check how many arguments and then split the parameter before sending it to invoke method as I did in methodInvoke.
Main class
public static void main(String[] args) {
try {
Class clazz = DummyClass.class;
//If I want run all methods in the same instance
Object instance = clazz.newInstance();
// this method is void
CreateCall.call(clazz, instance, "setObject", new Object[]{1, 2});
System.out.println("a : " + CreateCall.call(clazz, instance, "getA"));
System.out.println("b : " + CreateCall.call(clazz, instance, "getB"));
System.out.println(" a + b :" + CreateCall.call(clazz, instance, "sum"));
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
}
CreateCall class
class CreateCall {
public static Object call(Class clazz, Object instance, String methodName, Object... args) throws InstantiationException, IllegalAccessException, InvocationTargetException {
Object result = null;
Method[] methods = clazz.getDeclaredMethods();
for (Method method : methods) {
if (method.getName().equals(methodName))
result = methodInvoke(instance, method, args);
}
return result;
}
private static Object methodInvoke(Object instance, Method method, Object[] args) throws InvocationTargetException, IllegalAccessException {
return switch (method.getParameterCount()) {
case 0 -> method.invoke(instance);
case 1 -> method.invoke(instance, args[0]);
case 2 -> method.invoke(instance, args[0], args[1]);
case 3 -> method.invoke(instance, args[0], args[1], args[2]);
default -> method.invoke(instance, args);
};
}
}