[JSR308] Modifying the annotations in the source

Adrian Kuhn akuhn at iam.unibe.ch
Fri Jan 23 09:35:20 EST 2009


Hi Niko,

On 23.01.2009, at 02:52, Mahmood Ali wrote:

> It's possible to manipulate the compiler structures by casting them to
> the javac internal structure and manipulating the annotations fields
> (e.g. Tree to JCTree and Element to Symbol). The approach is a bit of
> gotcha points though:
> 1. A tree clean up routine is invoked after annotation processing
> phase.  As far as I can tell/remember it only removes the Symbol
> information from the tree, so it would be recalculated later.  If you
> simply manipulate the Element information, those changes might be lost
> before reaching code generation stage.  Also, you would need to use
> the AnnotationMirror compiler implementation (Attribute.Compound)
> rather than you own.
>
> 2. You might be better off, manipulating the tree itself, by using
> TreeMaker.  This page (http://www.ahristov.com/tutorial/java-compiler/new-operator.html
> ) explains some of the TreeMaker API points.  However, the code
> samples aren't applicable to your case directly.


I have a simple example that uses AST casting and TreeMaker to replace  
assert statements with real exceptions in the generated code, maybe  
this is of help for you.

http://code.google.com/p/forceassertions/source/browse/trunk/src/ch/akuhn/fa/ForceAssertions.java
http://www.iam.unibe.ch/~akuhn/blog/2008/roman-numerals-in-your-java/

cheers,
AA



--
Adrian Kuhn
Software Composition Group
University of Bern, Switzerland
http://www.iam.unibe.ch/~akuhn






More information about the JSR308 mailing list