1. Overview
Annotations can be defined on methods. On a class, annotations are equivalent to a class, which is equivalent to instance an object. Adding annotations is equivalent to adding a flag.
Commonly used annotations:
@Override: means a method to re-parent class,
This can also determine whether the parent class method is overwritten. Add this statement before the method. If the prompt is wrong, then you are not the method of the parent class that is overwritten. If the prompt is not error, then it is the method of the parent class that is overwritten.
@SuppressWarnings("deprecation"): cancel the compiler warning (for example, the method you are using is outdated)
@Deprecated: This statement is also placed at the top of the method, indicating that this method is outdated, or used on the class
The code copy is as follows:
import java.util.ArrayList;
import java.util.List;
public class annotationDemo {
/*
* For collections, if no storage type is specified, there will be a security warning.
* If you don't want to prompt a security warning, then add @SuppressWarnings (parameter) to the class or method.
*/
@SuppressWarnings("unchecked")
public static void main(String[] args) {
List list=new ArrayList();
}
}
2. Custom annotations
1. Format
Permissions @interface annotation name { }
step:
Define annotation class --->Define the class of the application annotation class --->Class that reflect the class of the application annotation class (this class can be defined separately or tested in the application annotation class)
The code copy is as follows:
import java.lang.annotation.Retention;
importjava.lang.annotation.RetentionPolicy;
//Define this annotation to be kept in bytecode
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
}
@MyAnnotation
// Application-defined annotation class
public class ApplyMyAnnotation {
public static void main(String[] args) {
if (ApplyMyAnnotation.class.isAnnotationPresent(MyAnnotation.class)) {// Determine whether there is a specified annotation class on this class
MyAnnotation annotation= (MyAnnotation) ApplyMyAnnotation.class
.getAnnotation(MyAnnotation.class);
System.out.println(annotation);
}
}
}
2. Declaration cycle
Format: For example: @Retention(RetentionPolicy.CLASS)
Define cycles on a custom annotation class, @Retention (parameter type) parameter type is RetentionPolicy
RetentionPolicy.CLASS: On the class file, the virtual machine does not retain the annotations during runtime
RetentionPolicy.RUNTIME: On the class file, the virtual comments are retained during runtime.
RetentionPolicy.SOURCE: On the source file, discard the annotation
SuppressWarnings and Override are RetentionPolicy.SOURCE,
Deprecated is in RetentionPolicy.RUNTIME. To be defined as the runtime call, it must be RetentionPolicy.RUNTIME.
The default is RetentionPolicy.CLASS:
3. Specify the target
Format: For example: method @Target(ElementType.METHOD)
What members can be annotated by the defined annotation. If this annotation is not declared, it can be placed on any program element.
It can be packages, interfaces, parameters, methods, local variables, fields, etc.
The code copy is as follows:
//Define this annotation to be kept in bytecode
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE})//It can be defined on methods and classes to represent types
public @interface MyAnnotation {
}
@MyAnnotation
// Application-defined annotation class
public class ApplyMyAnnotation {
@MyAnnotation//Defined on the method
public static void main(String[] args) {
if (ApplyMyAnnotation.class.isAnnotationPresent(MyAnnotation.class)) {// Determine whether there is a specified annotation class on this class
MyAnnotation annotation = (MyAnnotation) ApplyMyAnnotation.class
.getAnnotation(MyAnnotation.class);
System.out.println(annotation);
}
}
}
3. Add attributes to the annotation
1. Type
The attribute settings of annotations can be: 8 basic data types, String, enumeration, annotation, Class, array types,
2. Pay attention
If there is only one attribute in the annotation or only one attribute needs to be assigned, then it can be written directly when called without specifying the attribute name.
When the annotated attribute is an array type and only one value is assigned when assigning, {} can be omitted.
3. Example
3.1.Attribute type (is String)
The code copy is as follows:
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.*;
//Define this annotation to be kept in bytecode
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String value() ;
String Color() default "red";//Set the default value is "red"
}
@MyAnnotation("java")
public class ApplyMyAnnotation {
public static void main(String[] args) {
/**
* This is to obtain the annotation on the class, and you can also obtain the annotation on the method. Let’s take the annotation on the class as an example.
*/
if (ApplyMyAnnotation.class.isAnnotationPresent(MyAnnotation.class)) {// Determine whether there is a specified annotation class on this class
MyAnnotation annotation = (MyAnnotation) ApplyMyAnnotation.class
.getAnnotation(MyAnnotation.class);
System.out.println("value="+annotation.value());
System.out.println("Color="+annotation.Color());
}
}
}
result:
value=java
Color=red
From the calling program, it can also be seen that if only one attribute can be assigned, the attribute name can be omitted. Otherwise @ annotation class (attribute name = value)
3.2. Comprehensive Type
The code copy is as follows:
/*Enum Class*/
public enum Week{
SUN,MON;
}
/**
* Annotation class
*/
public @interface annotationText {
String value();
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.*;
//Define this annotation to be kept in bytecode
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String value() ;
String Color() default "red";//Set the default value is "red"
Week week() default Week.MON;//Enum type
int [] array() default {1,2,3};//array type
annotationText annotation() default @annotationText("MY"); //Annotation type
Class classDemo() default Integer.class;//Class type
}
@MyAnnotation(value="java",Color="green",week=Week.SUN,array=5,annotation=@annotationText("YOU"), classDemo=String.class)//array={4,5 ,6}
public class ApplyMyAnnotation {
public static void main(String[] args) {
/**
* This is to obtain the annotation on the class, and you can also obtain the annotation on the method. Let’s take the annotation on the class as an example.
*/
if (ApplyMyAnnotation.class.isAnnotationPresent(MyAnnotation.class)) {// Determine whether there is a specified annotation class on this class
MyAnnotation annotation= (MyAnnotation) ApplyMyAnnotation.class
.getAnnotation(MyAnnotation.class);
System.out.println("value="+annotation.value());
System.out.println("Color="+annotation.Color());
System.out.println("week="+annotation.week());
System.out.println("array length="+annotation.array().length);
System.out.println("annotation type value="+annotation.annotation().value());
System.out.println("Class type value="+annotation.classDemo());
}
}
}
result:
The code copy is as follows:
value=java
Color=green
week=SUN
array length = 1
Annotation type value = YOU
Class type value = classjava.lang.String
4. Annotations on Method
The code copy is as follows:
importjava.lang.annotation.Retention;
importjava.lang.annotation.RetentionPolicy;
/**
*Annotation class
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface annotationText{
Stringvalue();
}
publicclassApplyMyAnnotation{
publicstaticvoidmain(String[]args) throwsException{
Methodmethodshow=ApplyMyAnnotation.class.getMethod("show");
annotationTextanno=methodshow.getAnnotation(annotationText.class);
System.out.println(anno.value());
}
@annotationText("java")
publicvoidshow(){
System.out.println("hello");
}
}
Results: java