The most important part of using annotations is the processing of annotations, then the annotation processor is Involved.
in principle, the annotation processor obtains the annotation information on the inspected method through the reflection mechanism, and then makes specific processing according to the value of the annotation Element. Note Processor Class Library (java.lang.reflect.AnnotatedElement): Java uses the annotation interface to represent annotations in front of the program element, which is the parent interface for all annotation types. In addition, Java has added a annotatedelement interface under the Java.lang.reflect package, which represents a program element that can accept annotations in the program, which has the following implementation Classes: class definition Constructor: constructor Definition Field: The tired member variable defines method: the methods of the class define the Package: Class's bundle definition Java.lang.reflect package contains some tool classes that implement reflection functionality, in fact, The Java.lang.reflect package provides the ability to read runtime annotation information with all the provided reflection Apis. When a annotation type is defined as a run-time annotation, the annotation is visible to the runtime, and the annotation stored in the class file is read by the virtual machine when the class file is Loaded. The Annotatedelement interface is the parent interface for all program elements (class, method, and constructor), so the program obtains the Annotatedelement object for a class by Reflection. The program can call the following four methods of the object to access the Annotation Information: method 1:<t extends annotation> T getannotation (class<t> annotationclass): Returns an annotation of the specified type that exists on the program element, or null if the type annotation does not exist. Method 2:annotation[] getannotations (): Returns all annotations that exist on the program Element. Method 3:boolean is annotationpresent (class<?extends annotation> annotationclass): Determines whether the program element contains annotations of the specified type, and returns true if Present. otherwise, false is Returned. Method 4:annotation[] Getdeclaredannotations (): returnReturns all comments that exist directly on this Element. Unlike other methods in this interface, the method ignores inherited Annotations. (if no comment exists directly on this element, an array of zero length is returned.) The caller of the method can arbitrarily modify the returned array, which does not have any effect on the array returned by other Callers. In-depth understanding of Java: annotations (Annotation) customizing annotations to learn more about annotations, we must be able to define our own annotations and use annotations to define our own annotations before we have to understand the syntax that Java provides us with the meta-annotations and related definition annotations. Meta annotations: The role of meta annotations is to be responsible for annotating other Annotations. Java5.0 defines 4 standard meta-annotation types, which are used to provide descriptions of other annotation types. Java5.0 definition of meta-annotations: [email protected], [email protected], [email protected], [email protected] These types and the classes they support can be found in the Java.lang.annotation package. Let's take a look at the function of each meta-annotation and the instructions for using the corresponding Sub-parameters. @Target: @Target illustrates the range of objects that annotation modifies: annotation can be used for packages, types (classes, interfaces, enumerations, annotation types), type Members (methods, constructor methods, member variables, Enumeration values) , method parameters, and local variables (such as loop variables, catch parameters). Target can be more clearly decorated by using target in the declaration of the annotation Type. Function: used to describe the use of annotations (I.E. where annotations can be Used) value (elementtype) is: 1.CONSTRUCTOR: used to describe the constructor 2.FIELD: used to describe the domain 3.local_variable: used to describe the local variables 4.METHOD: used to describe the method 5.PACKAGE: used to describe the package 6.PARAMETER: used to describe the parameters 7.TYPE: used to describe classes, interfaces (including annotation types) or enum declaration usages: @Target (elementtyp E.type) Public @interface Table {/** * Datasheet name annotation, default value is class name * @return */public string TableName () Default "className"; @Target (elementtype.field) Public @interface nodbcolumn {} Note Table can be used for annotation classes, interfaces (including annotation types), or enum declarations, Note Nodbcolumn is only available for member variables of the annotation class. @Retention: @Retention defines how long the annotation is retained: some annotation appear only in the source code and are discarded by the compiler, while others are compiled in the class file The annotation that are compiled in the class file may be ignored by the virtual machine, while others will be read when class is loaded (note that it does not affect class execution because annotation is separated from the class in use). Using this meta-annotation can limit the "life cycle" of Annotation. Function: indicates the level at which this comment information needs to be saved to describe the lifetime of the annotation (i.e., what is the description of the annotation in Effect) value (retentionpoicy) is: 1.SOURCE: valid in the source file (that is, The source file is Reserved) 2.CLASS: Valid in class file (that is, class Reserved) 3.RUNTIME: valid at run time (that is, run-time retention) Retention meta-annotation type has a unique value as a member, Its value is derived from the enumeration type value of Java.lang.annotation.RetentionPolicy. The specific examples are as follows: @Target (elementtype.field) @Retention (retentionpolicy.runtime) public @interface Column {public String name () Default "fieldName"; Public String setfuncname () default "setfield"; Public String getfuncname () default "getField"; Public boolean defaultdbvalue () default false; The Retentionpolicy property value of the} column annotation is rutime so that the annotation processor can get the property value of the annotation by reflectionTo do some logical processing of the runtime @Documented: @Documented used to describe other types of annotation that should be considered as public APIs for annotated program members, so they can be documented by tools such as Javadoc. Documented is a markup annotation with no members. @Target (elementtype.field) @Retention (retentionpolicy.runtime) @Documented public @interface Column {public String Name () default "fieldName"; Public String setfuncname () default "setfield"; Public String getfuncname () default "getField"; Public boolean defaultdbvalue () default false; @Inherited: the @Inherited meta-annotation is a markup annotation @Inherited illustrates that a type that is being labeled is Inherited. If a annotation type that uses the @inherited modifier is used for a class, the annotation will be used for subclasses of that class. Note: @Inherited The annotation type is inherited by subclasses of the class being Labeled. The annotation class does not inherit from the interface it implements, and the method does not inherit annotation from the method it overloads. The reflection API enhances this inheritance when the retention of the @inherited annotation type callout annotation is Retentionpolicy.runtime. If we use Java.lang.reflect to query the annotation of a @inherited annotation type, the reflection code check will work: examine the class and its parent class until the specified annotation type is found, or reach the top level of the class inheritance Structure. Instance code:/** * * @author Peida * */@Inherited public @interface greeting {public enum fontcolor{bule,red,green}; String name (); FontColor FontColor () Default fontcolor.green; Custom annotations: When you use @interface to customize annotations, the Java.lang.annotation.Annotation interface is automatically inherited and other details are automatically completed by the Compiler. When you define annotations, you cannot inherit other annotations or Interfaces. @interface is used to declare an annotation, in which each method actually declares a configuration parameter. The name of the method is the name of the parameter, and the return value type is the type of the parameter (the return value type can only be base type, Class, String, enum). You can declare the default value of a parameter through Default. Define annotation format: public @interface annotation name {define body} the supported data types for 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. Above all types of array annotation type parameters how to set: first, only public or default These two access rights are decorated. for example, String value (), where the method is set to Defaul default type, and second, parameter members can only be byte,short,char,int,long,float,double with the base type. Boolean eight data types such as basic data types and string,enum,class,annotations, and arrays of these types. for example, string value (), where the parameter member is string; thirdly, If there is only one parameter member, it is best to set the parameter name to "value", followed by Parentheses. example: The following example Fruitname annotation has only one parameter member. Simple custom annotations and examples of using annotations: package annotation; Import java.lang.annotation.Documented; Import java.lang.annotation.ElementType; Import java.lang.annotation.Retention; Import java.lang.annotation.RetentionPolicy; Import Java.lang.annotation. Target; /** * Fruit Name notes * @author Peida * */@Target (elementtype.field) @Retention (retentionpolicy.runtime) @Documented public @inter Face fruitname {String value () default "";} Package annotation; Import java.lang.annotation.Documented; Import java.lang.annotation.ElementType; Import java.lang.annotation.Retention; Import java.lang.annotation.RetentionPolicy; Import java.lang.annotation.Target; /** * Fruit Color Annotations * @author Peida * */@Target (elementtype.field) @Retention (retentionpolicy.runtime) @Documented public @inter Face Fruitcolor {/** * Color Enumeration * @author Peida * */public enum color{bule,red,green};/** * Color Properties * @return */color FRUITC Olor () Default color.green; }package annotation; Import Annotation. fruitcolor.color; public class Apple {@FruitName ("apple") private string applename; @FruitColor (fruitcolor=color.red) private String apple Color; public void Setapplecolor (String applecolor) {this.applecolor = applecolor;} public String getapplecolor () {return APPL ecolor; } public void SETapplename (string Applename) {this.applename = applename;} public String getapplename () {return applename;} public voi D displayName () {System.out.println ("the name of the fruit is: apple");}} Default value of the annotation element: the annotation element must have a definite value, either specified in the default value of the definition annotation, or specified when the annotation is used, and the value of a non-basic type of annotation element cannot be Null. therefore, using an empty string or 0 as the default value is a common practice. This constraint makes it difficult for the processor to represent the presence or absence of an element, because in each annotation declaration, all elements are present and have corresponding values, in order to circumvent this constraint, we can only define special values, such as an empty string or a negative number, once to indicate that an element does not exist, and when defining annotations, This has become a customary usage. Example: 1 Package annotation; 2 3 Import java.lang.annotation.Documented; 4 Import java.lang.annotation.ElementType; 5 Import java.lang.annotation.Retention; 6 Import java.lang.annotation.RetentionPolicy; 7 Import java.lang.annotation.Target; 8 9/** 10 * Fruit Supplier Note * * @author peida * * * * @Target (elementtype.field) @Retention (retentionpolicy.runtime) 16 @Documented public @interface Fruitprovider {18/** 19 * Supplier Number * @return * */public int ID () default-1; 23 2 4/** 25 * Supplier Name: * @return * * * * The public String name () default ""; 29 30/** 31 * Supplier Address * @return * */public String Address () Default ""; 35}
Java basic-annotation