Java custom annotation and java Annotation
To learn about annotations in depth, we must be able to define our own annotations and use annotations. Before defining our own annotations, we must understand the syntax of the Meta annotation and related definition annotation provided by Java for us.
1 package annotation; 2 3 import static java. lang. annotation. elementType. METHOD; 4 import static java. lang. annotation. retentionPolicy. RUNTIME; 5 6 import java. lang. annotation. export ented; 7 import java. lang. annotation. elementType; 8 import java. lang. annotation. retention; 9 import java. lang. annotation. retentionPolicy; 10 import java. lang. annotation. target; 11/** 12*13 * @ author Minzhe Xu, afternoon, December 13, April 27, 2017. 5214*15 * @ Target indicates the location where the annotation is used. Possible values include 16 * ElemenetType in the ElemenetType enumeration class. CONSTRUCTOR: the CONSTRUCTOR declares 17 * ElemenetType. FIELD: FIELD Declaration (including enum instances) 18 * ElemenetType. LOCAL_VARIABLE: local variable Declaration 19 * ElemenetType. METHOD: METHOD declaration 20 * ElemenetType. PACKAGE: PACKAGE Declaration 21 * ElemenetType. PARAMETER: PARAMETER Declaration 22 * ElemenetType. TYPE: Class, interface (including annotation TYPE) or enum Declaration 23*24 * @ Retention indicates the level at which the annotation information is saved. Optional parameter values include: 25 * RetentionPolicy. SOURCE: The annotation will be discarded by the compiler 26 * RetentionPolicy. CLASS: annotation is available in the class file, but 27 * RetentionPolicy is discarded by VM. runtime vm: Annotations are retained at RUNTIME, so the annotation information can be read through the reflection mechanism. 28*29 * @ Documented includes this annotation in javadoc, which indicates that this annotation will be extracted into a document by the javadoc tool. The content in the doc document varies depending on the information in the annotation. Equivalent to @ see, @ param, etc. 30*31 * @ Inherited allows the subclass to inherit the annotations in the parent class. 32 */33 34 @ Target ({ElementType. TYPE, ElementType. CONSTRUCTOR, ElementType. METHOD, ElementType. PARAMETER, ElementType. FIELD}) 35 @ Retention (RetentionPolicy. RUNTIME) 36 public @ interface TestA {37 38/** 39 * @ interface is used to declare an annotation. In 40 *, each method actually declares a configuration parameter. 41 * The method name is the parameter name, and the return value type is the parameter type (the return value type can only be basic type, Class, String, and enum ). 42 * You can use default to declare the default value of a parameter. 43 */44 String name (); 45 int id () default 0; 46 Class gid (); 47 48}