1. Direct introduction
annotation is a reference data type, which generates XXX after compilation Class bytecode file,
2. Definition notes
[modifier list] @ interface annotation type name {}
//Custom annotation @interface annotation{ //name is the attribute of the annotation String name(); }
The modifier list can be omitted
3. How to use annotations?
*Use format: @ annotation type name *It can appear on classes, properties, methods and variables.
★★★★★ when using annotation, the attribute in the annotation must be assigned!!!
@annotation(value = "annotation") public class Test9 { @annotation("annotation") public static void main(String[] args) { @annotation(value = "annotation") int i = 0 ; } } //Custom annotations @interface annotation{ //value is the attribute of the annotation String value(); }
★★★★★★ when the attribute in the annotation has only value, the attribute name can be omitted. For other attributes, the attribute name cannot be omitted.
@annotation(value = "value" ,name = "name") public class Test9 { @annotation(value = "value" ,name = "name") public static void main(String[] args) { @annotation(value = "value" ,name = "name") int i = 0 ; } } @interface annotation{ //name is the attribute of the annotation String value(); String name(); }
★★★★★★ the attribute can also be an array. When the attribute name is value and there is only one, the array name can be omitted. And {} can be omitted when there is only one element
@annotation(value = {"value","value"}) public class Test9 { @annotation({"value","value"}) public static void main(String[] args) { @annotation("value" ) int i = 0 ; } } @interface annotation{ String[] value(); }
★★★★★ enumeration can also be used as an attribute
@annotation(value = {"value","value"},enum1 = {ENUM.SPRING,ENUM.WINTER}) public class Test9 { public static void main(String[] args) { int i = 0 ; } } //Custom annotation @interface annotation{ String[] value(); ENUM[] enum1() ; } //Enumeration type enum ENUM { SPRING ,WINTER ; }
4.JDK built-in annotation
*Annotation type Override: must be a method that overrides the parent class It works in the compile phase, independent of the run phase. *Comment type Deprecated: indicates that it is obsolete *Note type SuppressWarnings
public class Father{ public static void main(String[] args) { //Indicates that this variable is obsolete but accessible @Deprecated int i = 255 ; System.out.println(i); } public void doSome(String s){} } class son extends Father{ @Override //Method rewrite public void doSome(String s){} }
5. Meta annotation
The annotation of an annotation becomes a meta annotation *Common meta annotation: target retention *Target: the annotation used to mark the annotation, indicating where the "marked Annotation" can appear There is an ElementType [] array in the Target interface, which stores the following data: [only list some commonly used] TYPE: indicates that the annotation can only appear on the class Field: indicates that the annotation can only appear on the field attribute METHOD: indicates that the annotation can only appear on the METHOD CONSTRUCTOR: indicates that the annotation can only appear on the CONSTRUCTOR ANNOTATION_TYPE: indicates that the annotation can only appear on the annotation *Retention: indicates where the annotated annotation is finally saved RUNTIME: indicates that the annotation can be reflected by the reflection mechanism SOURCE: indicates that the annotation cannot be reflected by the reflection mechanism
We can see that the annotation annotation can only be annotated on classes and methods. When it is annotated on variables, it will report an error