Eclipse JDT 从Object-Type 到其构造MethodDeclarion?

From Object-Type to its constructing MethodDeclarion in Eclipse JDT?

我正在尝试使用 Eclipse JDT 从方法的参数转到它的 构造方法(如果它是一个对象)。

我需要 MethodDeclaration ASTNode(因为我可能必须深入研究 如果再次有对象参数,则递归更深 在声明中)。

这与在 Eclipse 中突出显示类型和 按 F3。所以我想这样做是可能的。

在我尝试通过名称和参数查找方法之前 通过循环遍历包的所有已解析的 CompilationUnits。 但这似乎很广泛? (并且解析 + getDeclaringMethod 只给了我 IMethodBinding,这是不同的东西并且不是很有效地转换成 MethodDeclaration?)

有没有更直接的方法从类型节点到方法声明节点 如果它不在同一个 CompilationUnit 中,它的构造函数事件?

您需要从绑定中检索 ICompilationUnit,解析它,然后找到相应的 MethodDeclaration。代码可能如下所示:

IType declaringType = (IType)methodBinding.getDeclaringClass().getJavaElement();
IMethod methodDeclaration = (IMethod)methodBinding.getMethodDeclaration().getJavaElement();

ICompilationUnit declaringUnit = declaringType.getCompilationUnit();
CompilationUnit unit = parseAST(declaringUnit);
unit.accept(new ASTVisitor() {
    @Override
    public boolean visit(MethodDeclaration node) {
        IMethod method = (IMethod)node.resolveBinding().getJavaElement();
        if (method.equals(methodDeclaration)) {
            ...
        }
        return false;
    }
});