Javaparser: Visit all node types with one method

1.5k views Asked by At

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.

1

There are 1 answers

0
Federico Tomassetti On BEST ANSWER

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