Java Annotations
From Arnout Engelen
- JSR 308 http://pag.csail.mit.edu/jsr308/ aims to allow annotations in more places.
- JSR 305 (http://jcp.org/en/jsr/detail?id=305) is proposing to standardize several Java annotations like @Pure and @NonNull and give them a defined (if informal) semantics.
Contents |
[edit] File format
Proposed file formats for dumped annotations are JAIF and source-like formats.
A more easily parsable (e.g. XML), standard format would imho be worthwhile. I whipped up an XML-based representation based on the JAIF BNF specification.
[edit] dumper tools
[edit] Trevor Harmon's dumper
Dumping the annotations in a class file (with BCEL), credits to Trevor Harmon:
http://vocaro.com/trevor/files/dump-annotations.tar.gz
The utility takes a class file as input and prints any annotations it finds. It formats this output to the same struct-like representation that the VM spec uses, making the output easy to read. It can be extended without much effort to dump custom annotation formats.
[edit] MIT dumper
(Michel Ernst on the jsr308 list, 9-2-2007)
At MIT, we have built tools that
* extract annotations from a classfile
* insert annotations into a classfile
* insert annotations into a source file
The first two tools work on all the new annotation locations that
JSR308 adds. (We plan to enhance the third tool in that way, too.)
As an intermediate representation, they use the textual format described at
http://pag.csail.mit.edu/jsr308/index-file-format.pdf
(It's not XML, because we wanted it to be readable by people.)
All this is part of the software that we plan to release this month.
Naturally, we can improve the implementation or change the intermediate
representation, based on suggestions from others.
-Mike
[edit] javac6 dumping
for the related task of printing out annotations from class files in a source-like format, JDK 6 javac can be used:
>javac -Xprint javax.annotation.processing.SupportedAnnotationTypes
package javax.annotation.processing;
@java.lang.annotation.Documented
@java.lang.annotation.Target({java.lang.annotation.ElementType.TYPE})
@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
public @interface SupportedAnnotationTypes {
java.lang.String[] value();
}
-Joe
See also:
