JAvaメソッド名に基づいて反射メソッドのパラメータタイプを取得する例
1183 ワード
/**
* ( )
* @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()," "));