3

I am using Javaparser to parse Java source code.

Is there a way to implement a Visitor that can visit the abstract Node class?

I want to visit every Node and print its line number, but I don't want to implement a visit() method for every Node type (AssignExpr, BinaryExpr, IfStmt, etc...) because there are so many types.

jem
  • 130
  • 8
  • That link is a deprecated link as said in the page .Refer [Github for latest code](http://javaparser.github.io/javaparser/) – Madhan Jun 26 '15 at 17:25

1 Answers1

6

A visitor makes sense only to process different types of element. You can instead start from the root (the CompilationUnit) and pass it to your method process:

void process(Node node){ // Do something with the node for (Node child : node.getChildrenNodes()){ process(child); } }

Disclaimer: I am a JavaParser contributor

Federico Tomassetti
  • 2,100
  • 1
  • 19
  • 26