Annotation: A new feature added after JDK1.5, which is called a metadata feature, called a comment after JDK1.5,
That is, using annotations to add information about some programs.
The Java.lang.annotation annotation interface is an interface that all annotation must implement.
System built-in annotation
After JDK1.5, the system has established the following three built-in annotation types, users can directly use.
@Override: Overwrite the annotation
@Deprecated: annotation used in disapproval
@SuppressWarnings: annotation to suppress safety warnings
Custom annotation
Annotation Definition Format:
"Public" @interface annotation name {
Data type variable name ();
}
public @interface Meaning { String value(); }
After that, you can use the @meaning format directly in your program.
@Meaning(value="itmyhome") class Demo{ }
You can set a parameter in annotation to receive the contents of a variable, such as value above, and you must assign a value to a parameter when you use annotation
such as: value= "Itmyhome"
Since you can set a parameter, you can also set multiple parameters.
public @interface Meaning { String key(); String value(); }
This annotation need to set two parameters when using, a key one value
@Meaning(key="hi",value="itmyhome") class Demo{ }
You can also set an array in
public @interface Meaning { String[] value(); }
The received content to pass the array
@Meaning(value={"hello","world"}) class Demo{ }
There is a feature in all the annotation defined above, and all of the parameter contents need to be set up when using annotations.
You can also set the default content for a parameter,
Use default at the time of declaration.
public @interface Meaning { String value() default ""; //默认为空 }
You can not set a value when you use it
@Meaning class Demo{ }
In the operation, for a annotation, sometimes the fixed period of the value range, only a fixed number of values, this time actually need to rely on enumeration.
public enum FormItemType { //定义枚举类型 hidden,text,select,date }
define Annotation
public @interface Meaning { FormItemType value() ; //设置为枚举类型 }
The value of the annotation can only be a value in the enumeration type
@Meaning(value=FormItemType.date) class Demo{ }
Retention and Retentionpolicy
In annotation, you can use retention to define the save scope of a annotation, which is defined as follows:
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Meaning { FormItemType value() ; //设置为枚举类型 }
In the retetion definition above, there is a retentionpolicy variable, which is used to specify annotation's save range, Retentionpolicy contains three ranges
Of the three ranges, the most important one is the runtime range, because it works at the time of execution.
Built-in annotation retentionpolicy
Three built-in definitions of annotation:
The override definition uses @retention (retentionpolicy.source) to appear only in the source file
The deprecated definition uses @retention (retentionpolicy.runtime), which can occur at execution time.
The suppresswarnings definition is @retention (retentionpolicy.source) and can only appear in the source file
A annotation if you want to make it meaningful, you must combine the reflection mechanism to get all the content set in the annotaion.
There are several methods in class that are related to annotation operations
PackageCom.Itmyhome;ImportJava.lang.annotation.Annotation;ImportJava.lang.reflect.Method;ClassDemo{@SuppressWarnings("Unchecked")@Deprecated@OverridePublicStringTostring(){Return"Hello";}}PublicClassT{PublicStaticvoidMain(String[]Args)ThrowsException{Class<?>C=Class.Forname("Com.itmyhome.Demo");method mt = c. Getmethod ( "toString" //Find tostring Method annotation an[] = mt. (); //get all Annotation for (annotation a:an) {system. Out. (a} } } /span>
At this point a annota has been made. The above operations are actually done through the built-in annotation of three systems, or you can customize a annotation
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface Meaning { FormItemType value() ; //设置为枚举类型 }
PackageCom.Itmyhome;ImportJava.lang.reflect.Method;ClassDemo{@Meaning(Value=Formitemtype.Select)Custom annotation@SuppressWarnings("Unchecked")@Deprecated@OverridePublicStringTostring(){Return"Hello";}}PublicClassT{PublicStaticvoidMain(String[]Args)ThrowsException{Class<?>C=Class.Forname("Com.itmyhome.Demo");MethodMt=C.GetMethod("ToString");Find the ToString MethodWhether the specified comment exists on this elementif (mt. Isannotationpresent (meaning. Classmeaning m = mt getannotation (meaning. Class//get specified annotation system.. (m. Value//get annotation value } } }
@Target
Indicates the kind of program element that the annotation type applies to. If a Target meta comment does not exist in the annotation type declaration, the declared type can be used on either program element.
If such meta-annotations exist, the compiler enforces the specified usage restrictions, such as: @Target (Elementtype.annotation_type)
ElementType Range of storage
Copyright NOTICE: This article uses the BY-NC-SA agreement to authorize, reproduced the Wheat Field Technical blog article please indicate the source
Original address: Http://itmyhome.com/2015/03/java-study-notes-annotation
Java Learning Note 11--annotation