Modify java AST before compiling

L

leppoc

Hi !

I'm trying to modify an Abstract Syntax Tree (a
com.sun.tools.javac.tree.JCTree).

I can get the AST with something like that:

// new compiler, fileManager, compilationUnits1.....
CompilationTask ct = compiler.getTask(null, fileManager, null, null,
null, compilationUnits1);
JavacTask jt = (JavacTask)ct;
try {
Iterable trees = jt.parse();
//... do something else here....
} catch (IOException e) {
e.printStackTrace();
}

Then, I obviously can walk throw the tree....

I now want to modify this AST. Is there any simple way to achieve this
?
For example, if a leaf is a JCBlock, I would like to do something like
that (Just to test):

((JCBlock)t).stats.append(((JCBlock)t).getStatements().last());

but it seems that everything is read-only in those JCTrees.

Then, I would like to generate the .class with the modified tree... any
idea ?

Thank you !
Best regards,

Yohann Coppel.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top