Defining annotation types
suggest changeAnnotation types are defined with @interface. Elements are declared like methods of an interface, with no parameters of their own.
@interface MyAnnotation {
String param1();
boolean param2();
int[] param3(); // array parameter
}
Default values
@interface MyAnnotation {
String param1() default "someValue";
boolean param2() default true;
int[] param3() default {};
}
Meta-Annotations
Meta-annotations are annotations that can be applied to annotation types. The predefined meta-annotations define how an annotation type can be used.
@Target
The @Target meta-annotation restricts the types the annotation can be applied to.
@Target(ElementType.METHOD)
@interface MyAnnotation {
// this annotation can only be applied to methods
}
Multiple values can be added using array notation, e.g. @Target({ElementType.FIELD, ElementType.TYPE})
Available Values
| ElementType | Target | Example |
|---|---|---|
ANNOTATION_TYPE |
annotation types | @Retention(RetentionPolicy.RUNTIME) @interface MyAnnotation {} |
CONSTRUCTOR |
constructors | @MyAnnotation public MyClass() {} |
FIELD |
fields, enum constants | @XmlAttribute private int count; |
LOCAL_VARIABLE |
local variables | @Unused String result; |
PACKAGE |
a package, in package-info.java |
@Deprecated package very.old; |
METHOD |
methods | @XmlElement public int getCount() { return 0; } |
PARAMETER |
method or constructor parameters | public Rectangle(@NamedArg("width") double width) {} |
TYPE |
classes, interfaces, enums | @XmlRootElement public class Report {} |
TYPE_PARAMETER |
type-parameter declarations (Java 8) | public <@MyAnnotation T> void f(T t) {} |
TYPE_USE |
a use of a type (Java 8) | String s = (@MyAnnotation String) o; |
@Retention
@Retention sets how long the annotation is kept. The default is RetentionPolicy.CLASS: the annotation is stored in the .class file but is not visible to reflection at runtime. Use RetentionPolicy.RUNTIME when reflection must see it.
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation {
// this annotation can be accessed with reflections at runtime
}
Available values
| RetentionPolicy | Effect |
|---|---|
CLASS |
Stored in the .class file, not visible at runtime. This is the default. |
RUNTIME |
Available at runtime through reflection. |
SOURCE |
Visible to the compiler and to annotation processors, then discarded. Not stored in the .class file. |
@Documented
The @Documented meta-annotation is used to mark annotations whose usage should be documented by API documentation generators like javadoc. It has no values. With @Documented, all classes that use the annotation will list it on their generated documentation page. Without @Documented, it’s not possible to see which classes use the annotation in the documentation.
@Inherited
The @Inherited meta-annotation is relevant to annotations that are applied to classes. It has no values. Marking an annotation as @Inherited alters the way that annotation querying works.
- For a non-inherited annotation, the query only examines the class being examined.
- For an inherited annotation, the query will also check the super-class chain (recursively) until an instance of the annotation is found.
Note that only the super-classes are queried: any annotations attached to interfaces in the classes hierarchy will be ignored.
@Repeatable
@Repeatable was added in Java 8. It means the annotation can be applied more than once to the same target. Its value element is the class of the containing annotation, as in @Repeatable(MyAnnotations.class).