Annotations are equivalent to a tag, adding annotations in a program is equivalent to marking the program with some kind of tag, no addition, then there is no kind of tag, later, the Javac compiler, development tools and other programs can use reflection to understand your class and the various elements there is no mark, see what you have tags, Go and do the right thing. Tags can be added to packages, classes, fields, methods, method parameters, and local variables.
Custom annotations and their applications 1), defining one of the simplest annotations
public @Interface myannotation { //... }
2), add annotations to a class:
@MyAnnotation Public class annotationtest{ //... }
Four types of meta-annotations are provided in Java, which are specifically responsible for annotating other annotations, as follows:
A @Retention-dollar annotation that indicates the level at which the annotation information (life cycle) needs to be saved. The optional retentionpoicy parameters include:
Retentionpolicy.source: Stuck in Java source file, compiler was discarded
Retentionpolicy.class: Stays in CLASS file, but is discarded by VM (default)
Retentionpolicy.runtime: In-memory bytecode, VMS will also retain annotations at run time, so the annotations can be read through the reflection mechanism
@Target-dollar annotations, the default value is any element that indicates where the annotation is used. The available ElementType parameters include
Elementtype.constructor: Constructor declaration
Elementtype.field: Member variables, objects, attributes (including enum instances)
Elementtype.local_variable: local variable declaration
Elementtype.method: Method declaration
Elementtype.package: Package Declaration
Elementtype.parameter: Parameter declaration
Elementtype.type: Classes, interfaces (including annotation types) or enum declarations
@Documented annotations are included in the Javadoc
@Inheried annotations allow subclasses to inherit annotations from the parent class
The following is a case of a mock note:
First write a custom annotation @myannotation, as shown in the following code:
Packagecom.pcict.anotation.test;Importjava.lang.annotation.*; @Retention (retentionpolicy.runtime) @Target ({elementtype.method, elementtype.type}) Public@Interfacemyannotation {//Adding attributes to annotationsString color (); String value ()default"I am XXX";//provide a default value for a property int[] Array ()default{1, 2, 3 }; Gender Gender ()defaultGender.man;//Add an enumeration//To add an enumeration propertyMetaannotation metaannotation ()default@MetaAnnotation (Birthday = "My date of birth is 1988-2-18");}
To write an enumeration class gender, add an enumeration attribute to the mock annotation, as shown in the following code:
package Com.pcict.anotation.test; public enum Gender {man { public String GetName () { return "male" ; }}, women { public String Getn Ame () { return "female" ; } }; // back Remember ";" public abstract String GetName ();}
Write an annotation class metaannotation, add annotation attributes to the analog annotations, and the code is as follows:
Package com.pcict.anotation.test; public @Interface metaannotation { String birthday ();}
Finally write the annotation test class annotationtest, the code is as follows:
Packagecom.pcict.anotation.test;//invoke annotations and assign values@MyAnnotation (metaannotation = @MetaAnnotation (Birthday = "My birth date is 1988-2-18"), color = "Red", array = { 23, 26 }) Public classAnnotationtest { Public Static voidMain (string[] args) {//Check if class annotationtest contains @myannotation annotations if(Annotationtest.class. isannotationpresent (myannotation.class)) { //get annotations if they existmyannotation annotation = (myannotation) annotationtest.class. Getannotation (myannotation.class); System.out.println (annotation); //Get Annotation PropertiesSystem.out.println (Annotation.color ()); System.out.println (Annotation.value ()); //Array int[] Arrs =Annotation.array (); for(intArr:arrs) {System.out.println (arr); } //EnumerationGender Gender =Annotation.gender (); System.out.println ("Sex is:" +gender); //Get Annotation PropertiesMetaannotation meta =annotation.metaannotation (); System.out.println (Meta.birthday ()); } }}
Run Annotationtest and the output is:
Java Custom Annotations Annotation detailed description