import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
public abstract class MethodCaller {
public static Object execute(Object o, String methodName){
return MethodCaller.execute(o, methodName, null, null);
}
public static Object execute(Object o, String methodName, Class<?>[] types, Object[] params){
Object ret = null;
try{
ret = o.getClass().getMethod(methodName, types).invoke(o, params);
}
catch(Exception e){
e.printStackTrace();
}
return ret;
}
public static Object execute(String className, String methodName){
return MethodCaller.execute(className, methodName, null, null);
}
public static Object execute(String className, String methodName, Class<?>[] types, Object[] params){
Object ret = null;
try{
Class<?> c = Class.forName(className);
boolean b = Modifier.isStatic(c.getDeclaredMethod(methodName).getModifiers());
if( b ){
ret = c.getDeclaredMethod(methodName, types).invoke(null, params);
}
else{
Constructor<?> ct = c.getConstructor();
ret = c.getDeclaredMethod(methodName,types).invoke(ct.newInstance(),params);
}
}
catch(Exception e){
e.printStackTrace();
}
return ret;
}
}