Get method parameter names through Spring LocalVariableTableParameterNameDiscoverer

import org.springframework.core.LocalVariableTableParameterNameDiscoverer
publicclass Test{
    privatestaticfinal LocalVariableTableParameterNameDiscoverer parameterNameDiscoverer = new LocalVariableTableParameterNameDiscoverer();

    /**
     * Get all parameter names of the method
     * @param method
     * @return
     */
    publicstatic String[] getParameterNames(Method method) {
        return parameterNameDiscoverer.getParameterNames(method);
    }

    publicstaticvoidmain(String[] args) throws ClassNotFoundException {
        Class<?> aClass = Class.forName("com.codercool.ssm.modules.system.service.impl.UserServiceImpl");
        Method[] methods = aClass.getMethods();
        StringBuilder sb = new StringBuilder();
        for (Method method : methods) {
            sb.append("方法:"+method.getName() + " ");
            String[] parameterNames = getParameterNames(method);
            if (parameterNames == null || parameterNames.length <1) {
                sb.append("无参");
            } else {
                sb.append("[");
                for (int i = 0; i < parameterNames.length ; i++) {
                    sb.append(parameterNames[i]);
                    sb.append(",");
                }
                sb.append("]");
            }
            sb.append("\n");
        }
        System.out.println(sb.toString());
    }
}


publicclass UserServiceImpl implements IUserService {

    @Autowired
    private UserDao userDao;

    publicvoidaddUser(String userId,User user) {
        userDao.add(user);
    }
}

 



Output result:
Method: addUser [userId,user,]
Method: wait No-parameter
method: wait No-parameter
method: wait No-parameter
method: equals No-parameter
method: toString No-parameter
method: hashCode No-parameter
method: getClass No-parameter
method: notify No parameter
method: notifyAll No parameter

extension information:
Spring This function is implemented based on the ASM framework



Output result:
Method: addUser [userId,user,]
Method: wait No-parameter
method: wait No-parameter
method: wait No-parameter
method: equals No-parameter
method: toString No-parameter
method: hashCode No-parameter
method: getClass No-parameter
method: notify No parameter
method: notifyAll No parameter

extension information:
Spring This function is implemented based on the ASM framework

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326581208&siteId=291194637