核心代码逻辑:
public class Unitls { /** * @description 调用指定类名的某方法,接收动态传参。 */ private static Object invokeClassMethod(String className,String methodName,Object... args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { Class clazz = Class.forName(className); Object obj = clazz.newInstance(); Method method = obj.getClass().getMethod(methodName, getArgsClassTypes(args)); return method.invoke(obj,args); } /** * @description 根据参数值集合,返回参数值类型集合 */ private static Class<?>[] getArgsClassTypes(Object... args){ //定义类型数组 Class<?>[] argsClassTypes = new Class<?>[args.length]; for (int i = 0; i<args.length;i++) { argsClassTypes[i] = args[i].getClass(); } return argsClassTypes; } }
搭配Console控制台实现动态传参利用反射调用方法:
private static final StringBuffer methodArgsTipsMessage=new StringBuffer(); //提示根据提示输入方法参数数字(9 结束输入 0字符串 1布尔值 ) private static final HashMap<String,String> methodArgsMap=new HashMap<>(); /** * @description 主方法 */ public static void methodInvoke() throws SecurityException, IllegalArgumentException{ //控制台输入输出控制 commandLineManagement(); } /** * @description 控制台输入输出管理,用于解析用户控制台输入内容并执行内部逻辑。 */ private static void commandLineManagement(){ Scanner scanner = new Scanner(System.in); //获取需要反射的类名 String className = getInputClassName(scanner); //获取需要调用的类方法名 String methodName = getInputMethodName(scanner); initMethofArgsMap(scanner); //获取方法参数,进一步确定需要调用的类方法及传参调用 ArrayList<Object> args = getInputMethodArgs(scanner); try { //方法传参调用(反射) invokeClassMethod(className,methodName,args.toArray()); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } private static String getInputClassName(Scanner scanner){ System.out.println("请输入类名"); return scanner.next(); } private static String getInputMethodName(Scanner scanner){ System.out.println("请输入方法名"); return scanner.next(); } private static void initMethofArgsMap(Scanner scanner){ methodArgsMap.put("0","String"); methodArgsMap.put("1","Boolean"); methodArgsMap.put("2","Long"); methodArgsMap.put("3","Short"); methodArgsMap.put("4","Float"); methodArgsMap.put("5","Int"); String exitFlag="请按规则输入方法参数类型:9 Stop the input \r\n"; methodArgsTipsMessage.append(exitFlag); methodArgsTipsMessage.append(methodArgsMap.toString()); } private static Object getScannerValue(String argsType,Scanner scanner){ switch(argsType){ case "String": System.out.println("您接下来要输入的类型必须是字符串类型"); return scanner.next(); case "Boolean": System.out.println("您接下来要输入的类型必须是布尔类型"); return scanner.nextBoolean(); case "Long": System.out.println("您接下来要输入的类型必须是长整型类型"); return scanner.nextLong(); case "Short": System.out.println("您接下来要输入的类型必须是短整型类型"); return scanner.nextShort(); case "Float": System.out.println("您接下来要输入的类型必须是浮点型类型"); return scanner.nextFloat(); case "Int": System.out.println("您接下来要输入的类型必须是整型类型"); return scanner.nextInt(); } return null; } private static ArrayList<Object> getInputMethodArgs(Scanner scanner){ System.out.println(methodArgsTipsMessage.toString()); String inputType = scanner.next(); ArrayList<Object> argsList = new ArrayList<>(); //继续监听scanner,收集输入的参数值,直到输入值不能被解析,则停止收集。 while(methodArgsMap.containsKey(inputType)){ argsList.add(getScannerValue(methodArgsMap.get(inputType),scanner)); System.out.println(methodArgsTipsMessage); inputType = scanner.next(); } scanner.close(); System.out.println("尝试调用反射指定类的方法"); return argsList; }