I have been looking for a solution for below requirement -

  • Source files are written with Custom Annotation on a method
  • Method body needs a little variation based on the annotation.
  • Source file should not be changed, but input to compiler should be modified source file

I have looked at below APIs -

  • javax.annotation.processing - Annotation processing.
  • javax.lang.model.* - Language model used in annotation processing and Compiler Tree API
  • com.sun.source.* - Compiler Tree API.

I thought of designing this by following :

  1. Write an annotation processor
  2. Generate the compiler tree
  3. Edit the compiler tree at runtime without affecting origional source file
  4. Supply the tree to compiler

Compiler Tree API appears to be promissing where it gives access to com.sun.source.tree.MethodTree

However compiler Tree API appears to be Read Only. I can not figure out how to acomplish the steps 3 & 4

Is there any API for this which I can adopt to acomplish the task

NOTE: I am looking for only Source Code manipulation technique. No runtime byte code manipulation / AOP

Environment: Java 6

Edit
Report