Custom annotations:
When you use @interface to customize annotations, the Java.lang.annotation.Annotation interface is automatically inherited and other details are automatically completed by the compiler. When you define annotations, you cannot inherit other annotations or interfaces. @interface used to declare an annotation, in which each method actually declares a configuration parameter. The name of the method is the name of the parameter, and the return value type is the type of the parameter (the return value type can only be the base type, Class, String, enum). You can declare the default value of a parameter via default.
To define the annotation format:
Public @interface Note name {definition body}
The supported data types for the annotation parameters:
1. All basic data Types (Int,float,boolean,byte,double,char,long,short)
2.String type
3.Class type
4.enum type
5.Annotation type
6. All of the above types of arrays
How to set the parameters of the annotation type:
First, only two access rights, public or default, can be decorated. For example, String value (), where the method is set to the Defaul default type;
Second, the parameter members can only use the basic type Byte,short,char,int,long,float,double,boolean eight kinds of basic data types and string,enum,class,annotations data types, And some of these types of arrays. For example, String value (), where the parameter member is string;
Third, if there is only one parameter member, it is best to set the parameter name to "value" and then add the parentheses. Example: The following example Fruitname annotation has only one parameter member.
Simple custom annotations and use annotation instances:
package annotation;
Import java.lang.annotation.Documented;
Import Java.lang.annotation.ElementType;
Import java.lang.annotation.Retention;
Import Java.lang.annotation.RetentionPolicy;
Import Java.lang.annotation.Target; /** * @author Peida * */@Target (Elementtype.field) @Retention (retentionpolicy.runtime) @Documented public @i Nterface Fruitname {String value () Default "";}
package annotation;
Import java.lang.annotation.Documented;
Import Java.lang.annotation.ElementType;
Import java.lang.annotation.Retention;
Import Java.lang.annotation.RetentionPolicy;
Import Java.lang.annotation.Target; /** * Fruit Color Annotation * @author Peida */@Target (Elementtype.field) @Retention (retentionpolicy.runtime) @Documented public @i
Nterface Fruitcolor {/** * Color enumeration * @author Peida * */public enum color{Bule,red,green};
/** * Color Property * @return/Color Fruitcolor () default color.green; }
package annotation;
import annotation. Fruitcolor.color;
public class Apple {
@FruitName ("Apple")
private String applename;
@FruitColor (fruitcolor=color.red)
private String AppleColor;
public void Setapplecolor (String applecolor) {
this.applecolor = AppleColor;
}
Public String Getapplecolor () {return
applecolor;
}
public void Setapplename (String applename) {
this.applename = applename;
}
Public String Getapplename () {return
applename;
}
public void DisplayName () {
System.out.println ("Fruit name is: Apple");
}
Default value for callout element:
The annotation element must have a definite value, either specified in the default value of the definition annotation, or specified when using the annotation, the value of the callout element of the non base type is not nullable. Therefore, it is a common practice to use an empty string or 0 as the default value. This constraint makes it difficult for the processor to represent the presence or absence of an element, because all the elements exist in the declaration of each annotation, and all have corresponding values, in order to circumvent this constraint, we can only define special values, such as an empty string or a negative number, indicating that an element does not exist at one time, when defining annotations, This has become a habitual usage. For example:
1 package annotation;
2 3 Import java.lang.annotation.Documented;
4 Import Java.lang.annotation.ElementType;
5 Import java.lang.annotation.Retention;
6 Import Java.lang.annotation.RetentionPolicy;
7 Import Java.lang.annotation.Target; 8 9/** 10 * Fruit Supplier NOTE * @author Peida * * @Target (Elementtype.field) @Retention (retentionpolicy.runti ME) @Documented public @interface Fruitprovider {18/** 19 * Supplier Number * * @return
LIC int id () default-1;
23 24/** 25 * Name of supplier * @return/Public String name () default "";
29 30/** 31 * Supplier Address * * @return * * The public String addresses () default "";
}