Skip to main content

APT - Compile time Annotation Processing

3 replies [Last post]
milesy
Offline
Joined: 2009-07-08
Points: 0

Hi,

When processing Annotations are compile time, is there a way to raise a warning or an error in the compile process?

Thanks

Chris

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
jddarcy
Offline
Joined: 2004-11-02
Points: 0

Yes, using apt

com.sun.mirror.apt.Messager
http://java.sun.com/javase/6/docs/jdk/api/apt/mirror/com/sun/mirror/apt/...

using standardized annotation processing in javac as of JDK 6

javax.annotation.processing.Messager
http://java.sun.com/javase/6/docs/api/javax/annotation/processing/Messag...

milesy
Offline
Joined: 2009-07-08
Points: 0

I figured out how to do this, although this seems very undocumented throughout the full internet.

My next question which has stumped me, is when I visit say a method declaration, is there a way for me to access the source file being ran against?

thanks

Chris

jddarcy
Offline
Joined: 2004-11-02
Points: 0

> I figured out how to do this, although this seems
> very undocumented throughout the full internet.
>
> My next question which has stumped me, is when I
> visit say a method declaration, is there a way for me
> to access the source file being ran against?

Both the apt and standardized annotation processing APIs only model up to the the method declaration, the interior of methods and constructors are not covered in the model. However, if you are content using the javac-specified tree API (http://java.sun.com/javase/6/docs/jdk/api/javac/tree/index.html), you can map from a javax.lang.model.Element to a tree representing the source inside a method using com.sun.source.util.Trees.getTree.