本文介绍了FieldDeclaration到IField-从FieldDeclaration获取IBinding的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 FieldDeclaration (类型:ASTNode)转换为 IField (类型:JavaElement)。是否可以从FieldDeclaration ASTNode获取绑定,就像node.resolveBinding()一样,用于MethodDeclaration节点。

How can a FieldDeclaration (type: ASTNode) be converted to an IField (type: JavaElement). Is it possible to get the binding from the FieldDeclaration ASTNode, just like node.resolveBinding() as for MethodDeclaration node.

需要:我是访问具有公共常量的Class中的FieldDeclaration节点,并希望在项目中搜索该字段的引用。我使用的是JDT的SearchEngine。为此,我想创建一个搜索模式,如下所示:

Need : I am visiting a FieldDeclaration node in a Class having public constants, and want to search references for that field in the project. I am using JDT's SearchEngine for same. For this I want to create a search pattern as follows :

SearchPattern.createPattern(iField, IJavaSearchConstants.REFERENCES);

我已将此作为对我的,但没有得到相同答案。将其作为单独的问题发布。

I have asked this as a comment to one of my questions, but didn't get the answer for same. Posting it as separate question.

预先感谢答案。

回答Deepak的答案。

In reply to Deepak's answer.

使用您的方法,我可以按以下方式检索JavaElement

Using your approach i can retrieve the JavaElement as follows

List<VariableDeclarationFragment> fragments = node.fragments();
VariableDeclarationFragment fragment = fragments.get(0);
IJavaElement fieldElement = fragment.resolveBinding().getJavaElement();

如果我通过此IJavaElement而不是IField来创建搜索模式,它将返回相同的结果

If i am passing this IJavaElement to create the search pattern instead of the IField will it return the same result as those for an IField.

推荐答案

与往常一样,ASTView插件是您的朋友! :-)在ASTView中,您可以看到该绑定可用于VariableDeclarationFragment,但不能用于FieldDeclaration。

As usual ASTView plugin is your friend! :-) In the ASTView you can see that binding is available for VariableDeclarationFragment but not for a FieldDeclaration.

从FieldDeclaration获取绑定

Getting the binding from FieldDeclaration


  • 获取FieldDeclaration的碎片 =>现在,您有一堆VariableDeclarationFragment节点

  • 调用VariableDeclarationFragment#resolveBinding()(this方法是从VariableDeclaration继承的)

这篇关于FieldDeclaration到IField-从FieldDeclaration获取IBinding的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 14:36