- /**
- * 根据方法名称取得反射方法的参数类型(没有考虑同名重载方法使用时注意)
- * @param obj 类实例
- * @param methodName 方法名
- * @return
- * @throws ClassNotFoundException
- */
- public static Class[] getMethodParamTypes(Object classInstance,
- String methodName) throws ClassNotFoundException{
- Class[] paramTypes = null;
- Method[] methods = classInstance.getClass().getMethods();//全部方法
- for (int i = 0; i< methods.length; i++) {
- if(methodName.equals(methods[i].getName())){//和传入方法名匹配
- Class[] params = methods[i].getParameterTypes();
- paramTypes = new Class[ params.length] ;
- for (int j = 0; j < params.length; j++) {
- paramTypes[j] = Class.forName(params[j].getName());
- }
- break;
- }
- }
- return paramTypes;
- }
- //取得方法测试(Test类大家还是任意写吧,这里不列举了)
- Method m = Test.class.newInstance().getClass().getDeclaredMethod("方法名称", getMethodParamTypes(Test.class.newInstance(),"方法名称"));
- //该片段来自于http://www.codesnippet.cn/detail/2212201411378.html
来源: http://www.codesnippet.cn/detail/2212201411378.html