反射方法:
public class ReflectionUtil { /** * 调用示例 * public ApiResponse<String> myTest() throws Exception { * Class[] argsType=new Class[]{Class.forName("java.lang.String")}; * Object[] args=new Object[]{"hello"}; * Object obj = ReflectionUtil.invokeMethod(new ReportImpl(),"Test",argsType,args); * return new ApiResponse().Success((String)obj); * } * @param owner 类的实例 * @param methodName 方法名 * @param argsClass 参数类型 * @param args 参数 * @return * @throws Exception */ public static Object invokeMethod(Object owner,String methodName,Class[] argsClass,Object[] args) throws Exception{ Object objRtn=null; Class ownerClass = owner.getClass(); Method method = ownerClass.getMethod(methodName, argsClass); objRtn = method.invoke(owner, args); return objRtn; } }