Previous blog http://blog.csdn.net/u010590685/article/details/47029447 introduced the basic knowledge of Java annotations Today we learn how to use custom annotations.
First we will declare an annotation with the following declaration code:
Importjava.lang.annotation.Documented;ImportJava.lang.annotation.ElementType;Importjava.lang.annotation.Inherited;ImportJava.lang.annotation.Retention;ImportJava.lang.annotation.RetentionPolicy;ImportJava.lang.annotation.Target;@Target(Elementtype.type)//The scope of the annotation is where the annotation is used@Retention(Retentionpolicy.runtime)//The level of annotations, that is, when the annotations can be retained@Documented@Inherited Public@ interface myannaation { PublicStringvalue();//annotations can receive parameters}
There are four annotations on our statement. What do they do for each other?
@Target:
@Target illustrates the range of objects that annotation modifies: annotation can be used for packages, types (classes, interfaces, enumerations, annotation types), type members (methods, constructor methods, member variables, enumeration values), Method parameters and local variables (such as loop variables, catch parameters). Target can be more clearly decorated by using target in the declaration of the annotation type.
Function: Used to describe the scope of use of annotations (i.e., where the annotations described can be used)
The values (ElementType) are:
1.CONSTRUCTOR: Used to describe the constructor
2.FIELD: Used to describe the domain
3.local_variable: Used to describe local variables
4.METHOD: Used to describe the method
5.PACKAGE: Used to describe the package
6.PARAMETER: Used to describe parameters
7.TYPE: Used to describe classes, interfaces (including annotation types) or enum declarations
@Retention:
@Retention defines how long the annotation is retained: Some annotation appear only in the source code and are discarded by the compiler, while others are compiled in the class file The annotation that are compiled in the class file may be ignored by the virtual machine, while others will be read when class is loaded (note that it does not affect class execution because annotation is separated from the class in use). Using this meta-annotation can limit the "life cycle" of annotation.
Function: Indicates the level at which the annotation information needs to be saved to describe the life cycle of the annotation (i.e., the scope of the annotation being described is valid)
The values (Retentionpoicy) are:
1.SOURCE: Valid in source file (i.e., source file retention)
2.CLASS: Valid in class file (that is, class reserved)
3.RUNTIME: Valid at run time (that is, runtime retention)
@Documented:
@Documented used to describe other types of annotation that should be used as public APIs for annotated program members, so they can be documented by tools such as Javadoc. Documented is a markup annotation with no members.
@Inherited:
@Inherited meta-annotation is a markup annotation, @Inherited illustrates that a type that is labeled is inherited. If a annotation type that uses the @inherited modifier is used for a class, the annotation will be used for subclasses of that class.
Note: Annotations on interfaces and methods cannot be inherited
Let's take a look at the parameter types that the annotations can accept, we only have one parameter in the code above, and here we have to note that when only one parameter is the parameter name defaults to value, so that when we use it again , we can go directly to it without having to thank the parameter name, and we'll discuss it later. Let's look at the types of 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. Arrays of all types above
All right, here's the note. The next time we'll talk about how we can implement the features we want after the annotations are declared. By the time you need to use the reflection mechanism of Java, interested students can first understand.
Copyright NOTICE: This article is the original blog article, reproduced please indicate the source
Remarks on "Interview plus sub-item" Java Custom annotations