Annotations are only available in JDK 5.0, but using our annotation compiler you can acheive similar functionality with JDK 1.4.2 as well.
Annotations must map to an annotation type, in JDK 5.0 they are defined as:
package com.mypackage;
public @interface MyAnnotation
{
String myString();
int myInteger();
}
Annotation types for use with the annotation compiler are defined in exactly the same way for JDK 1.4.2, with the important difference that '@interface' is replaced by 'interface'. i.e. the similar annotation type is a normal Java interface:
package com.mypackage;
public interface MyAnnotation
{
String myString();
int myInteger();
}
The syntax for using annotations in JDK 1.4.2 is almost exactly the same as JDK 5.0 annotations except for these subtle differences:
they are embedded as doclet tags
You use a double at sign, i.e. '@@'
You MUST have a space after the tag name otherwise you will get a compilation error. (This is the quirkiness of the QDox doclet compiler used to compile the annotations.')
You cannot import the annotation type, you must use the fully qualified name of the interface.
You cannot specify default values for an annotation's value
This example shows an annotated class in JDK 1.4.2:
package com.mypackage;
/**
* @@com.mypackage.MyAnnotation (myString="class", myInteger=5)
*/
public class MyClass
{
/**
* @@com.mypackage.MyAnnotation (myString="field", myInteger=4)
*/
private String myField;
/**
* @@com.mypackage.MyAnnotation (myString="constructor", myInteger=3)
*/
public MyClass()
{
}
/**
* @@com.mypackage.MyAnnotation (myString="method", myInteger=3)
*/
public int myMethod()
{
}
}