1 Reply Latest reply: Aug 6, 2009 8:03 AM by 843793 RSS

    Visiting a class using Trees API

      Hi, say I have the following class (simplified)
      public class SourceCodeProcessorImpl extends SimpleTreeVisitor<Object, Trees> {
          private Trees trees;
          private TreePath treePath
          public void setTrees(Trees trees) {
              this.trees = trees;
          public void setTreePath(TreePath treePath) {
              this.treePath= treePath;
          public Object visitMethodInvocation(MethodInvocationTree node, Trees p) {
              return super.visitMethodInvocation(node, p);
          public void process() throws SourceProcessorException {
              treePath.accept(this, trees);
      When I set the TreePath for the class element, and the Trees instance and call process() on my class should anything happen?

      visitMethodInvocation is not being invoked, when I assumed it should be.

      Is there anything glaringly obvious I have missed or misunderstood?



      Edited by: Milesy on Aug 5, 2009 8:19 AM
        • 1. Re: Visiting a class using Trees API
          I have also tried the following method


          My code looks like.
              public boolean process(Set<? extends TypeElement> annotations,
                                     RoundEnvironment roundEnv) {
                  // For each annotation type to be processed.
                  for (TypeElement annotation : annotations) {
                      // For each annotated element.
                      for (Element element : roundEnv.getElementsAnnotatedWith(annotation)) {
                          // If the annotated element is a class.
                          if (element.getKind().isClass()) {
                              // Process class element.
                              Trees trees = Trees.instance(processingEnv);
                              TreePath treePath = trees.getPath(element);
                              TestScanner testScanner = new TestScanner();
                              System.out.println("1: " + treePath);
                              System.out.println("2: " + trees);
                              testScanner.scan(treePath, trees);
                  return true;
              private class TestScanner extends TreeScanner<Object, Trees> {
                  public Object visitMethod(MethodTree node, Trees p) {
                      return super.visitMethod(node, p);
                  public Object visitClass(ClassTree node, Trees p) {
                      return super.visitClass(node, p);
          All I get is the following error during processing.
          1: com.sun.source.util.TreePath@1bb60c3
          2: com.sun.tools.javac.api.JavacTrees@cdb06e
          An annotation processor threw an uncaught exception.
          Consult the following stack trace for details.
                  at com.sun.source.util.TreePath.access$000(TreePath.java:23)
                  at com.sun.source.util.TreePath$1.hasNext(TreePath.java:108)
                  at com.sun.source.util.TreeScanner.scan(TreeScanner.java:74)
                  at annotations.processing.processors.annotation.CoreC
                  at annotations.processing.processors.annotation.SpsaC
                  at com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcess
                  at com.sun.tools.javac.processing.JavacProcessingEnvironment.discoverAnd
                  at com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessin
                  at com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler
                  at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:727)
                  at com.sun.tools.javac.main.Main.compile(Main.java:353)
                  at com.sun.tools.javac.main.Main.compile(Main.java:279)
                  at com.sun.tools.javac.main.Main.compile(Main.java:270)
                  at com.sun.tools.javac.Main.compile(Main.java:69)
                  at com.sun.tools.javac.Main.main(Main.java:54)
          Thank You