annotation Flashcards

1
Q

Marker annotation

A

@Inherited
@Override
@Documented

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Annotation Interface default value

A

public @interface HelloWorld {
public String sayHello() default “hello world”;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

@Retention

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

@Target

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

@Inherited

A
@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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

@Repeatable

A
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() { } }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Default annotation

A

Default annotation element value must be a nonnull constant expression.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Annotation element

A
Annotation element type must be
Primitive
String
Class
enum
another annotation
an array of any of the types
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Annotation can apply to

A
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 { ... }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

@SafeVarargs

A

requires the method to which it is applied contain a varargs param an dbe unable to overriden.
static
final
private

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Annotation elements

A

Must be public (implicit or explicitly)
Can not be marked as final. Annotation elements are implicitly abstract
Can be Array but not List

How well did you know this?
1
Not at all
2
3
4
5
Perfectly