This discussion is archived
1 Reply Latest reply: Aug 6, 2009 6:03 AM by 843793 RSS

Visiting a class using Trees API

843793 Newbie
Currently Being Moderated
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) {
        System.out.println("visitMethodInvocation");
        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?

Thanks

Chris

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

    [http://today.java.net/pub/a/today/2008/04/10/source-code-analysis-using-java-6-compiler-apis.html]

    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) {
                System.out.println("visitMethod");
                return super.visitMethod(node, p);
            }
    
            public Object visitClass(ClassTree node, Trees p) {
                System.out.println("visitClass");
                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.
    java.lang.NullPointerException
            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
    lassElementProcessor.process(CoreClassElementProcessor.java:78)
            at annotations.processing.processors.annotation.SpsaC
    odeAnnotationProcessor.process(SpsaCodeAnnotationProcessor.java:53)
            at com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcess
    or(JavacProcessingEnvironment.java:624)
            at com.sun.tools.javac.processing.JavacProcessingEnvironment.discoverAnd
    RunProcs(JavacProcessingEnvironment.java:553)
            at com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessin
    g(JavacProcessingEnvironment.java:698)
            at com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler
    .java:981)
            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

    Chris