1. The most common annotation
•@Override: used on methods to tell others that this method overrides the parent class
•@Deprecated: It is recommended that others not use the old API. It will generate a warning message during compilation and can be set on all elements in the program.
•@SuppressWarnings: Temporarily turn off some warning messages
•@Entity: Indicates that this class is a persistent class
2. Design an own Annotation
Read the code first before talking
1. Annotation implementation with only one parameter
Copy the code code as follows:
package chb.test.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyAnnotation1 {
String value();
}
2. Annotation implementation with two parameters
Copy the code code as follows:
package chb.test.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyAnnotation2 {
String description();
boolean isAnnotation();
}
3. Annotation experimental class
Copy the code code as follows:
package chb.test.annotation;
@MyAnnotation1("this is annotation1")
public class AnnotationDemo {
@MyAnnotation2(description="this is annotation2",isAnnotation=true)
public void sayHello(){
System.out.println("hello world!");
}
}
4.Annotation test description class
Copy the code code as follows:
package chb.test.annotation;
import java.lang.reflect.Method;
import org.junit.Test;
public class TestAnnotation {
@Test
public void test() throws ClassNotFoundException, SecurityException, NoSuchMethodException{
Class<?> cls = Class.forName("chb.test.annotation.AnnotationDemo");
boolean flag = cls.isAnnotationPresent(MyAnnotation1.class);
if(flag){
System.out.println("Judge the class to be annotation");
MyAnnotation1 annotation1 = cls.getAnnotation(MyAnnotation1.class);
System.out.println(annotation1.value());
}
Method method = cls.getMethod("sayHello");
flag = method.isAnnotationPresent(MyAnnotation2.class);
if(flag){
System.out.println("The judgment method is also annotation");
MyAnnotation2 annotation2 = method.getAnnotation(MyAnnotation2.class);
System.out.println(annotation2.description()+"/t"+annotation2.isAnnotation());
}
}
}
As a result of the experiment, the console output the following information:
Determine whether the class is annotation
this is annotation1
The judgment method is also annotation
this is annotation2 true
3. Introduction and explanation
1. @Target(ElementType.TYPE) in MyAnnotation1
The ElementType in @Target is used to specify which elements the Annotation type can be used on. For example:
TYPE (type), FIELD (attribute), METHOD (method), PARAMETER (parameter), CONSTRUCTOR (constructor), LOCAL_VARIABLE (local variable), PACKAGE (package), where TYPE (type) refers to the type that can be used in Class ,Interface,Enum and Annotation types.
2. @Retention(RetentionPolicy.RUNTIME) in MyAnnotation1
RetentionPolicy has three strategies, namely:
•SOURCE: This Annotation type information will only be retained in the program source code. If the source code is compiled, the Annotation data will disappear and will not be retained in the compiled .class file.
•CLASS: This Annotation type information is retained in the program source code and also in the compiled .class file. During execution, this information will not be loaded into the JVM. Note: The default strategy is CLASS type
•RUNTIME: Indicates that information is retained in the source code and compiled .class files, and this information will be loaded into the JVM during execution.
3. @Documented in MyAnnotation1
The purpose is to display this Annotation information on the JAVA API document. If @Documented is not added, the relevant annotation information will not be displayed on the JAVA API document.
4. @interface in MyAnnotation1
Keyword, indicating that the class is defined for Annotation
5. String value() in MyAnnotation1;
Indicates that there is a member parameter with the name value and the access right as the default modifier. Note the following two points:
•Access rights can only be modified with public and default (default)
•Parameter members can only use the eight basic data types of byte, short, char, int, long, float, double, boolean and data types such as String, Enum, Class, annotations, and arrays of these types
6.@MyAnnotation1("this is annotation1") in AnnotationDemo
Because MyAnnotation1 has only one parameter, you can write the value directly in parentheses. Note: If the Annotation has only one parameter, it is recommended that the parameter name be defined as value
7.cls.isAnnotationPresent(MyAnnotation1.class) in TestAnnotation
Determine whether the class uses the annotation of MyAnnotation1
8. MyAnnotation1 in TestAnnotation annotation1 = cls.getAnnotation(MyAnnotation1.class)
Returns the annotation of this class for MyAnnotation1
9. method.isAnnotationPresent(MyAnnotation2.class) in TestAnnotation
Determine whether the method uses the annotation of MyAnnotation2