- May apply to:
- packages,
- types,
- variables,
- constructors,
- methods and parameters
- type case: myString = (@NotNull String) str;
- implements: class UnModifiableList<T> implements @Readonly List<@Readonly T>
- throws declaration: void monitorTemperature() throws @Critical TemperatureException {...}
- NOT on object / instance
- Elements
- optional name / value pairs
- value can be primitives, strings, enums and arrays of them
- may define default values for some or all
Annotation types are special interfaces
public @interface Author { String name(); String created(); int revision() default 1; String[] reviewers() default {}; } public @interface Complexity { ComplexityLevel value() default ComplexityLevel.MEDIUM; } public enum ComplexityLevel { VERY_SIMPLE, SIMPLE, MEDIUM, COMPLEX, VERY_COMPLEX; }
// nested enum
public @interface Complexity { public enum Level { VERY_SIMPLE, SIMPLE, MEDIUM, COMPLEX, VERY_COMPLEX; }
Annotation that applies to other annotations
- @Retention (RetentionPolicy.SOURCE/CLASS/RUNTIME)
- @Documented (by default annotations are not included in Javadoc)
- @Target(ElementType.ANNOTATION_TYPE/CONSTRUCTOR/FIELD/LOCAL/METHOD/PACKAGE/PARAMETER/TYPE)
- @Inherited: annotation can be inherited from super class (not true by default)
- @Repeatable
Type annotation: annotatedObject.getClass().getAnnotation(AnnotationType.class)