我正在使用编译器 Tree api 将我的代码解析为 AST,但是从不调用方法 visitCompilationUnit,尽管调用了方法 visitClass,我做错了什么?
第二个问题:有什么方法可以告诉编译器截断编译后的代码(我只对 AST 感兴趣,而不对类文件感兴趣)。
谢谢。
@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);
}
}