我正在爲Eclipse JDT編寫一些簡單的AST訪問者。我有一個MethodVisitor
和FieldVisitor
類,每個類擴展ASTVisitor
。以MethodVisitor
爲例。在該類'Visit
方法(這是一個覆蓋)中,我能夠找到MethodDeclaration
節點中的每一個節點。當我有其中一個節點時,我想查看其Modifiers
以查看它是否爲public
或private
(也可能是其他修改器)。有一種方法稱爲getModifiers()
,但我不清楚如何使用它來確定應用於特定MethodDeclaration
的修飾符的類型。我的代碼發佈在下面,如果您有任何想法,請告訴我如何繼續。如何確定Eclipse JDT中方法或字段的修飾符?
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;
}
}