What does the accept() method of ASTNode do and how does it use the ASTVisitor?
What does the accept
method of ASTNode do (The javadoc didn't help too much...) and when will the visit(Expression node)
method be called?
Here is an example code of how I need to use it:
final List<Expression> listi = new ArrayList<Expression>();
String stringi = opi.generate(entryContract, true_false_maybe);
// stringi represen开发者_Python百科tes an expression, for example "g!=h".
parser.setSource(stringi.toCharArray());
unit = (CompilationUnit) parser.createAST(null);
ASTNode astRoot = unit.getRoot();
astRoot.accept(new ASTVisitor() {
public boolean visit(Expression node) {
listi.add(node);
return true;
}
});
Thank you
I guess your Expression
class is a subtype of the ASTNode
class, and the ASTVisitor
class present other visit methods (which surely will be empty), accepting as an argument other ASTNode
subclasses.
It's an implementation of the GoF Visitor Design Pattern (also described at Wikipedia).
The accept
method on ASTNode
will just invoke the visit
method on the visitor implementation, passing itself as parameter for the visit
method.
精彩评论