Starting with JDK 5.0, Java added support for meta data (MetaData), which is Annotation (annotations)
Annotation is actually a special tag in the code that can be read at compile, class load, run time, and perform the appropriate processing. By using Annotation, programmers can embed some supplemental information in the source file without changing the original logic.
Annotation can be used like modifiers, which can be used to modify the declarations of packages, classes, constructors, methods, member variables, parameters, and local variables, which are stored in the Annotation "Name=value" pair.
Annotation can be used to set metadata for program elements (classes, methods, member variables, etc.)
Three basic Annotation:
@Override: Qualifies overriding the parent class method, which can only be used for methods
@Deprecated: Used to indicate that a program element (class, method, and so on) is obsolete
@SuppressWarnings: Suppress Compiler warnings
JDK5.0 provides annotation types specifically on annotations, namely:
1.Retention
2.Target
3.Documented
4.Inherited
@Retention: can only be used to decorate a Annotation definition that specifies how long the Annotation can be retained, @Rentention a member variable that contains a Retentionpolicy type, using @Rent You must specify a value for the value member variable when ention:
Retentionpolicy.source: Compiler directly discards comments for this policy
Retentionpolicy.class: The compiler will record the comments in the CLASS file. The JVM does not preserve annotations when running Java programs. This is the default value
Retentionpolicy.runtime: The compiler will record the comments in the class file. When you run a Java program, the JVM preserves the comments. The program can get the comment by reflection
@Target: used to decorate the Annotation definition to specify which program elements the decorated Annotation can use to decorate. The @Target also contains a member variable named value.
@Documented: The Annotation class that is used to specify that the Annotation is decorated by the meta-javadoc tool is extracted as a document.
Annotations defined as documented must have the retention value set to runtime.
@Inherited: The Annotation that it modifies will have inheritance. If a class uses a Annotation that is @Inherited decorated, its subclasses automatically have that annotation
In practical applications, use less
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: Classes Definition
Constructor: Constructor definition
Field: Tired member variable definition
Method: Methods Definition of Class
Package: Packages definition for Class
The Java.lang.reflect package consists of a number of tool classes that implement the reflection function, and 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 modified program element, if the type annotation does not exist, Then NULL is returned.
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 (): Returns all annotations 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.
A simple annotation Processor:
/*********** Note Declaration ***************//** * fruit name Annotation * @author yang * */@Target ( Elementtype.field) @Retention (retentionpolicy.runtime) @Documentedpublic @interface fruitname { string value () default "";} /** * fruit Color Annotations * @author yang * */@Target (Elementtype.field) @Retention ( Retentionpolicy.runtime) @Documentedpublic @interface fruitcolor { /** * Color Enumeration * @author peida * */ public enum Color{ bule,red,green}; /** * Color Properties * @return */ color fruitcolor () default color.green;} Note by/** * fruit supplier * @author yang * */@Target (Elementtype.field) @Retention (retentionpolicy.runtime) @ documentedpublic @interface FruitProvider { /** * supplier number * @return */ public int id () default -1; /** * supplier name * @return */ public string name () default ""; /** * Supplier Address * @ Return */ public string address () default "";} /*********** Annotations Using ***************/public class apple { @FruitName ("AppLe ") private String appleName; @FruitColor (fruitcolor=color.red) private String appleColor; @FruitProvider (id=1,name= "Shaanxi Red Fuji Group", address= "No. 89th, Yan an LU, Xi ' an city, Shaanxi province") private String appleProvider; public void setapplecolor (String applecolor) { this.applecolor = applecolor; } public string getapplecolor () { return applecolor ; } public void setapplename ( String applename) { this.appleName = Applename; } &nBsp; public string getapplename () { return applename; } public Void setappleprovider (String appleprovider) { this.appleprovider = appleprovider; } public String getappleprovider () { return appleprovider ; } public void displayname () { system.out.println ("The name of the fruit is: Apple"); }}/*** Note Processor ***************/public class fruitinfoutil { public static void getfruitinfo (Class<?> clazz) { &nbsP String strfruitname= " Fruit name:"; string strfruitcolor= " Fruit color:" string strfruitprovicer= " Supplier information: "; field[ ] fields = clazz.getdeclaredfields (); for (Field field :fields) { if (Field.isannotationpresent (fruitname.class)) { fruitname fruitname = (Fruitname) field.getannotation (fruitname.class); strfruitname=strfruitname+fruitname.value (); system.out.println (Strfruitname); } else if (Field.isannotationpresent (fruitcolor.class)) { FruitColor fruitColor= (Fruitcolor) Field.getannotation (Fruitcolor.class); strfruitcolor=strfruitcolor+fruitcolor.fruitcolor (). toString (); system.out.println (StrFruitColor ); } else if (Field.isannotationpresent (FruitProvider.class)) { FruitProvider fruitProvider= (Fruitprovider) field.getannotation (Fruitprovider.class); strfruitprovicer= " Supplier Number:" +fruitprovider.id () + " Supplier Name:" + Fruitprovider.name () + " Supplier Address:" +fruitprovider.address (); system.out.println (Strfruitprovicer); } } }}/*********** Output Results ***************/public class fruitrun { /* * * @param args */ public static Void main (String[] args) { fruitinfoutil.getfruitinfo (Apple.class); }}==================================== fruit name:apple fruit color:red supplier number:1 supplier Name: Shaanxi Red Fuji Group supplier Address: Red Fuji Building, No. 89th, Yan an LU, Xi ' an, Shaanxi province
The basics of Java annotations (see the map below) are basically all over again, the next we can synthesize the application and further deepen the understanding and application of the various knowledge points of annotations by designing a simple ORM framework based on annotations.
650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M00/98/76/wKioL1k8zU6h-WbfAAdDqAiRht8629.jpg "title=" 25200814-475cf2f3a8d24e0bb3b4c442a4b44734.jpg "alt=" Wkiol1k8zu6h-wbfaaddqairht8629.jpg "/>
This article is from the "Ciyo Technology sharing" blog, please be sure to keep this source http://ciyorecord.blog.51cto.com/6010867/1934218
Java Annotations Annotation