How do I get a list of Methods called from a Class in Eclipse IDE?

前端 未结 7 1677
眼角桃花
眼角桃花 2021-01-23 04:38

I am using Eclipse IDE for my Java project. I need a list of methods which are being called from a particular class i.e. I need to see a list of all the methods which are being

7条回答
  •  攒了一身酷
    2021-01-23 05:01

    To find the methods that are called from a class (assuming programatically), I would use the ASM bytecode analyzing/manipulation library. The below example is a ClassVisitor that prints all the methods called from a class.

    import org.objectweb.asm.ClassAdapter;
    import org.objectweb.asm.ClassVisitor;
    import org.objectweb.asm.MethodVisitor;
    import org.objectweb.asm.commons.InstructionAdapter;
    
    public class MethodCallFinder extends ClassAdapter {
    
        private String name;
    
        public MethodCallFinder(ClassVisitor classVisitor) {
            super(classVisitor);
        }
    
        @Override
        public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
            this.name = name;
            super.visit(version, access, name, signature, superName, interfaces);
        }
    
        @Override
        public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
            return new MethodCallPrinter(super.visitMethod(access, name, desc, signature, exceptions));
        }
    
        private class MethodCallPrinter extends InstructionAdapter {
    
            public MethodCallPrinter(MethodVisitor methodVisitor) {
                super(methodVisitor);
            }
    
            @Override
            public void visitMethodInsn(int opcode, String owner, String name, String desc) {
                System.out.printf("Class %s calls method %s with descriptor %s from class %s%n", MethodCallFinder.this.name, name, desc, owner);
                super.visitMethodInsn(opcode, owner, name, desc);
            }
        }
    }
    

提交回复
热议问题