Java Learning Note 11--annotation
Annotation: A new feature added after JDK1.5, known as the metadata feature, is called a comment after JDK1.5, that is, the use of 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 ();
}
[Java]View Plaincopy
- Public @interface meaning {
- String value ();
- }
After that, you can use the @meaning format directly in your program.
[Java]View Plaincopy
- @Meaning (value="Itmyhome")
- Class demo{
- }
You can set a parameter in annotation to receive the contents of the variable, such as value above, and when using annotation, you must assign a value to the parameter such as: Value= "Itmyhome"
Since you can set a parameter, you can also set multiple parameters.
[Java]View Plaincopy
- Public @interface meaning {
- String key ();
- String value ();
- }
This annotation need to set two parameters when using, a key one value
[Java]View Plaincopy
- @Meaning (key="Hi", value="Itmyhome")
- Class demo{
- }
You can also set an array in
[Java]View Plaincopy
- Public @interface meaning {
- String[] Value ();
- }
The received content to pass the array
[Java]View Plaincopy
- @Meaning (value={"Hello","World"})
- Class demo{
- }
There is a feature in all the annotation defined above, all the parameter contents need to be set up when using the annotation, then you can set the default content for a parameter, use default when declaring.
[Java]View Plaincopy
- Public @interface meaning {
- String value () default ""; //default is empty
- }
You can not set a value when you use it
[Java]View Plaincopy
- @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.
[Java]View Plaincopy
- Public enum Formitemtype { //define enum type
- Hidden,text,select,date
- }
define Annotation
[Java]View Plaincopy
- Public @interface meaning {
- Formitemtype value (); //Set to enum type
- }
The value of the annotation can only be a value in the enumeration type
[Java]View Plaincopy
- @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:
[Java]View Plaincopy
- @Retention (Retentionpolicy.runtime)
- @Target (Elementtype.field)
- Public @interface meaning {
- Formitemtype value (); //Set to enum type
- }
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
[Java]View Plaincopy
- Package com.itmyhome;
- Import java.lang.annotation.Annotation;
- Import Java.lang.reflect.Method;
- Class demo{
- @SuppressWarnings ("unchecked")
- @Deprecated
- @Override
- Public String toString () {
- return "Hello";
- }
- }
- Public class T {
- public static void Main (string[] args) throws exception{
- class<?> C = class.forname ("Com.itmyhome.Demo");
- Method MT = C.getmethod ("toString"); //Find the ToString method
- Annotation an[] = mt.getannotations (); //Get all the annotation
- For (Annotation a:an) {
- System.out.println (a);
- }
- }
- }
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
[Java]View Plaincopy
- @Retention (Retentionpolicy.runtime)
- @Target (Elementtype.method)
- Public @interface meaning {
- Formitemtype value (); //Set to enum type
- }
[Java]View Plaincopy
- Package com.itmyhome;
- Import Java.lang.reflect.Method;
- Class demo{
- @Meaning (Value=formitemtype.select) //Custom annotation
- @SuppressWarnings ("unchecked")
- @Deprecated
- @Override
- Public String toString () {
- return "Hello";
- }
- }
- Public class T {
- public static void Main (string[] args) throws exception{
- class<?> C = class.forname ("Com.itmyhome.Demo");
- Method MT = C.getmethod ("toString"); //Find the ToString method
- //The specified comment is present on this element
- if (mt.isannotationpresent (meaning. Class)) {
- meaning M = mt.getannotation (meaning. Class); //Get the specified annotation
- System.out.println (M.value ()); //Get the value of annotation
- }
- }
- }
@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
Java Learning Note 11--annotation