org.exolab.javasource

Class JAnnotation

public class JAnnotation extends Object

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 Specificaiton Third Edition) based on the methods called. For "Marker Annotation", construct with the approprite JAnnotationType. JAnnotationType preliminaryType = new JAnnotationType("Preliminary"); JAnnotation preliminary = new JAnnotation(preliminaryType); Outputs

Author: Andrew Fawcett

UNKNOWN: 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 Propulsion Systems, Inc., All rights reserved.\""); Outputs Yoyodyne Propulsion Systems, Inc., All rights reserved.") For "Normal Annotation", construct as above then call the approprite setValue methods that accept an "elementName" parameter. JAnnotationType requestForEnhancementType = new JAnnotationType("RequestForEnhancement"); JAnnotation requestForEnhancement = new JAnnotation(requestForEnhancementType); requestForEnhancement.setElementValue("id", "2868724"); requestForEnhancement.setElementValue("sysopsis", "\"Provide time-travel functionality\""); requestForEnhancement.setElementValue("enginer", "\"Mr. Peabody\""); requestForEnhancement.setElementValue("date", "\"4/1/2004\""); Outputs id = 2868724, sysopsis = "Provide time-travel functionality", enginer = "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 first = "Joe", last = "Hacker")) Finally annotation elements who's 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 { "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.

Field Summary
static Stringvalue
Name of a single element
Constructor Summary
JAnnotation(JAnnotationType annotationType)
Constructs a JAnnotation for the given annotation type
Method Summary
JAnnotationTypegetAnnotationType()
Returns the JAnnotationType associated with this JAnnotation
String[]getElementNames()
Returns the names of the elements set by this annotation
StringgetElementValue(String elementName)
Returns the given annotation element value
JAnnotationgetElementValueAnnotation(String elementName)
Returns the given annotation element value as a JAnnotation
JAnnotation[]getElementValueAnnotationList(String elementName)
Returns the given annotation element value as a JAnnotation list
String[]getElementValueList(String elementName)
Returns the given annotation element value as a list
ObjectgetElementValueObject(String elementName)
Returns the given annotation element value as Object, typically used if the value type is not known.
StringgetValue()
Returns the "value" annotation element value
JAnnotationgetValueAnnotation()
Returns the "value" annotation element value as an JAnnotation
static voidmain(String[] args)
Test
voidprint(JSourceWriter jsw)
Prints the source code for this JAnnotation to the given JSourceWriter
voidsetElementValue(String elementName, String stringValue)
Sets the given annotation element value
voidsetElementValue(String elementName, String[] stringValues)
Sets the given annotation element value as a list
voidsetElementValue(String elementName, JAnnotation annotationValue)
Sets the given annotation element value as an annotation
voidsetElementValue(String elementName, JAnnotation[] annotationValues)
Sets the given annotation element values as a list of annotation values
voidsetValue(String stringValue)
Sets the "value" annotation element value
voidsetValue(String[] stringValue)
Sets the "value" annotation element value as a list
voidsetValue(JAnnotation annotationValue)
Sets the "value" annotation element value as an annotation
voidsetValue(JAnnotation[] annotationValues)
Sets the "value" annotation element value as a list of annotation values

Field Detail

value

public static final String value
Name of a single element

Constructor Detail

JAnnotation

public JAnnotation(JAnnotationType annotationType)
Constructs a JAnnotation for the given annotation type

Parameters: annotationType

Method Detail

getAnnotationType

public JAnnotationType getAnnotationType()
Returns the JAnnotationType associated with this JAnnotation

getElementNames

public String[] getElementNames()
Returns the names of the elements set by this annotation

Returns: array of element names

getElementValue

public String getElementValue(String elementName)
Returns the given annotation element value

Parameters: elementName

Returns:

Throws: IllegalArgumentException when the element value is not a String

getElementValueAnnotation

public JAnnotation getElementValueAnnotation(String elementName)
Returns the given annotation element value as a JAnnotation

Parameters: elementName

Returns: annotation element value as a JAnnotation

Throws: IllegalArgumentException when the element value is not a JAnnotation

getElementValueAnnotationList

public JAnnotation[] getElementValueAnnotationList(String elementName)
Returns the given annotation element value as a JAnnotation list

Parameters: elementName

Returns:

Throws: IllegalArgumentException when the element value is not a JAnnotation[]

getElementValueList

public String[] getElementValueList(String elementName)
Returns the given annotation element value as a list

Parameters: elementName

Returns:

Throws: IllegalArgumentException when the element value is not a String[]

getElementValueObject

public Object getElementValueObject(String elementName)
Returns the given annotation element value as Object, typically used if the value type is not known. This will either be a String or JAnnotation as single value or as an array.

Parameters: elementName

Returns: annotation element value as Object

getValue

public String getValue()
Returns the "value" annotation element value

Returns:

Throws: IllegalStateException when the element value is not a String

getValueAnnotation

public JAnnotation getValueAnnotation()
Returns the "value" annotation element value as an JAnnotation

Returns:

Throws: IllegalStateException when the element value is not a JAnnotation

main

public static void main(String[] args)
Test

Parameters: args

print

public void print(JSourceWriter jsw)
Prints the source code for this JAnnotation to the given JSourceWriter

Parameters: jsw the JSourceWriter to print to. [May not be null]

setElementValue

public void setElementValue(String elementName, String stringValue)
Sets the given annotation element value

Parameters: elementName stringValue

setElementValue

public void setElementValue(String elementName, String[] stringValues)
Sets the given annotation element value as a list

Parameters: elementName stringValues

setElementValue

public void setElementValue(String elementName, JAnnotation annotationValue)
Sets the given annotation element value as an annotation

Parameters: elementName annotationValue

setElementValue

public void setElementValue(String elementName, JAnnotation[] annotationValues)
Sets the given annotation element values as a list of annotation values

Parameters: elementName annotationValues

setValue

public void setValue(String stringValue)
Sets the "value" annotation element value

Parameters: stringValue

setValue

public void setValue(String[] stringValue)
Sets the "value" annotation element value as a list

Parameters: stringValue

setValue

public void setValue(JAnnotation annotationValue)
Sets the "value" annotation element value as an annotation

Parameters: annotationValue

setValue

public void setValue(JAnnotation[] annotationValues)
Sets the "value" annotation element value as a list of annotation values

Parameters: annotationValues

Intalio Inc. (C) 1999-2004. All rights reserved http://www.intalio.com