Sub topic 9
Meta-annotations: Retention, Target, Inherited, Repeatable
This chapter assumes you can declare a simple annotation with @interface and apply it to a class or field. A meta-annotation is an annotation placed on an annotation declaration. It controls how long the annotation lives, where it may be used, and how it behaves with inheritance and repetition.
Retention: how long the annotation survives
@Retention takes a RetentionPolicy value:
| Policy | Kept in source | Kept in class file | Visible to reflection |
|---|---|---|---|
| `SOURCE` | yes | no | no |
| `CLASS` (default) | yes | yes | no |
| `RUNTIME` | yes | yes | yes |
The default is CLASS, so an annotation without @Retention is invisible to reflection. The usual symptom is that getAnnotation returns null even though the annotation is plainly on the element. Any annotation you plan to read at runtime needs @Retention(RetentionPolicy.RUNTIME).
Target: where it may be applied
@Target lists the allowed ElementType values, such as TYPE, FIELD, METHOD, PARAMETER, and CONSTRUCTOR. The compiler rejects any use outside that list. Without @Target, the annotation can be applied to most declarations, which is rarely what you want. Name the targets explicitly.
Inherited: passing the annotation to subclasses
@Inherited makes a class annotation visible on subclasses through getAnnotation and isAnnotationPresent. It has limits:
- It works only on classes. Annotations on interfaces, methods, and fields are never inherited this way.
- It affects only lookups through
Class. A subclass's own methods do not pick up annotations from overridden methods.
Repeatable: using an annotation more than once
Applying the same annotation twice is an error unless the annotation is @Repeatable. You must also declare a container annotation with a value() array of the repeatable type. The container's retention and targets must be at least as wide as the repeatable annotation's.
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Repeatable(Roles.class)
@interface Role {
String value();
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface Roles {
Role[] value();
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Inherited
@interface Audited {
}
@Audited
@Role("admin")
@Role("editor")
class Base {
}
class Child extends Base {
}
public class MetaDemo {
public static void main(String[] args) {
System.out.println(Child.class.isAnnotationPresent(Audited.class));
System.out.println(Base.class.getAnnotation(Role.class));
System.out.println(Base.class.getAnnotationsByType(Role.class).length);
}
}The program prints true, null, and 2:
Childsees@Auditedbecause that annotation is@Inherited.getAnnotation(Role.class)returnsnullonBase. The compiler wrapped the two@Roleuses in a hidden@Rolescontainer, so no@Roleis directly present.getAnnotationsByTypelooks through the container and returns both.
Exercise
A custom @Timed annotation is placed on a method, but method.getAnnotation(Timed.class) returns null. The declaration has @Target(ElementType.METHOD) and no @Retention. What is wrong, and what is the fix?
Show answer
Without @Retention, the policy defaults to CLASS, so the annotation is stored in the class file but not available to reflection at runtime. Add @Retention(RetentionPolicy.RUNTIME) to the @Timed declaration.