从JDT AST解析源代码时,我可以使用JDT搜索引擎吗? [英] Can I use JDT search engine while parsing a source from JDT AST

查看:113
本文介绍了从JDT AST解析源代码时,我可以使用JDT搜索引擎吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JDT AST解析给定的源。我想在使用AST时触发给定的访问者时找到给定对象/变量的引用。
例如:
考虑以下代码:

I'm using JDT AST to parse a given source. I want to find the references of a given object/variable when it triggers the relavant visitor when using AST. E.g.: Consider the following code:

public class SampleClass {
    public void printMe(){
        System.out.println("hello");
    }

    public static void main(String a[]){
        SampleClass s =new SampleClass();
        // do some other work
        s.printMe();
    }
}

当我解析以上代码时,在变量声明为 s时,它将调用 VariableDeclarationFragment类型的visitor方法。那时,我想在访问其余代码行之前找出变量 s的所有引用。这可能吗?我想到了使用JDT SearchEngine并在那时调用分别解析引用。但是没有成功。

When I'm parsing the above code, when it comes to the variable declaration of "s", it will call the visitor method of "VariableDeclarationFragment" type. At that point I want to find out all the references of variable "s" before going to visit rest of the code lines. Is this possible? I thought of using JDT SearchEngine and call at that point to resolve the references separately. But wasn't successful. Can I do it by only using AST itself?

请注意,我在独立程序中使用JDT AST,而不是在Eclipse插件项目中使用。在这种情况下,我是否能使用SearchEngine感到有些困惑,因为它无法解析给定代码单元(类,方法等)的IJava *类型。
请分享您的专业知识来解决这个问题。

Please note that I'm using JDT AST in a stand-alone program and not as a Eclipse plugin project. I'm bit confused whether I can use the SearchEngine in that case as it couldnt resolve IJava* types for a given code unit (class,method etc.). Please share your expertise to sort out this.

推荐答案

使用搜索引擎是过分的。搜索引擎用于跨文件搜索。而且,如果没有启动工作台(即-在后台没有Eclipse实例),则无法使用搜索引擎。

Using the search engine is overkill. The search engine is meant for cross file searching. And, without the workbench started (ie- without an Eclipse instance in the background) you cannot use the search engine.

看起来您只想查找对变量的引用在同一文件中。最好的选择是创建一个访问者,该访问者将访问整个文件并查找对该变量的引用。由于这些是变量,并且它们的范围不会转义声明它们的方法,因此您只需要访问该方法。

It looks like you only want to find references to variables in the same file. Your best bet here is to create a visitor that will visit the entire file and look for references to the variable. Since these are variables and their scope doesn't escape the method they are declared in, you only need to visit that method.

类似这样的事情:

class MyVariableVisitory extends ASTVisitor {
    public boolean visit(SimpleName node) {
        if (node.getIdentifier().equals(variableToLookFor)) {
            acceptMatch(node);
        }
        return true;
    }
}

由于您只在寻找对变量的引用,因此您只需查看名称 ast节点。

Since you are only looking for references to variables, you only need to look at Name ast nodes.

这篇关于从JDT AST解析源代码时,我可以使用JDT搜索引擎吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆