1

I am using the compiler Tree api to parse my code into a AST, but the method visitCompilationUnit is never called, althrough the method visitClass is called, what am I doing wrong?

Second question: is there any way to tell the compiler to truncate the compiled code (I am interrested only in the AST, not in the class file).

Thanks.

@SupportedSourceVersion(value=SourceVersion.RELEASE_7)
@SupportedAnnotationTypes("*")
public class Parser extends AbstractProcessor {
.
.
.
    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnvironment) {
       for (Element e : roundEnvironment.getRootElements()) {
            System.out.println(e + "***");
            TreePath tp = trees.getPath(e);
            // invoke the scanner
            rootVisitor.scan(tp, trees);
        }
        return true;
    }
}



public class OdpaVisitor extends TreePathScanner<Object, Trees> {

    protected RepositoryIface repository;

    private String pckg;

    public OdpaVisitor(RepositoryIface repository) {
        this.repository = repository;
    }

    @Override
    public Object visitCompilationUnit(CompilationUnitTree node, Trees p) {
        repository.savePackage(node.getPackageName().toString());
        this.pckg = node.getPackageName().toString();
        return super.visitCompilationUnit(node, p);
    }    

    @Override
    public Object visitClass(ClassTree node, Trees p) {
        repository.saveClass(node.getSimpleName().toString(), pckg);
        return super.visitClass(node, p);
    }
}

1 Answer 1

2

You probably got a reference to a ClassTree using the getTree method.

You need

  1. Get a reference to a TreePath using the getPath method - use your previously discovered ClassTree as the argument e.
  2. Get a reference to the CompilationUnitTree using the getCompilationUnit method.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.