CustomTreeVisitor extends AbstractParseTreeVisitor implements ParseTreeItemVisitor { @Override public Object visit(final ParseTree tree) { final int n = tree.getChildCount(); for (int i = 0; i < n; i++) { final ParseTree c = tree.getChild(i); visit(c); } for (final ParseTreeItemVisitor visitor : this.visitors) { visitor.apply(tree); } return null; } CustomTreeVisitor(final ParseTreeItemVisitor... visitors); @Override Object visit(final ParseTree tree); @Override void fillContext(SensorContext context, AntlrContext antlrContext); @Override void apply(ParseTree tree);  }
@Test public void visit() throws IOException { final CharStream charStream = CharStreams.fromStream(this.getClass().getResourceAsStream("/dart/main.dart")); final Dart2Lexer lexer = new Dart2Lexer(charStream); lexer.removeErrorListeners(); final CommonTokenStream stream = new CommonTokenStream(lexer); stream.fill(); final Dart2Parser parser = new Dart2Parser(stream); parser.removeErrorListeners(); final ParseTree root = parser.compilationUnit(); CustomTreeVisitor customTreeVisitor = new CustomTreeVisitor(new ParseTreeItemVisitor() { @Override public void apply(ParseTree tree) { } @Override public void fillContext(SensorContext context, AntlrContext antlrContext) { assertThat(antlrContext.getTokens().length).isEqualTo(371); } }); AntlrContext antlrContext = AntlrUtils.getRequest(IOUtils.toString(this.getClass().getResourceAsStream("/dart/main.dart"), "UTF-8")); customTreeVisitor.visit(root); customTreeVisitor.fillContext(null, antlrContext); }