@Override, @Deprecated, @SuppressWarnings are three common annotations.
@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface NativeQuery { String value() default ""; boolean pagable() default false; boolean withGroupby() default false; Class<?> model() default DEFAULT.class; Class<? extends ColumnTranslator> translator() default DummyColumnTranslator.class; String[] alias() default {}; Class<?>[] clazzes() default {}; public static final class DEFAULT {} }
The annotation @Retention can be used to modify annotations, which are annotations of annotations, called meta-annotations.
The Retention annotation has an attribute value, which is of type RetentionPolicy. Enum RetentionPolicy is an enumeration type.
This enumeration determines how the Retention annotation should be maintained. It can also be understood as the use of Retention with RetentionPolicy. RetentionPolicy has 3 values: CLASS RUNTIME SOURCE
Annotation modified with @Retention(RetentionPolicy.CLASS), indicating that the annotation information is retained in the class file (bytecode file) when the program is compiled, but will not be read by the virtual machine At runtime;
annotations modified with @Retention(RetentionPolicy.SOURCE ) indicate that the annotation information will be discarded by the compiler and will not remain in the class file, and the annotation information will only remain in the source file;
use @Retention( RetentionPolicy.RUNTIME ) modified annotations, indicating that the annotation information is retained in the class file (bytecode file) when the program is compiled, it will be retained by the virtual machine at runtime,
so they can be read in a reflective way. RetentionPolicy.RUNTIME allows you to read the information of the annotation N annotation from the JVM so that it can be used when analyzing the program. The
annotation @Target is also a meta-annotation used to modify the annotation. It has an attribute ElementType which is also an enumeration type with the
value: ANNOTATION_TYPE CONSTRUCTOR FIELD LOCAL_VARIABLE METHOD PACKAGE PARAMETER TYPE
An annotation modified by @Target(ElementType.METHOD) means that the annotation can only be used to modify the method.
@Target: Specifies where the annotations defined by program elements are used. It uses another class: ElementType, which is an enumeration class that defines annotation types that can be applied to different program elements to prevent users from misusing them. Look at java.lang.annotation
ElementType is an enumeration type that indicates where annotations can be used, look at the ElementType class:
public enum ElementType {
TYPE, // Specify the applicable point as class, interface, enum
FIELD, // Specify the applicable point For field
METHOD, // specify the applicable point as method
PARAMETER, // specify the applicable point as the method's parameter
CONSTRUCTOR, // specify the applicable point as constructor
LOCAL_VARIABLE, // specify the use point as the local variable
ANNOTATION_TYPE, // specify the applicable point as annotation Type
PACKAGE // Specify the applicable point as package
}
/** * Created by hailong on 2016/11/21. */ @Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotation { String hello() default "gege"; String world(); int[] array() default { 2, 4, 5, 6 }; Class style() default String.class; }
@MyAnnotation(hello = "beijing", world="shanghai",array={},style=int.class) public class MyTest { @MyAnnotation(world = "shanghai",array={1,2,3}) @Deprecated @SuppressWarnings("") public void output() { System.out.println("output something!"); } }
public class MyReflection { public static void main(String[] args) throws Exception { MyTest myTest = new MyTest(); Class<MyTest> myTestClass = MyTest.class; // method Method method = myTestClass.getMethod("output", new Class[] {}); // true if there is an annotation @MyAnnotation on the MyTest class name // kind if(MyTest.class.isAnnotationPresent(MyAnnotation.class)) { System.out.println("have annotation"); } if (!method.isAnnotationPresent(MyAnnotation.class)) { } else { method.invoke(myTest, null); //Call the output method //Get the information of the annotation @MyAnnotation on the method MyAnnotation myAnnotation = method.getAnnotation(MyAnnotation.class); String hello = myAnnotation.hello(); String world = myAnnotation.world(); System.out.println(hello + ", " + world);//Print the values of attributes hello and world System.out.println(myAnnotation.array().length);//Print the length of the attribute array array System.out.println(myAnnotation.style()); } //Get all the annotations on the output method, of course, are modified by RetentionPolicy.RUNTIME Annotation[] annotations = method.getAnnotations(); for (Annotation annotation : annotations) { System.out.println(annotation.annotationType().getName()); } } }
After setting the value of the class attribute, there will be a class in the framework that uses the reflection mechanism to set the specific method implementation.