The information in the note is already in class, how should we read it?
1 Java.lang.reflect.AnnotatedElement Interface: 2 3 Public annotation[] Getannotation (Class annotationtype); 4 5 Public annotation[] Getdeclaredannotations (); 6 7 Public Boolean isannotationpresent (Class annotationtype);
View Code
Class, Constructor, field, method, package and other categories, have implemented the Annotatedelement interface, since class, Constructor, field, method, package and other categories, All implement the Annotatedelement interface, obviously they can be called annotatedelement inside the method, the above methods are defined in this interface, When defining annotation, you must set Rententionpolicy as runtime, that is, you can read annotation information in the VM.
Second, through the reflection of the way to read the function in the class, the method is modified above the annotation information.
1, public boolean isannotationpresent (class<? extends annotation> Annotationclass)
This code means: Annotationclass The specified annotation, if it exists on the top of the method, returns a true, this method is mainly used to determine whether there are annotations on a method.
2. Public <t extends annotation> T getannotation (class<t> annotationclass)
Returns a annotation object that is a specific annotation class object, and returns the annotation if such a annotation exists. Otherwise, a null value is returned
3, class<? Extends Annotation> Annotationtype ()
Returns the class object corresponding to the annotation.
Third, limit the use of annotation objects
@Target
Use Java.lang.annotation.Target to define the timing of its use, which is to limit where annotations can be used: methods, classes, member properties, variables, Specify one of the enumeration values for Java.lang.annotation.ElementType at the time of definition to indicate which targets this annotation can decorate.
Package Java.lang.annotation
Public Enum elementtype{
type,//for Class,interface,enum
field,//for Field
method,//Applicable method
parameter,//for Parater on method
consttructor,//for constructor
local_variable,//applicable local Variables
annotation_type,//Suitable for ANNOTATION mode
package//for Package
}
1 Packagecom.javase.annotation;2 ImportJava.lang.annotation.ElementType;3 ImportJava.lang.annotation.Target;4 /**5 * Customize an annotation, this annotation can only modify the method6 * @authorX_xiongjie7 *8 */9 @Target (Elementtype.method)Ten Public@InterfaceMytarget { One String value (); A } - - Packagecom.javase.annotation; the - Public classMytargettest { -@MyTarget (value= "Zhangsan") - Public voiddosomething () { +System.out.println ("Hello World"); - } +}View CodeIv. Requirements for API file @documented
In the production of Javadoc files, if you want to add annotation content to the API file, need to use java.lang.annotation.Documented, in the generation of Java doc, the annotation information is also generated to Java Doc.
1 Packagecom.javase.annotation;2 Importjava.lang.annotation.Documented;3 4 @Documented5 Public@Interfacedocumentedannotation {6 String hello ();7 }8 Packagecom.javase.annotation;9 Ten Public classDocumentedtest { One@DocumentedAnnotation (hello= "Welcome") A Public voidmethod () { -System.out.println ("HelloWorld"); - } the}View CodeV. @Inherited
when defining annotation, add the annotation of java.lang.annotation.Inherited morphology, inherited indicates whether an annotation information can be automatically inherited, and if you add inherited annotations to the annotations, they are inherited by the quilt class in the parent class.
VI: summary
1. To know what the origin of the annotation is, how to define the annotation.
2, how to use reflection to parse the information of the annotations, and to parse the annotations under the modified class, methods. Depending on whether the annotation has the value of the annotation, the corresponding method is called, or the corresponding class is processed. This is very important, you can get the Retentionpolicy set to runtime.
Java annotation mechanism