泛型使用

1、被调用类
public class ReflectTest { public void test1(HttpServletRequest request, HttpServletResponse response) { System.out.println("进入。。。。。。。。。。。参数 rep,res接口"); } }
2、公共方法
/** * 反射公共方法 * * @param className 类路径 * @param methodName 调用方法名称 * @param parameterTypes 参数的数据类型数据 * @param paramsValue 参数值数据 */ public Object reflect(String className, String methodName, Class[] parameterTypes, Object[] paramsValue) { Object result = null; try { Class clazz = Class.forName(className); // 无参构造 Constructor constructor = clazz.getDeclaredConstructor(); // 私有方法要使用这个方法获取权限 constructor.setAccessible(true); // 得到反射对象 Object reflectTest = constructor.newInstance(); // 得到调用方法 Method method = clazz.getDeclaredMethod(methodName, parameterTypes); // 私有方法要调用这个 method.setAccessible(true); // 开始调用方法 result = method.invoke(reflectTest, paramsValue); } catch (Exception e) { e.printStackTrace(); return 1; } return result; }
3、main入口
public static void main(String[] args) {
		ReTest reTest = new ReTest();
		String className = "com.tydic.project.redis.test.ReflectTest";
		String methodName = "test1";
		Class[] parameterTypes = new Class[] { HttpServletRequest.class, HttpServletResponse.class };
		Object[] argss = new Object[] { null, null };
		Object result = reTest.reflect(className, methodName, parameterTypes, argss);
		System.out.println(result);
	}


原文地址:https://www.cnblogs.com/gaolt/p/11592666.html