catalogue
3. User defined annotation and reflection implementation function
I Annotation overview
1. What is annotation?
Definition: Annotation, also known as metadata. A code level description. It is jdk1 A feature introduced in version 5 and later is at the same level as class, interface and enumeration. It can be declared in front of packages, classes, fields, methods, local variables, method parameters, etc. to describe and annotate these elements.
2. Role of annotations
- Writing documents: generate documents through the annotations identified in the code [generate doc documents]
- Code analysis: analyze the code through the annotation marked in the code [use reflection]
- Compilation check: enable the compiler to implement basic compilation check through the annotations identified in the code [Override]
3.JDK predefined annotations
@Override: check whether the annotation method inherits from the parent class interface
@Deprecated: this annotation indicates that the content of the annotation is obsolete
@SuppressWarnings: suppress warnings
II Custom annotation
Format of annotation:
//Meta annotation public @interface Annotation name { //Attribute list }
1. Attribute list
Essence: the essence of annotation is an interface;
@After the interface is decompiled by javap, it is interface extensions # Java lang.annotation. Annotation
Therefore, the @ interface declaration represents that an interface inherits Java lang.annotation. Annotation
Attributes: abstract methods defined in annotations
The returned result must be of the following types
1. Basic data type
2.string type 3 Enumeration type
4. Notes
5. Arrays of the above types
Notes on attribute assignment:
1. If the default keyword is used to give the default initial value to the attribute when defining the attribute, the annotation can be used without assignment
2. If only one attribute needs to be assigned and the name of the attribute is value, value can be omitted during assignment
3. During array assignment, if the attribute is an array and there is only one value, the braces can be omitted
2. Meta annotation
Four meta annotations provided in JDK
1.@Target: describes where the current annotation can work
ElementType.TYPE: can act on a class
ElementType.METHOD: can act on methods
ElementType.FIELD: can act on member variables
2.@Retention: describes the stage to which annotations are retained
SOURCE< CLASS < RUNTIME
SOURCE: indicates that the current annotation is only valid in the code phase
CLASS: indicates that the annotation will be retained to bytecode stage
RUNTIME: indicates that the annotation will be retained to the RUNTIME JVM
Customized annotation: retentionpolicy RUNTIME
3.@Documented: describes whether annotations are extracted into the avaDoc api
4.@inherited: describes whether annotations can be inherited by subclasses
3. User defined annotation and reflection implementation function
Functions to be realized: the annotation has two attributes, one represents the class name, the other represents the method name, and the other implements the method in this class
Custom annotation:
/** * Custom annotation * Give a class name a method name to implement this method in this class */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface MyAnno { //Class name String className(); //Method name String methodName(); }
Custom class:
public class Demo1 { public void sayHello() { System.out.println("hello~~~~"); } }
Annotation and reflection implementation:
@MyAnno(className = "com.anno.Demo1",methodName = "sayHello") public class DemoTest { public static void main(String[] args) throws Exception { //Gets the class object of the current class Class<DemoTest> demoTestClass = DemoTest.class; //Get annotation /** * Annotation is essentially an interface * getAnnotation()Equivalent to getting our annotation implementation class * public anno implements MyAnno { * //It is equivalent to implementing a method to return the properties in our annotation * String className() { * return className; * } * String methodName() { * return menthodName; * } * } */ MyAnno anno = demoTestClass.getAnnotation(MyAnno.class); //Get full class name String className = anno.className(); //Get method name String methodName = anno.methodName(); // System.out.println(className); // System.out.println(methodName); //Realize the function we want to achieve through reflection Class<?> clazz = Class.forName(className); Method method = clazz.getDeclaredMethod(methodName); //Execute the corresponding method method.invoke(clazz.newInstance()); } }
result:
hello~~~~