如何在 Eclipse 中搜索对 AST 或 CompilationUnit 上字段的引用?

发布于 2024-11-05 05:57:12 字数 553 浏览 0 评论 0原文

嗨,

我正在开发一个 Eclipse 插件。我 需要找到所有参考文献 使用 AST 或 jdt.core.dom 的源 或类似的东西。我需要这个 诸如 ASTNodes 之类的引用,以便 获取父节点并检查几个 表达式中的事物 where 涉及参考文献。 预先感谢。


编辑:

我想更具体一点,我的问题是我尝试捕获一些对常量的引用,但是...我不知道如何才能捕获此引用的匹配项。我需要检查涉及对确定常量的引用的表达式。我只得到使用它们的方法的来源。

我认为问题在于范围或模式:

pattern = SearchPattern.createPattern(field, IJavaSearchConstants.REFERENCES);


scope = SearchEngine.createJavaSearchScope(declaringType.getMethods());

预先感谢!

Hi,

I'm developing an Eclipse plugin. I
need to find all the references in the
source using AST's or jdt.core.dom
or something like that. I need this
references like ASTNodes in order to
get the parent node and check several
things in the expression where
references are involved.
Thanks beforehand.


Edited:

I want to concrete a little more, My problem is that I try to catch some references to a constant but... I have not idea how I can do to catch in the matches this references. I need check the expressions which the references to a determined constant are involved. I only get the source of the method where they are used.

I think the problem is the scope or the pattern:

pattern = SearchPattern.createPattern(field, IJavaSearchConstants.REFERENCES);


scope = SearchEngine.createJavaSearchScope(declaringType.getMethods());

Thanks beforehand!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅沫记忆 2024-11-12 05:57:13

我用过类似的东西:

  1. 搜索声明
    方法,返回一个 IMethod
  2. Search 以查找对
    IMethod,记录这些 IMethod
  3. 对于每个返回的 IMethod,创建一个
    从其编译单元

搜索声明或引用的 AST 类似于以下代码。

SearchRequestor findMethod = ...; // do something with the search results
SearchEngine engine = new SearchEngine();
IJavaSearchScope workspaceScope = SearchEngine.createWorkspaceScope();
SearchPattern pattern = SearchPattern.createPattern(searchString,
            IJavaSearchConstants.METHOD, IJavaSearchConstants.DECLARATIONS,
            SearchPattern.R_EXACT_MATCH);
SearchParticipant[] participant = new SearchParticipant[] { SearchEngine
            .getDefaultSearchParticipant() };
engine.search(pattern, participant, workspaceScope, findMethod,
                monitor);

获得 IMethod 引用后,您可以使用以下方法访问 AST:

ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setResolveBindings(true);
if (methodToSearch.isBinary()) {
    parser.setSource(methodToSearch.getClassFile());
} else {
    parser.setSource(methodToSearch.getCompilationUnit());
}
CompilationUnit cu = (CompilationUnit) parser.createAST(null);

请参阅 http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse.jdt.doc.isv/guide/jdt_int_core.htm 了解有关 java 搜索的更多详细信息、java 模型和 AST。

I used something like:

  1. Search for the declaration of an
    method, returns an IMethod
  2. Search for references to the
    IMethod, record those IMethods
  3. For each IMethod returned, create an
    AST from its compilation unit

Searching for declarations or references looks like the following code.

SearchRequestor findMethod = ...; // do something with the search results
SearchEngine engine = new SearchEngine();
IJavaSearchScope workspaceScope = SearchEngine.createWorkspaceScope();
SearchPattern pattern = SearchPattern.createPattern(searchString,
            IJavaSearchConstants.METHOD, IJavaSearchConstants.DECLARATIONS,
            SearchPattern.R_EXACT_MATCH);
SearchParticipant[] participant = new SearchParticipant[] { SearchEngine
            .getDefaultSearchParticipant() };
engine.search(pattern, participant, workspaceScope, findMethod,
                monitor);

Once you have your IMethod references, you can get to the AST using:

ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setResolveBindings(true);
if (methodToSearch.isBinary()) {
    parser.setSource(methodToSearch.getClassFile());
} else {
    parser.setSource(methodToSearch.getCompilationUnit());
}
CompilationUnit cu = (CompilationUnit) parser.createAST(null);

See http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse.jdt.doc.isv/guide/jdt_int_core.htm for more details on java search, the java model, and the AST.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文