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
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);
}
}
}