Reputation: 4445
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);
}
}
Upvotes: 1
Views: 1203
Reputation: 10891
You probably got a reference to a ClassTree using the getTree method.
You need
Upvotes: 2