如何在eclipse中的AST或CompilationUnit的字段上搜索引用? [英] How could I search references to a field on a AST or a CompilationUnit in eclipse?
问题描述
您好,
Hi,
我正在开发一个Eclipse插件。 I
需要使用AST或$ code> jdt.core.dom
来查找
源中的所有引用。我需要这个
引用,如 ASTNodes
,以便
获取父节点,并在表达式中检查几个
项,其中
涉及参考。
事先感谢。
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());
提前感谢
推荐答案
我使用的东西如下:
- 搜索
方法的声明,返回一个IMethod - 搜索对
的引用IMethod,记录这些IMethods - 对于返回的每个IMethod,从其编译创建一个
AST单位
搜索声明或引用如下所示:
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);
一旦你有你的IMethod参考,你可以使用:
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);
请参阅 http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse.jdt .doc.isv / guide / jdt_int_core.htm 有关java搜索,java模型和AST的更多详细信息。
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.
这篇关于如何在eclipse中的AST或CompilationUnit的字段上搜索引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!