Annotations Flashcards
Retence anotace - syntax, druhy (3)
@Retention(RetentionPolicy.RUNTIME)
source
class
runtime
Target anotace - syntax, druhy (11)
@Target(ElementType.METHOD, ElementType.FIELD)
annotation_type
field, local_variable, parameter
method, constructor
package, module
type, type_parameter, type_use
Dedicnost anotaci
Nededi se, ale lze vynutit pomoci @Inherited
Anotace v javadocu
@Documented - vypise se do javadocu
Anotace, ktera se muze opakovat - definice, pouziti
@Repeatable(Ann1.class)
public @interface Ann2 { … }
@Ann1({@Ann2, @Ann2})
Elementy anotace - jake muzou byt druhy? (5)
primitive, String, class, enum
array cehokoli z toho
Zapis anotace s retention, target, elementem s a bez hodnoty, array elementem
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.FIELD)
public @interface MyAnnotation {
int field1() default 0;
int field2();
int[] field3;
}
Pouziti anotace - zapis hodnot array (1), zapis pro anotaci s jedinym elementem (2)
@Ann(element = {“a”, “b”})
@Ann(element = value)
@Ann(value)
Deprecation: anotace, javadoc anotace
@Deprecated(since=”11”, forRemoval=true)
@deprecated
Potlaceni syntax warningu anotaci
castovani
pouziti deprecated metody
pretypovani varargs (heap pollution)
@SuppressWarnings(“unchecked”, “deprecation”)
@SafeVarargs + metoda bud private nebo final
Reflection - ktere anotace nejdou reflexi zjistit?
anotace tridy (2)
anotace - ziskej typ a elementy
source
class.getAnnotations(), getAnnotationsByType(Ann.class)
a.getAnnotationType()
a.jmenoElementu()