Java basic consolidation notes (6)-Annotations
Annotation is also called metadata. A code-level description. It is a feature introduced by JDK and later versions. It is at the same level as classes, interfaces, and enumeration. It can be declared before packages, classes, fields, methods, local variables, and method parameters to describe and annotate these elements.
API
Application structure of Annotation
Call/structure relationship:A <-B <-C
A, B, and C are explained as follows:
A: annotation class
@interface A{}
B: The "annotation class" class is applied.
@AClass B{}
C: class that performs the reflection operation on the class of the annotation class.
Class C{ public void f(){ B.class.isAnnotationPresent(A.class); A a = B.class.getAnnotion(A.class); }}
Meta Annotation
The role of meta-annotation is to annotate other annotations. The four meta annotations are:@Target,@Retention,@Documented,@Inherited
@Retention
Indicates the level at which the annotation information is saved. Optional parameter values are of Enumeration type.RetentionPolicy
, IncludingRetentionPolicy.SOURCE
,RetentionPolicy.CLASS
(Default ),RetentionPolicy.RUNTIME
Corresponding to: java source File> class File> byte code in memory
RetentionPolicy. the SOURCE annotation will be discarded by the compiler. the CLASS annotation is available in the class file, but the RetentionPolicy will be discarded by the VM. the runtime vm also keeps comments at RUNTIME, so the annotation information can be read through the reflection mechanism.
@Target
Indicates where the annotation is used and the possible values are in the enumeration class.ElemenetType
, Including
ElemenetType. CONSTRUCTOR declares ElemenetType. FIELD Declaration (including enum instances) ElemenetType. the LOCAL_VARIABLE local variable declares ElemenetType. the METHOD declares ElemenetType. the PACKAGE declares ElemenetType. PARAMETER declaration ElemenetType. TYPE class, interface (including annotation TYPE) or enum Declaration
@Documented
Include 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@see,@param
And so on
@Inherited
Allow subclass to inherit the annotation in the parent class
Custom Annotation
Use@interface
The custom annotation automatically inheritsjava.lang.annotation.Annotation
The compilation program automatically completes other details. You cannot inherit other annotations or interfaces when defining annotations.@interface
It is used to declare an annotation. Each method actually declares a configuration parameter. 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, or enum ). You can use default to declare the default value of a parameter.
Define annotation format:
Public @ interface annotation name {definition body}
Data Types supported by annotation parameters:
1. All basic data types (int, float, boolean, byte, double, char, long, short)
2. String type
3. Class type
4. enum type
5. Annotation type
6. All types of arrays above
Sample Code
The following example is the one mentioned above.A <-B <-C. A custom Annotation@A
And then the annotation is used in Class B.@A
Finally, use reflection to read data in Class C.@A
Information in
A.java
package com.iot.annotation;import java.lang.annotation.*;/** * Created by brian on 2016/2/20. */@Target({ElementType.TYPE,ElementType.METHOD,ElementType.FIELD,ElementType.CONSTRUCTOR})@Retention(RetentionPolicy.RUNTIME)public @interface A { String name(); int id() default 0; Class
gid();}
B.java
Package com. iot. annotation; import java. util. hashMap; import java. util. map;/*** Created by brian on 2016/2/20. */@ A (name = "type", gid = Long. class) // class annotation public class B {@ A (name = "param", id = 1, gid = Long. class) // class member annotation private Integer age; @ A (name = "construct", id = 2, gid = Long. class) // constructor annotation public B () {}@ A (name = "public method", id = 3, gid = Long. class) // class method annotation public void a () {}@ A (name = "protected method", id = 4, gid = Long. class) // class method annotation protected void B () {Map
M = new HashMap
(0) ;}@ A (name = "private method", id = 5, gid = Long. class) // class method annotation private void c () {Map
M = new HashMap
(0) ;}public void B (Integer ){}}
C.java
Package com. iot. annotation; import java. lang. annotation. annotation; import java. lang. reflect. constructor; import java. lang. reflect. method;/*** Created by brian on 2016/2/20. */public class C {/*** simply print out the class annotation used in class B * This method only prints the Type annotation * @ throws ClassNotFoundException */public static void parseTypeAnnotation () throws ClassNotFoundException {Class clazz = Class. forName ("com. iot. annotation. B "); Annotation [] annotations = clazz. getAnnotations (); for (Annotation annotation: annotations) {A a = (A) annotation; System. out. println ("id =" +. id () + "; name =" +. name () + "; gid =" +. gid () ;}}/*** simply print out the Method annotation used in Class B * This Method only prints the Method annotation */public static void parseMethodAnnotation () {Method [] methods = B. class. getDeclaredMethods (); for (Method method: methods) {/** determines whether there is a specified annotation type in the method */boolean hasAnnotation = Method. isAnnotationPresent (. class); if (hasAnnotation) {/** specify the type annotation of the method returned Based on the annotation type */A annotation = method. getAnnotation (. class); System. out. println ("method =" + method. getName () + "; id =" + annotation. id () + "; description =" + annotation. name () + "; gid =" + annotation. gid ());}}} /*** simply print out the Method annotation used in Class B * This Method only prints the Method annotation */public static void parseConstructAnnotation () {Constructor [] constructors = B. class. getConstructors (); for (Constructor constructor: constructors) {/** determine whether the constructor has annotation of the specified annotation type */boolean hasAnnotation = Constructor. isAnnotationPresent (. class); if (hasAnnotation) {/** specify the type annotation of the method returned Based on the annotation type */A annotation = (A) constructor. getAnnotation (. class); System. out. println ("constructor =" + constructor. getName () + "; id =" + annotation. id () + "; description =" + annotation. name () + "; gid =" + annotation. gid () ;}} public static void main (String [] args) throws ClassNotFoundException {parseTypeAnnotation (); parseMethodAnnotation (); parseConstructAnnotation ();}}