Java gets an example of the parameter type of a reflection method based on the method name

  • 2020-04-01 02:58:52
  • OfStack




public static Class[]  getMethodParamTypes(Object classInstance, 
 String methodName) throws ClassNotFoundException{
 Class[] paramTypes = null;
   Method[]  methods = classInstance.getClass().getMethods();//All the way
 for (int  i = 0;  i< methods.length; i++) {
     if(methodName.equals(methods[i].getName())){//Matches the passed 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 a method Test (the Test class is not listed here)
 Method m =  Test.class.newInstance().getClass().getDeclaredMethod(" Method names ", getMethodParamTypes(Test.class.newInstance()," Method names "));


Related articles: