Create Annotation instance with defaults, in Java

To create an instance you need to create a class that implements:

  • java.lang.annotation.Annotation
  • and the annotation you want to “simulate”

For example:
public class MySettings implements Annotation, Settings

But you need to pay special attention to the correct implementation of equals and hashCode according to the Annotation interface.
http://download.oracle.com/javase/1.5.0/docs/api/java/lang/annotation/Annotation.html

If you do not want to implement this again and again then have a look at the javax.enterprise.util.AnnotationLiteral class.
That is part of the CDI(Context Dependency Injection)-API.
(@see code)

To get the default values you can use the way that is described by akuhn (former known as: Adrian).
Settings.class.getMethod("a").getDefaultValue()

Leave a Comment

tech