0

I write a custom annotation Processor, I hope get ast tree in process method in eclipse ecj compiler:

private JavacProcessingEnvironment env;
private BaseProcessingEnvImpl eclipseEnv;

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    
    if (!annotations.isEmpty()) {

        Set<? extends Element> elementsAnno = roundEnv.getElementsAnnotatedWith(xxxxAnnoClass.class);

        if (env != null) {  // javac
            Context context = env.getContext();
            Trees trees = Trees.instance(env);
            elementsAnno.forEach(e -> {
                JCTree tree = ((JavacTrees) trees).getTree(e);
            });
            new TreeTranslator().translate(tree);

        }else {         // eclipse ecj
            // how to write the code equivalent of javac 


        }
    }
}
Guo
  • 1,761
  • 2
  • 22
  • 45
  • To my knowledge ecj has its own AST API not related to `com.sun.tools.javac.tree.*`. So, therefore it is not possible. Please note, the API you use are marked with _"This is NOT part of any supported API."_ (in contrast to the AST API of ecj). – howlger Jun 19 '21 at 17:37
  • @howlger I am not trying to related to `com.sun.tools.javac.tree.*`, I just want to do something similar. I just want to get ecj's ast tree**(not jctree)** from element in **eclipse ecj**. – Guo Jun 19 '21 at 22:50
  • I see. So you want to [something like this](https://stackoverflow.com/q/65254336/6505250), but override the methods that visit the annotations rather than field declarations (see [Javadoc of ASTVisitor](https://help.eclipse.org/latest/topic/org.eclipse.jdt.doc.isv/reference/api/org/eclipse/jdt/core/dom/ASTVisitor.html)). – howlger Jun 21 '21 at 08:24

0 Answers0