annotation Flashcards
Marker annotation
@Inherited
@Override
@Documented
Annotation Interface default value
public @interface HelloWorld {
public String sayHello() default “hello world”;
}
@Retention
RetentionPolicy.SOURCE – The marked annotation is retained only in the source level and is ignored by the compiler.
RetentionPolicy.CLASS – The marked annotation is retained by the compiler at compile time, but is ignored by the Java Virtual Machine (JVM).
RetentionPolicy.RUNTIME – The marked annotation is retained by the JVM so it can be used by the runtime environment.
@Target
ElementType.ANNOTATION_TYPE can be applied to an annotation type.
ElementType.CONSTRUCTOR can be applied to a constructor.
ElementType.FIELD can be applied to a field or property.
ElementType.LOCAL_VARIABLE can be applied to a local variable.
ElementType.METHOD can be applied to a method-level annotation.
ElementType.PACKAGE can be applied to a package declaration.
ElementType.PARAMETER can be applied to the parameters of a method.
ElementType.TYPE can be applied to any element of a class.
@Inherited
@Inherited annotation indicates that the annotation type can be inherited from the super class. When the user queries the annotation type and the class has no annotation for this type, the class' superclass is queried for the annotation type. This annotation applies only to class declarations.
@Repeatable
Step 1: Declare a Repeatable Annotation Type @Repeatable(Schedules.class) public @interface Schedule { String dayOfMonth() default "first"; String dayOfWeek() default "Mon"; int hour() default 12; }
Step 2: Declare the Containing Annotation Type
The containing annotation type must have a value element with an array type. The component type of the array type must be the repeatable annotation type. The declaration for the Schedules containing annotation type is the following:
public @interface Schedules {
Schedule[] value();
}
@Schedule(dayOfMonth=”last”)
@Schedule(dayOfWeek=”Fri”, hour=”23”)
public void doPeriodicCleanup() { … }
Another example
public class AnnotationTest {
public @interface MealContainer { Meal[] value(); }
@java.lang.annotation.Repeatable(MealContainer.class) public @interface Meal { String value(); String mainDish(); }
@Meal(value="breakfast", mainDish="cereal") @Meal(value="lunch", mainDish="pizza") @Meal(value="dinner", mainDish="salad") public void evaluateDiet() { } }
Default annotation
Default annotation element value must be a nonnull constant expression.
Annotation element
Annotation element type must be Primitive String Class enum another annotation an array of any of the types
Annotation can apply to
declarations of classes, fields, methods, and other program elements. lambda other annotation When used on a declaration, each annotation often appears, by convention, on its own line.
Class instance creation expression:
new @Interned MyObject();
Type cast:
myString = (@NonNull String) str;
implements clause: class UnmodifiableList implements @Readonly List { ... }
Thrown exception declaration: void monitorTemperature() throws @Critical TemperatureException { ... }
@SafeVarargs
requires the method to which it is applied contain a varargs param an dbe unable to overriden.
static
final
private
Annotation elements
Must be public (implicit or explicitly)
Can not be marked as final. Annotation elements are implicitly abstract
Can be Array but not List