1

I am currently analysing Java source code. I have done AST analysis by using Compiler Tree API to parser source code. But I have a problem to add new AST node(insert a expression or statement for example) to compilation unit which can be compiled by Java compiler later. I did not find any method or class can do this trick.

Mike Laren
  • 8,028
  • 17
  • 51
  • 70
Saddy
  • 313
  • 4
  • 22

1 Answers1

1

I don't think the Java compiler is designed to let you regenerate source code from the AST. After all, it has no need for that.

If you want to modify programs, you might consider to use a source-to-source program transformation system (PTS). These are tools that parse source code (building ASTs), allow you to use source-to-source transformation rules written in the syntax of the parsed source code, to make changes to the ASTs, and can regenerate source text from the ASTs.

PTS rules essentially say, "if you see this, replace it by that". An example for our PTS (the DMS Software Reengineering Toolkit):

rule convert_square_to_multiply:(b: primitive_expression):product -> product
  =  "  \b ** 2 "  ->   " \b * \b " 
  if no_side_effects(b);

This rule finds an expression, raised to power 2, and applies a classic compiler "strength reduction" to make it cheaper to evaluate. Note the extra semantic check constraint on b; your PTS has to be able to help you implement such "analyses".

The rule works by:

  • matching the AST for the parsed source program to the AST implied by the pattern in the left hand side of the rule ("if you see this...")
  • when matched, the pattern variable ("b") is bound to the corresponding subtree
  • checking the "if" condition to verify it is true,
  • if so, the matched subtree is removed and replaced by a tree whose shape is implied by the right hand side of the rule ("replace it by this")

The beauty of all this is the manipulation of the AST without writing grotty procedural code to climb up and down the tree, checking node types, splicing nodes in and out, etc. Source to source rewrites are much easier to write, inspect and maintain.

If you can't use a PTS, you might consider building your own prettyprinter; see my SO answer on how to do that. I doubt the Java compiler captures enough information to regenerate "pretty" source code; it might have enough to regenerate compilable source code. As an example, I'm pretty sure the Java compiler AST does not retain comments; they'll consequently be very hard to regenerate. A standard PTS captures and reproduces all this stuff.

Community
  • 1
  • 1
Ira Baxter
  • 93,541
  • 22
  • 172
  • 341