Hi, I added some statements to an existed AST tree and got a new AST tree, I wanted to compile the new tree, do you have any idea that how to compile AST to bytecode?
The short answer: JDT has no function to directly compile an AST (because the public AST is different from what the compiler uses). Thus you first need to flatten the AST to its text representation and that can be compiled as normal.
For obtaining the text representation two options come to mind:
- use the debug representation of the CompilationUnit (via toString()), but I'm not sure if this is 100% accurate, the JavaDoc mentions "for debugging purposes only".
- create your AST changes via an ASTRewrite which produces TextEdits that can be applied to the original Document => changes will be persisted in the original source files.
- if you don't want to modify source files you should use a WorkingCopy
I'm aware that this is just a terse outline,
HTH,
Stephan