Basic principles and programming implementation of Java annotations

Source: Internet
Author: User

Java 1.5 started with annotations and reflections, and rightly speaking, annotations are part of reflection, no reflection, annotations are not working properly, but leaving annotations, reflection can still be used, so

The definition of reflection should contain annotations in order to be reasonable. Of course, this is just a personal idea, as to why Java officials are so clear and clear, the most important thing is how to use annotations.


The functionality of the annotations is divided into 2 parts:

    1. As a specific tag

    2. Carrier of additional information


>>>> Defining a Userannotation annotation class

@Target (value = {Elementtype.field}) @Retention (retentionpolicy.runtime) public @interface userannotation {public int ID () default 0;public string name () default "";p ublic int age () default 18;public string gender () default "M";}

One of the @target @retention itself is an annotation.

The "@target" annotation is assigned to a class of Java member annotations, specifying what the annotation target should be.

Annotated source code for @target

@Documented @retention (retentionpolicy.runtime) @Target (elementtype.annotation_type)//This is not cheating, it is indeed self-explanatory, So annotations can also be given to annotations public @interface Target {elementtype[] value (),//value can make an array value={...}} Public enum ElementType {type,//Give class (type) annotation field,//To the field annotation, do not forget that the field can be the object method,//To the methods annotated PARAMETER,//to the parameter note C Onstructor,//to the construction method annotated local_variable,//to the local variable annotation annotation_type,//annotation (this seems to be inappropriate for the Class) package,//packet annotation Type_para METER,//Do not know, wait until you know I write again here Type_use//This also don't know}

"@Retention" indicates the state of the annotation run, in other words, what state the annotation is in to run

Note @Retention 's source code

@Documented @retention (retentionpolicy.runtime) @Target (elementtype.annotation_type) public @interface Retention { Retentionpolicy value ();} Public enum Retentionpolicy {source,//source state run, class,//compile class file when run runtime//run-time}

In general, the source state running and compilation State running annotations are often related to the compiler, such as error, warning, class compilation parameters, etc., these 2 types of state and the editor plug-in is very close, here no longer discuss

The runtime uses the most in daily development, which is a state that developers can control.


"@Documented" amount, not much explanation, in particular, the explanation of the document is generated to add the interpretation of the class .


The annotation method description, the annotation only method, No field, because the annotation is also a interface, but preceded by an @ symbol, and one point is that these methods can not be implemented, writing and C + + pure virtual function similar

See the explanation of the ID () method

public int ID () default 0; /* First, the method must be public, remove public, the default is public, interface interface not so? * Second, default defaults are not required, the method must have a return value, the return value can be a complex object in Java, or it can be a basic type, Enumeration all lines * such as elementtype[] value (); */


The annotations are defined, how to use them, as follows

public class testmain{   @UserAnnotation (age=20,gender= "F", Id=2014,name= "Zhangsan")//use of annotations   private object obj;    public static void main (String [] args]  throws exception  {     filed objfield =  testmain.class.getfield ("obj");     userannotation ua =  Objfield.getannotation (Userannotation.class);//Get annotated, play a role in marking           system.out.println (Ua.age () + "," +ua.gender () + "," +ua.id () + "," +ua.name ());     //*** Further operation, if the object is to point to a user class, then you can say the value of the annotation to him     testmain tm = new testmain () ;     objfiled.set (Tm,new user (Ua.age (), Ua.gender (), Ua.id (), Ua.name ());  //Good, Send your own information to obj, play the role of additional information         //-----------please feel free to daydream ~ ~, Here's how annotations can get annotations for their annotations-------------  &NBsp Target t = ua.annotationtype (). Getannotation (Target.class)    ElementType[]  Values = t.value ();    //~~~~~~~~~~~~~~, once again, free to daydream ~~~~~~~~~~~~~~       sysout.out.println ("Note: It is reverie, not blind!! ");   }}


Try doing it.



Basic principles and programming implementation of Java annotations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.