FieldDeclaration到IField——从FieldDeclaration获取IBinding

时间:2023-01-14 11:59:38

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.

如何将FieldDeclaration (type: ASTNode)转换为IField (type: JavaElement)。是否可以从FieldDeclaration ASTNode获得绑定,就像node. resolvebinding()和MethodDeclaration节点一样。

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 :

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

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.

我问了这个问题作为对我的一个问题的评论,但是没有得到同样的答案。把它作为单独的问题发布。

Thanks in advance for the answer.

谢谢你的回答。


In reply to Deepak's answer.

作为对迪帕克回答的回答。

Using your approach i can retrieve the JavaElement as follows

使用您的方法,我可以像下面这样检索JavaElement。

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

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.

如果我传递这个IJavaElement来创建搜索模式,而不是IField,它会返回与IField相同的结果吗?

1 个解决方案

#1


4  

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

和往常一样,ASTView插件是你的朋友!:-)在ASTView中,您可以看到绑定可用于变量声明片段,但不适用于字段声明。

Getting the binding from FieldDeclaration

从FieldDeclaration获取绑定

  • Get the 'fragments' of the FieldDeclaration => you now have a bunch of VariableDeclarationFragment nodes
  • 获取FieldDeclaration =>的“片段”现在您有了一组可变声明片段节点
  • Call VariableDeclarationFragment#resolveBinding() (this method is inherited from VariableDeclaration)
  • 调用VariableDeclarationFragment#resolveBinding()(此方法继承自VariableDeclaration)

#1


4  

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

和往常一样,ASTView插件是你的朋友!:-)在ASTView中,您可以看到绑定可用于变量声明片段,但不适用于字段声明。

Getting the binding from FieldDeclaration

从FieldDeclaration获取绑定

  • Get the 'fragments' of the FieldDeclaration => you now have a bunch of VariableDeclarationFragment nodes
  • 获取FieldDeclaration =>的“片段”现在您有了一组可变声明片段节点
  • Call VariableDeclarationFragment#resolveBinding() (this method is inherited from VariableDeclaration)
  • 调用VariableDeclarationFragment#resolveBinding()(此方法继承自VariableDeclaration)