美文网首页
2019-01-25 通过反射获取参数名称的两种方法spring

2019-01-25 通过反射获取参数名称的两种方法spring

作者: Albert陈凯 | 来源:发表于2019-01-25 17:06 被阅读15次
    
    import java.lang.reflect.Method;
    import java.util.Arrays;
    import java.util.stream.Collectors;
    import javassist.ClassPool;
    import javassist.CtClass;
    import javassist.CtMethod;
    import javassist.Modifier;
    import javassist.bytecode.CodeAttribute;
    import javassist.bytecode.LocalVariableAttribute;
    import javassist.bytecode.MethodInfo;
    import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
    
    public class Test {
    
        public static void main(String[] args) throws Exception {
    
            springParamName();
    
            System.out.println("====");
            javassistGetInfo();
        }
    
        static void springParamName() {
    
            LocalVariableTableParameterNameDiscoverer u =
                    new LocalVariableTableParameterNameDiscoverer();
            AuditSave demo = new AuditSave();
            Method[] methods = demo.getClass().getDeclaredMethods();
            Arrays.stream(methods).forEach(
                    x -> {
                        String[] params = u.getParameterNames(x);
    
                        if (null != params && params.length > 0) {
                            System.out
                                    .println(x.getName() + " " +
                                            Arrays
                                                    .stream(params)
                                                    .collect(
                                                            Collectors.joining(","))
                                    );
                        }
    
                    }
            );
    
        }
    
    
        static void javassistGetInfo() throws Exception {
            Class<?> clazz = Class.forName("com.sx.common.aop.AuditSave");
            ClassPool pool = ClassPool.getDefault();
            CtClass cc = pool.get(clazz.getName());
    
            Method[] declaredMethods = clazz.getDeclaredMethods();
            for (Method mt : declaredMethods) {
                String modifier = Modifier.toString(mt.getModifiers());
                Class<?> returnType = mt.getReturnType();
                String name = mt.getName();
                Class<?>[] parameterTypes = mt.getParameterTypes();
    
                System.out
                        .println("parameterTypes: " + Arrays.stream(parameterTypes).map(x -> x.toString())
                                .collect(Collectors.joining(",")));
    
                System.out.print("\n" + modifier + " " + returnType.getName() + " " + name + " (");
    
                //CtMethod[] declaredMethods1 = cc.getDeclaredMethods();
                CtMethod ctm = cc.getDeclaredMethod(name);
                MethodInfo methodInfo = ctm.getMethodInfo();
                CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
                LocalVariableAttribute attribute = (LocalVariableAttribute) codeAttribute
                        .getAttribute(LocalVariableAttribute.tag);
    
    //          System.out.println("attribute: " + new Gson().toJson(attribute));
    
                int pos = Modifier.isStatic(ctm.getModifiers()) ? 0 : 1;
                for (int i = 0; i < ctm.getParameterTypes().length; i++) {
                    System.out.print(parameterTypes[i] + " " + attribute.variableName(i + pos));
                    if (i < ctm.getParameterTypes().length - 1) {
                        System.out.print(",");
                    }
                }
    
                System.out.print(")");
    
                Class<?>[] exceptionTypes = mt.getExceptionTypes();
                if (exceptionTypes.length > 0) {
                    System.out.print(" throws ");
                    int j = 0;
                    for (Class<?> cl : exceptionTypes) {
                        System.out.print(cl.getName());
                        if (j < exceptionTypes.length - 1) {
                            System.out.print(",");
                        }
                        j++;
                    }
                }
    
                System.out.println("\n\n");
            }
    
        }
    
    
    }
    
    

    相关文章

      网友评论

          本文标题:2019-01-25 通过反射获取参数名称的两种方法spring

          本文链接:https://www.haomeiwen.com/subject/gbkvjqtx.html