Copy the code code as follows:
/**
* Obtain the parameter type of the reflection method based on the method name (note when using the overloaded method with the same name without considering it)
* @param obj class instance
* @param methodName method name
* @return
* @throws ClassNotFoundException
*/
public static Class[] getMethodParamTypes(Object classInstance,
String methodName) throws ClassNotFoundException{
Class[] paramTypes = null;
Method[] methods = classInstance.getClass().getMethods();//All methods
for (int i = 0; i< methods.length; i++) {
if(methodName.equals(methods[i].getName())){//Match the incoming method name
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;
}
//Get the method test (you can write whatever you want for the Test class, I won’t list it here)
Method m = Test.class.newInstance().getClass().getDeclaredMethod("Method Name", getMethodParamTypes(Test.class.newInstance(),"Method Name"));