Question

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);
    }
}
Était-ce utile?

La solution

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.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top