private static void receiveParameterNames(final KlassInfo declaringklass) {
        if (declaringklass.getType().getClassLoader() == null) {
            // We can not find parameter name for class which is in JDK
            return;
        }

        ClassReader cr = null;
        try {
            InputStream stream = ClassLoaderUtils.getClassAsStream(declaringklass.getType());
            cr = new ClassReader(stream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        cr.accept(new ClassVisitor(Opcodes.ASM5) {
            @Override
            public MethodVisitor visitMethod(final int access, final String name, final String desc, final String signature, final String[] exceptions) {
                final MethodInfo method = searchMethod(declaringklass, name, desc);
                if (method == null) {
                    return super.visitMethod(access, name, desc, signature, exceptions);
                }

                MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
                return new MethodVisitor(Opcodes.ASM5, mv) {
                    List<ParameterInfo> parameters = method.getParameters();
                    boolean isStatic = method.isStatic();

                    @Override
                    public void visitLocalVariable(String name, String desc, String signature, Label start, Label end, int index) {
                        int offset = isStatic ? index : index - 1;
                        if (offset >= 0 && offset < parameters.size()) {
                            parameters.get(offset).name = name;
                        }
                        super.visitLocalVariable(name, desc, signature, start, end, index);
                    }

                    int visitParameterIndex = 0; // JDK8 parameter name 是按照循序存储的，这里需要一个计数器

                    @Override
                    public void visitParameter(String name, int access) {
                        parameters.get(visitParameterIndex++).name = name;
                        super.visitParameter(name, access);
                    }
                };
            }

            private MethodInfo searchMethod(KlassInfo declaringklass, String name, String desc) {
                if ("<cinit>".equals(name)) return null;
                if ("<init>".equals(name)) return null;

                jetbrick.asm.Type[] argumentTypes = jetbrick.asm.Type.getArgumentTypes(desc);
                for (MethodInfo method : declaringklass.getDeclaredMethods()) {
                    if (method.getName().equals(name) && argumentTypes.length == method.getParameterCount()) {
                        Class<?>[] types = method.getParameterTypes();
                        boolean matched = true;
                        for (int i = 0; i < argumentTypes.length; i++) {
                            if (!jetbrick.asm.Type.getType(types[i]).equals(argumentTypes[i])) {
                                matched = false;
                                break;
                            }
                        }
                        if (matched) {
                            return method;
                        }
                    }
                }
                return null;
            }
        }, ClassReader.SKIP_FRAMES);
    }
private static void receiveParameterNames ( final KlassInfo declaringklass ) { if ( declaringklass . getType ( ) . getClassLoader ( ) == null ) { return ; } ClassReader cr = null ; try { InputStream stream = ClassLoaderUtils . getClassAsStream ( declaringklass . getType ( ) ) ; cr = new ClassReader ( stream ) ; } catch ( IOException e ) { throw new RuntimeException ( e ) ; } cr . accept ( new ClassVisitor ( Opcodes . ASM5 ) { @ Override public MethodVisitor visitMethod ( final int access , final String name , final String desc , final String signature , final String [ ] exceptions ) { final MethodInfo method = searchMethod ( declaringklass , name , desc ) ; if ( method == null ) { return super . visitMethod ( access , name , desc , signature , exceptions ) ; } MethodVisitor mv = super . visitMethod ( access , name , desc , signature , exceptions ) ; return new MethodVisitor ( Opcodes . ASM5 , mv ) { List < ParameterInfo > parameters = method . getParameters ( ) ; boolean isStatic = method . isStatic ( ) ; @ Override public void visitLocalVariable ( String name , String desc , String signature , Label start , Label end , int index ) { int offset = isStatic ? index : index - 1 ; if ( offset >= 0 && offset < parameters . size ( ) ) { parameters . get ( offset ) . name = name ; } super . visitLocalVariable ( name , desc , signature , start , end , index ) ; } int visitParameterIndex = 0 ; @ Override public void visitParameter ( String name , int access ) { parameters . get ( visitParameterIndex ++ ) . name = name ; super . visitParameter ( name , access ) ; } } ; } private MethodInfo searchMethod ( KlassInfo declaringklass , String name , String desc ) { if ( "<cinit>" . equals ( name ) ) return null ; if ( "<init>" . equals ( name ) ) return null ; jetbrick . asm . Type [ ] argumentTypes = jetbrick . asm . Type . getArgumentTypes ( desc ) ; for ( MethodInfo method : declaringklass . getDeclaredMethods ( ) ) { if ( method . getName ( ) . equals ( name ) && argumentTypes . length == method . getParameterCount ( ) ) { Class < ? > [ ] types = method . getParameterTypes ( ) ; boolean matched = true ; for ( int i = 0 ; i < argumentTypes . length ; i ++ ) { if ( ! jetbrick . asm . Type . getType ( types [ i ] ) . equals ( argumentTypes [ i ] ) ) { matched = false ; break ; } } if ( matched ) { return method ; } } } return null ; } } , ClassReader . SKIP_FRAMES ) ; }
使用 ASM 获取参数名称