我正在为 Eclipse JDT 编写一些简单的 AST 访问者。我有一个MethodVisitor
和FieldVisitor
类,每个都扩展了ASTVisitor
. 举个MethodVisitor
例子。在那个类的Visit
方法(这是一个覆盖)中,我能够找到每个MethodDeclaration
节点。当我拥有其中一个节点时,我想查看它Modifiers
是否是public
或private
(也许还有其他修饰符)。有一种方法称为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;
}
}