Meta-annotations refer to annotations, including @retention @Target @Document @Inherited four.
[Email protected]: Define retention policies for annotations
@Retention (Retentionpolicy.source)//annotations only exist in the source code and are not included in the class bytecode file
@Retention (Retentionpolicy.class)//default retention policy, annotations exist in the CLASS bytecode file, but are not available at run time.
@Retention (retentionpolicy.runtime)//annotations are present in the class bytecode file and can be obtained through reflection at run time
The first thing to be clear is the life cycle length SOURCE < CLASS < RUNTIME, so the former can function where the latter must also be able to function. In general, if you need to dynamically obtain the annotation information at runtime, it can only be annotated with runtime, if you want to do some preprocessing at compile time, such as generating some auxiliary code (such as Butterknife), use class annotation; If you just do some checking, like @ Override and @SuppressWarnings, you can select the SOURCE annotation.
[email protected]: Define the purpose of the annotation
Source:
@Documented &NBSP;
@Retention (retentionpolicy.runtime)
@Target (Elementtype.annotation_type) &NBSP;
public @interface Target {
elementtype[] Value (); &NBSP;
}
@Target (elementtype.type) //interfaces, classes, enumerations, annotations
@Target (Elementtype.field)//field, enumerated constant
@Target (Elementtype.method)//method
@Target (Elementtype.parameter)//Method parameters
@Target (elementtype.constructor) // Constructor
@Target (elementtype.local_variable)//local variable
@Target (elementtype.annotation_ TYPE)//annotations
@Target (elementtype.package)//package
[email Protected]: Note that the note will be included in Javadoc
[email protected]: The description subclass can inherit the annotation from the parent class
example:
//applicable classes, interfaces (including annotation types) or enumerations@Retention (retentionpolicy.runtime) @Target (elementtype.type) Public@InterfaceClassInfo {String value (); } //Apply field attribute, also include enum constant@Retention (retentionpolicy.runtime) @Target (Elementtype.field) Public@InterfaceFieldInfo {int[] Value (); } //Applicable methods@Retention (retentionpolicy.runtime) @Target (Elementtype.method) Public@InterfaceMethodInfo {String name ()default"Long"; String data (); intAge ()default27; }
These 3 annotations apply to different elements, each with different attributes, and you need to set these property values when using annotations.
Then define a test class to use these annotations:
@ClassInfo ("Test Class") Public classtestruntimeannotation {@FieldInfo (value= {1, 2}) PublicString fieldInfo = "Filedinfo"; @FieldInfo (Value= {10086}) Public inti = 100; @MethodInfo (Name= "Bluebird", data = "Big") Public StaticString Getmethodinfo () {returnTestruntimeannotation.class. Getsimplename (); Get the annotation information in the code:Private void_testruntimeannotation () {StringBuffer sb=NewStringBuffer (); Class<?> cls = testruntimeannotation.class; Constructor<?>[] Constructors =cls.getconstructors (); //gets the annotations of the specified typeSb.append ("Class annotation:"). Append ("\ n"); ClassInfo ClassInfo= Cls.getannotation (ClassInfo.class); if(ClassInfo! =NULL) {sb.append (modifier.tostring (Cls.getmodifiers ())). Append (" "). Append (Cls.getsimplename ()). Append ("\ n"); Sb.append ("Note value:"). Append (Classinfo.value ()). Append ("\ n")); } sb.append ("Field Note:"). Append ("\ n")); Field[] Fields=Cls.getdeclaredfields (); for(Field field:fields) {FieldInfo FieldInfo= Field.getannotation (FieldInfo.class); if(FieldInfo! =NULL) {sb.append (modifier.tostring (Field.getmodifiers ())). Append (" "). Append (Field.gettype (). Getsimplename ()). Append (" "). Append (Field.getname ()). Append ("\ n"); Sb.append ("Note value:"). Append (Arrays.tostring (Fieldinfo.value ())). Append ("\ n"); }} sb.append ("Method Note:"). Append ("\ n")); Method[] Methods=Cls.getdeclaredmethods (); for(Method method:methods) {MethodInfo MethodInfo= Method.getannotation (MethodInfo.class); if(MethodInfo! =NULL) {sb.append (modifier.tostring (Method.getmodifiers ())). Append (" "). Append (Method.getreturntype (). Getsimplename ()). Append (" "). Append (Method.getname ()). Append ("\ n"); Sb.append ("Annotation value:"). Append ("\ n")); Sb.append ("Name:"). Append (Methodinfo.name ()). Append ("\ n"); Sb.append ("Data:"). Append (Methodinfo.data ()). Append ("\ n"); Sb.append ("Age:"). Append (Methodinfo.age ()). Append ("\ n"); }} System.out.print (Sb.tostring ()); }
Java meta annotations