当前位置:网站首页>Parse method's parameter list (including parameter names)

Parse method's parameter list (including parameter names)

2022-08-10 23:29:00 Technical log

Don't say much, just go to Demo:

public class User {public static void main(String[] args) throws NoSuchMethodException {getMethodParams(User.class,"main",null);}/*** Parse method parameter list:* @param clazz: fully qualified class name* @param methodName: method name* @param agrCount: method with n parameters; if it is empty, this condition is excluded;*/public static void getMethodParams(Class clazz, String methodName, Integer agrCount) {//Get the specified method:Method method = Arrays.stream(clazz.getMethods()).filter((x) -> {return methodName.equals(x.getName()) && (Objects.isNull(agrCount) || agrCount.equals(x.getParameterCount()));}).findFirst().orElse(null);//Get parameter information:if (Objects.nonNull(method)) {for (Parameter parameter : method.getParameters()) {System.out.println("Parameter type: " + parameter.getType());System.out.println("Parameter name: " + parameter.getName());System.out.println("=================================");}}}}

Test results:

原网站

版权声明
本文为[Technical log]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/222/202208102228443838.html