34 _ annotation definition and reflection call, 34 annotation reflection call
Custom annotations and Their Applications
- Define a simple annotation: public @ interface MyAnnotation {}
Package com. itcast. day2; import java. lang. annotation. elementType; import java. lang. annotation. retention; import java. lang. annotation. retentionPolicy; import java. lang. annotation. target; // The default Retention is the class stage, and the life cycle of the annotation MyAnnotation is the run stage @ Retention (RetentionPolicy. RUNTIME) // The default value is any element. If only the METHOD is entered in the array, an error is returned when the annotation class is used for compiling @ Target ({ElementType. METHOD, ElementType. TYPE}) public @ interface MyAnnotation {}/*** constraint ention * three phases of the java program * source phase :. java ---> compile --->. class * class stage :. class --> enter the jvm check stage ---> bytecode * runtime stage: security check passed-transferred to memory, considered as bytecode **/
- Add it to a class: @ MyAnnotation public class MyAnnotationTest {}
package com.itcast.day2;@MyAnnotationpublic class MyAnnotationTest {}
- Use reflection to test whether @ MyAnnotation exists in the definition of AnnotationTest
package com.itcast.day2;public class MyAnnotationTestRun { public static void main(String[] args) { if(MyAnnotationTest.class.isAnnotationPresent(MyAnnotation.class)){ MyAnnotation MyAnnotation=MyAnnotationTest.class.getAnnotation(MyAnnotation.class); System.out.println(MyAnnotation);//com.itcast.day2.MyAnnotation() } }}
- According to the reflection test, the @ resolve ention meta annotation is introduced. The three values are as follows:
Entionpolicy. SOURCE, entionpolicy. CLASS, and entionpolicy. RUNTIME;
Corresponding to java source code> class File> bytecode in memory
- Thinking: What are the attribute values of @ Override, @ SuppressWamings, and @ Deprecated annotations?
@ Override: used to show the compiler whether the rewrite rules are met (method name, parameter list, and return value must be the same ). It is generally self-written code. ------ SOURCE stage
@ SuppressWarnings is used to tell the compiler not to issue a warning during the compilation phase. It is generally self-written code. --- SOURCE stage
@ Deprecated indicates whether the method is outdated. It must be scanning binary data at runtime after memory is transferred. It may be something someone else wrote that I am calling it. --- RUNTIME stage
- Demonstration and explanation @ Target meta Annotation
- The default value of Target is any element, and the value of Target is set to ElementType. METHOD. If an error is reported for the annotation added to the class, set {ElementType in array mode. METHOD, ElementType. TYPE.
- You do not need to remember the meta annotation and Its enumerated attribute values. You only need to refer to the definition of the API help document provided by jdk or its source code, as shown in the following figure.Java. lang. annotationClass under the package.