2

我正在为我的 Java 项目使用 Eclipse IDE。我需要从特定类调用的方法列表,即我需要查看从类中调用但未在其中声明的所有方法的列表。我正在寻找一些可能已经存在于 Eclipse 中的选项。我不愿意为此编写代码(这将是我的最后选择)。

让我用这个例子来解释 -

public class ABC {

 public void methodA {
  System.out.println("In methodA");
  BCD bcd = new BCD();
  bcd.callMethodAA(); // defined in class BCD
 }

 public void methodB {
  System.out.println("In methodB");
  CDE cde = new CDE();
  cde.callMethodBB(); // defined in class CDE
 }
}

我想要一个向我展示的选项 - 我们正在从 ABC 类调用 - a) callMethodAA b) callMethodBB

4

7 回答 7

4

如果您只需要列出正在使用的方法,则没有语言内的方法可以实现这一点。虽然可能有一些覆盖工具可以处理这个问题。

但是如果它关于所有可用的方法,你可以使用反射:

    Class<?> cls = Class.forName("className");
    Method[] methodList = cls.getMethods();
于 2013-08-14T13:07:46.103 回答
3

要查找从类中调用的方法(假设以编程方式),我将使用ASM 字节码分析/操作库。下面的示例ClassVisitor打印了从类中调用的所有方法。

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);
        }
    }
}
于 2013-08-14T13:38:33.443 回答
1

您可以通过创建一个调用类中所有方法的方法来实现。如果您已经拥有其中之一,那就更好了。如果我创建这个,以 Junit 的 Logo.java 类为例:

private void ExposeAllCalledMethods()
{
    Logo x = new Logo();
    x.loadImage("something");
    Graphics g;
    x.paint(g);     
}

注意我不需要调用paintBackround() 因为paint() 已经调用了它。

然后我可以右键单击方法名称ExposeAllCalledMethods并选择 Open Call Hierarchy。然后在调用层次结构窗口中单击被调用者按钮(参见图像中的绿色箭头)并打开所有灰色层次结构箭头,如下图所示。显示当前类调用的所有方法的完整列表。

<Shameless Plug> 现在我希望我已经在我的新 Pluralsight Eclipse 课程中展示了如何做到这一点。</无耻插头>

显示所有调用方法的层次结构窗口.

于 2013-08-14T19:25:16.620 回答
1

在 Eclipse 中,您可以通过在编辑器窗格中选择(突出显示)方法,然后构建调用层次结构(可能是 Ctrl-Alt-h)来完成此操作。然后,您可以使用 Call Hierarchy 窗格右上角的切换按钮从“Show Caller Hierarchy”(默认)切换到“Show Callee Hierarchy”。你想要的那个看起来像侧向树,右边有孩子,像 o-[ 不是 ]-o。

于 2016-11-03T20:55:20.290 回答
0

如果是在运行时,您可以使用Dynamic Proxy。动态代理在您的类的方法调用之前被调用,您可以在某处(到另一个类,或文件或其他)记录已调用的方法。

于 2013-08-14T13:12:24.273 回答
0

The answer dependents on the context:

If you like to have this for your code within your IDE: This depends on the IDE. In Eclipse, I believe, it is not possible. You can get the method which calls a given class (constructor of method) via "Open Call Hierarchy".

If you like to do it at runtime: This is solvable with AspectJ, see for example http://www.eclipse.org/aspectj/doc/released/progguide/language-thisJoinPoint.html (at the end there is a piece to get the caller of any method).

于 2013-08-14T13:34:08.333 回答
0

str + o 列出当前打开的类中所有可用的方法。这回答了你的问题了吗?

于 2013-08-14T13:09:45.203 回答