Table of contents
annotations
declaring an annotation
@interface ClassPreamble {
String author();
String date();
int currentRevision() default 1;
String lastModified() default "N/A";
String lastModifiedBy() default "N/A";
String[] reviewers();
}
annotation annotations
annotation can be inherited from its superclass
@Inherited
meta annotation that includes annotated annotation in the javadoc
@Documented
target
specify which java elements may have the annotation
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Menu {
String text();
}
target types
ElementType.ANNOTATION_TYPE annotation type
ElementType.CONSTRUCTOR constructor
ElementType.FIELD field or property
ElementType.LOCAL_VARIABLE local variable
ElementType.METHOD method-level annotation
ElementType.PACKAGE package declaration
ElementType.PARAMETER parameters of a method
ElementType.TYPE element of a class
retention
setting the retention policy of an annotation
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface Immutable {
}
available retention policies
RetentionPolicy.SOURCE only in source; ignored by the compiler
RetentionPolicy.CLASS retained by the compiler at compile time, but ignored by the JVM
RetentionPolicy.RUNTIME retained by the JVM; can be used by the runtime environment
repeatable
annotation can be repeated
/* container annotation for backwards compatibility */
public @interface Imports {
Import[] value();
}
@Repeatable(Imports.class)
public @interface Import {
String value();
}