JAnnotation represents a single annotation against a code element. The methods described on the JAnnotatedElement interface are used to associate JAnnotation's with various other objects in this package describing Java code elements.
The print method outputs annotations in various forms (as described in the Java Language Specification Third Edition) based on the methods called.
For "Marker Annotation", construct with the appropriate JAnnotationType.
JAnnotationType preliminaryType = new JAnnotationType("Preliminary"); JAnnotation preliminary = new JAnnotation(preliminaryType);
outputs
@Preliminary()
For "Single Element Annotation", construct as above and call the setValue(value) method to set the value of the "value" element of the annotation type.
JAnnotationType copyrightType = new JAnnotationType("Copyright"); JAnnotation copyright = new JAnnotation(copyrightType); copyright.setValue("\"2002 Yoyodyne Systems, Inc., All rights reserved.\"");
outputs
@Copyright("2002 Yoyodyne Propulsion Systems, Inc., All rights reserved.")
For "Normal Annotation," construct as above then call the appropriate setValue methods that accept an "elementName" parameter.
JAnnotationType requestType = new JAnnotationType("RequestForEnhancement"); JAnnotation request = new JAnnotation(requestType); request.setElementValue("id", "2868724"); request.setElementValue("synopsis", "\"Provide time-travel functionality\""); request.setElementValue("engineer", "\"Mr. Peabody\""); request.setElementValue("date", "\"4/1/2004\"");
outputs
@RequestForEnhancement( id = 2868724, sysopsis = "Provide time-travel functionality", engineer = "Mr. Peabody", date = "4/1/2004")
"Complex" annotations are also supported via the various setValue methods that take a JAnnotation object.
JAnnotationType nameType = new JAnnotationType("Name"); JAnnotationType authorType = new JAnnotationType("Author"); JAnnotation author = new JAnnotation(authorType); JAnnotation name = new JAnnotation(nameType); name.setElementValue("first", "\"Joe\""); name.setElementValue("last", "\"Hacker\""); author.setValue(name);
outputs
@Author(@Name( first = "Joe", last = "Hacker"))
Finally annotation elements whose types are arrays are supported via the setValue methods that take arrays:
JAnnotationType endorsersType = new JAnnotationType("Endorsers"); JAnnotation endorsers = new JAnnotation(endorsersType); endorsers.setValue(new String[] { "\"Children\"", "\"Unscrupulous dentists\""});
outputs
@Endorsers( { "Children", "Unscrupulous dentists" })
Note: Conditional element values are not currently supported. However the setValue methods taking String values can be used to output this construct literally if desired.
@author
Andrew Fawcett
@version $Revision: 8009 $ $Date: 2006-04-25 16:09:10 -0600 (Tue, 25 Apr 2006) $