Annotations are tokens that add some kind of annotation to a program to mark a program , and in the Javac compiler, development tools and other programs can use reflection to see if there are tags on your class and on various elements, 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), define one of the simplest annotations
Public @interface Myannotation {//...}
2), add annotations to a class:
@MyAnnotationpublic class annotationtest{//...}
Annotation Creation Case:
package com.ljq.test; import java.lang.annotation.elementtype; import java.lang.annotation.retention; import java.lang.annotation.retentionpolicy; import java.lang.annotation.target; /** * define an annotation * * * @author Jiqinlin * */ //java provides four kinds of meta-annotations, which are specifically responsible for annotating other annotations, as follows //@Retention meta annotations, which indicate the level at which the annotation information needs to be saved (life cycle). The optional retentionpoicy parameters include: //retentionpolicy.source: stuck in the Java source file and the compiler is discarded // Retentionpolicy.class: Stays in the CLASS file but is discarded by the VM (default) // Retentionpolicy.runtime: In-memory bytecode, the VM will also retain annotations at run time, so you can read the annotation information //the @Target meta-annotation by the reflection mechanism, and the default value is any element that indicates where the annotation is used. The available ElementType parameters include the //ElementType.CONSTRUCTOR: constructor declaration //ElementType.FIELD: member variables, objects, attributes (including enum instances) //ElementType.LOCAL_VARIABLE: local variable declarations //ElementType.METHOD: method declarations // elementtype.package: Package declaration //ElementType.PARAMETER: parameter declaration //ElementType.TYPE: class, Interfaces (including annotation types) or enum declarations //@Documented include annotationsIn Javadoc //@Inheried allow subclasses to inherit annotations @Retention (retentionpolicy.runtime) @Target in the parent class ({Elementtype.method, elementtype.type}) public @interface myannotation { //Add attributes to annotations string color (); string value () default "I am Lin Yi-chin"; //provides a default value int[] array () default {1, 2 for a property, 3}; gender gender () default gender.man; //add an enumeration metaannotation metaannotation () default @MetaAnnotation (birthday= " My date of birth is 1988-2-18 "); //Add enumeration property }
annotations test class
package com.ljq.test; /** * annotation Test * * * @author jiqinlin * */ //invoke annotations and assign values @MyAnnotation ([email protected] (birthday = " My date of birth is 1988-2-18 "), color=" Red ", array={23, 26}) public class annotationtest { public static void main (String[] args) { //Check if class annotationtest contains @myannotation annotations if (AnnotationTest.class.isAnnotationPresent (myannotation.class)) { //get annotations if they exist myannotation annotation= (myannotation) AnnotationTest.class.getAnnotation ( Myannotation.class); system.out.println (annotation); //Get Annotation Properties system.out.println (Annotation.color ()); system.out.println (Annotation.value ()); //Array int[] arrs=annotation.array (); for (Int arr:arrs) { system.out.println (arr); } //Enumeration gender gender=annotation.gender (); &nbsP; system.out.println ("Gender:" +gender); //Get Annotation Properties Metaannotation meta=annotation.metaannotation (); system.out.println (Meta.birthday ()); } }}
enum class gender, adding enumeration properties to the mock annotations
package com.ljq.test; /** * enumeration, Add enumeration properties to the analog annotations * * @author jiqinlin * */ public enum gender { MAN{ public String GetName () {return "Male";} }, women{ public string getname () {return "female";} }; //remember there was ";" public abstract string getname ();}
Annotation class metaannotation, adding annotation properties to mock annotations
Package com.ljq.test;
Recommended Watch: xutils annotation mechanism http://blog.csdn.net/rain_butterfly/article/details/37931031
Java Annotations Annotation Detailed