如何确定 Eclipse JDT 中方法或字段的修饰符?
我正在为 Eclipse JDT 编写一些简单的 AST 访问者。我有一个 MethodVisitor
和 FieldVisitor
类,它们各自扩展了 ASTVisitor
。以MethodVisitor
为例。在该类的 Visit
方法(这是一个重写)中,我能够找到每个 MethodDeclaration
节点。当我有这些节点之一时,我想查看它的修饰符,看看它是公共的还是私有的(也许还有其他修饰符,例如出色地)。有一个名为 getModifiers()
的方法,但我不清楚如何使用它来确定应用于特定 MethodDeclaration
的修饰符类型。我的代码发布在下面,如果您有任何想法,请告诉我如何继续。
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.MethodDeclaration;
public class MethodVisitor extends ASTVisitor {
private List<MethodDeclaration> methods;
// Constructor(s)
public MethodVisitor() {
this.methods = new ArrayList<MethodDeclaration>();
}
/**
* visit - this overrides the ASTVisitor's visit and allows this
* class to visit MethodDeclaration nodes in the AST.
*/
@Override
public boolean visit(MethodDeclaration node) {
this.methods.add(node);
//*** Not sure what to do at this point ***
int mods = node.getModifiers();
return super.visit(node);
}
/**
* getMethods - this is an accessor methods to get the methods
* visited by this class.
* @return List<MethodDeclaration>
*/
public List<MethodDeclaration> getMethods() {
return this.methods;
}
}
I am writing some simple AST visitors for the Eclipse JDT. I have a MethodVisitor
and FieldVisitor
class which each extend the ASTVisitor
. Take the MethodVisitor
for instance. In that class' Visit
method (which is an override), I am able to find each of the MethodDeclaration
nodes. When I have one of those nodes, I want to look at its Modifiers
to see whether it is public
or private
(and perhaps other modifiers as well). There is a method called getModifiers()
, but it is unclear to me how to use this to determine the type of modifier applied to the particular MethodDeclaration
. My code is posted below, please let me know if you have any ideas how to proceed.
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.MethodDeclaration;
public class MethodVisitor extends ASTVisitor {
private List<MethodDeclaration> methods;
// Constructor(s)
public MethodVisitor() {
this.methods = new ArrayList<MethodDeclaration>();
}
/**
* visit - this overrides the ASTVisitor's visit and allows this
* class to visit MethodDeclaration nodes in the AST.
*/
@Override
public boolean visit(MethodDeclaration node) {
this.methods.add(node);
//*** Not sure what to do at this point ***
int mods = node.getModifiers();
return super.visit(node);
}
/**
* getMethods - this is an accessor methods to get the methods
* visited by this class.
* @return List<MethodDeclaration>
*/
public List<MethodDeclaration> getMethods() {
return this.methods;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如文档所述,调用
getModifiers()
的结果是相关修饰符
常量。例如,如果您想查明该方法是否为final
,您可以使用:或者您可以使用
Modifier
类中的便捷方法:As the documentation states, the result of calling
getModifiers()
is a bit-wise "or" of the relevantModifier
constants. So for example, if you want to find out whether or not the method isfinal
you'd use:Or you can use the convenience methods in the
Modifier
class:还有一个辅助方法
modifiers()
可以为您提供您的方法所具有的修饰符列表。要检查它是否是final
,您可以直接检查该列表。There is one more helper method
modifiers()
which gives you the list of modifiers your method has. To check whether it isfinal
or not, you can directly check in that list.