Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 representes 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

like image 759
hizki Avatar asked Nov 05 '22 05:11

hizki


1 Answers

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.

like image 103
Tomas Narros Avatar answered Nov 11 '22 05:11

Tomas Narros