Ext.: http://blog.csdn.net/zen99t/article/details/49508447
(ii) Custom annotations
First a paragraph of the code has a general impression, and then slowly explain (in fact, the code comments have been explained very clearly):
[Java]View Plain Copy
- Package diydescription;
- Import java.lang.annotation.Documented;
- Import Java.lang.annotation.ElementType;
- Import java.lang.annotation.Inherited;
- Import java.lang.annotation.Retention;
- Import Java.lang.annotation.RetentionPolicy;
- Import Java.lang.annotation.Target;
- @Target ({elementtype.method,elementtype.type})
- @Retention (Retentionpolicy.runtime)
- @Inherited
- @Documented
- Public @interface Description { //Use @interface keyword to define annotations
- //members declared with no parameters and no exception
- String desc ();
- /* String desc (int a);
- * String Desc () throws Exception;
- * All the wrong way of declaring
- */
- String author ();
- //String author () default ""; Legal Statements
- //You can specify a default value for a member with default
- int Age () default 18;
- /*
- * If stated: Map map (); The error will be:
- * Invalid type Map for the annotation attribute description.map;
- * Only primitive type, String, Class, annotation, enumeration
- * is permitted or 1-dimensional arrays thereof
- *
- * Only the original type and string, Class, annotation, enumeration can
- */
- }
Package diydescription; Import java.lang.annotation.Documented; Import Java.lang.annotation.ElementType; Import java.lang.annotation.Inherited; Import java.lang.annotation.Retention; Import Java.lang.annotation.RetentionPolicy; Import Java.lang.annotation.Target; @Target ({elementtype.method,elementtype.type}) @Retention (retentionpolicy.runtime) @Inherited @Documented public @ Interface Description {//Use the @interface keyword to define annotations//Members to declare string desc () with no parameters and no exception;/* String desc (int a); * String desc () throws Exception; * All are the wrong way of declaring */String author (); String author () default ""; Legal declaration//You can specify a default value of int age () for a member with default 18; /* * If declared: Map map (); Will error: * Invalid type MAP for the annotation attribute description.map; * Only primitive type, String, Class, annotation, enumeration * is permitted or 1-dimensional arrays thereof * * Only primitive types and s Tring, Class, annotation, enumeration can */}
1. The syntax requirements for custom annotations First look at the following sections of the above code:
1.Use the @interface keyword to define annotations, note the location of keywords
2.Members are declared in a non-parametric, no-exception manner, noting that the declaration of a generic class member variable is distinguished
3.You can specify a default value for a member using default, as shown above
4.Member types are restricted, valid types include primitive types, and string, Class, Annotation, enumeration (there are 8 basic data types for Java: Byte (bytes), short (shorter), int (integer), Long (length ), float (single-precision floating-point number Type), double (double-precision floating-point number Type), char (character type), Boolean (Boolean type)
5.Note Classes can have no members, and annotations with no members are called identity annotations, such as @override in JDK annotations, @Deprecation
6.If the annotation has only one member and the member is named value (), the member name and assignment Number "=" can be ignored when used, such as the @suppvisewarnings of the JDK annotations, and if the member name is not value, the member name and the assignment number "=" are indicated when used. The example code is as follows:
[Java]View Plain Copy
- Package Jtzeng;
- Import Java.lang.annotation.ElementType;
- Import Java.lang.annotation.Target;
- @Target ({elementtype.type})
- Public @interface SingleValue1 {
- String desc ();
- }
- Package Jtzeng;
- Import Java.lang.annotation.ElementType;
- Import Java.lang.annotation.Target;
- @Target ({Elementtype.method})
- Public @interface SingleValue2 {
- String value ();
- }
- Package Jtzeng;
- @SingleValue1 (desc = "This is a type annotation") //Use to indicate the member name and assignment number "="
- Public class Test {
- @SingleValue2 ("This is a method note") //can be ignored when using the member name and assignment number "="
- public void print () {
- System.out.println ();
- }
- }
Package Jtzeng; Import Java.lang.annotation.ElementType; Import Java.lang.annotation.Target; @Target ({elementtype.type}) public @interface SingleValue1 {String desc (),} package Jtzeng; Import Java.lang.annotation.ElementType; Import Java.lang.annotation.Target; @Target ({Elementtype.method}) public @interface SingleValue2 {String value ()}, package Jtzeng; @SingleValue1 (desc = "This is a type annotation")//use to indicate the member name and assignment Number "=" public class Test {@SingleValue2 ("This is a method note")//use can omit member name and assignment number "=" public void print () {System.out.println ();}}
2. What is a meta-annotation? is the annotated annotation, which is to annotate your own definition of annotations, you define an annotation yourself, but what you want your annotations to have, you need to use meta-annotations to illustrate your annotations. There are 4 meta annotations, the upper part of the following code:
2.1. @TargetThat is, the scope of the annotation, which is used to illustrate the use of annotations (that is, where annotations can be used, such as class annotations, method annotations, member variable annotations, etc.)
Value:Elemenettype.constructor —————————-Constructor Declaration
Elemenettype.field ———————————— – Domain declarations (including enum instances)
Elemenettype.local_variable ————————-local variable declarations
Elemenettype.method ———————————-Method declaration
Elemenettype.package ——————————— Package Declaration
Elemenettype.parameter —————————— Parameter Declaration
Elemenettype.type ————————————— classes, interfaces (including annotation types) or enum declarations
usage Examples:First define a description annotation,
[Java]View Plain Copy
- PACKAGE JTZENG;  
- IMPORT JAVA.LANG.ANNOTATION.ELEMENTTYPE;  
- IMPORT JAVA.LANG.ANNOTATION.TARGET;  
-
- @Target ({Elementtype.type,elementtype.field})
- public @interface Description {
-   
- string desc ();
- string author ();
- int age () default 21;  
- }   
Package Jtzeng; Import Java.lang.annotation.ElementType; Import Java.lang.annotation.Target; @Target ({Elementtype.type,elementtype.field}) public @interface Description {String desc (); String author (); int age () default 21; }
Then define a test class for testing, in order to let you see the error, here is given in the picture. It can be found that since the annotations are defined above, the @Target contains only
Elemenettype.typeAnd
Elemenettype.field, the annotations in the class and declaration are possible, and the annotations on the methods are error-able. 2.2. What is the scope of the annotation that @Retention describes is valid.
The values are:Retentionpolicy.source ———————— – only displayed in the source code, will be lost at compile time
Retentionpolicy.class ————————— – The compile time is recorded in CLASS, and the runtime ignores
Retentionpolicy.runtime ————————-runtime exists and can be read by reflection
usage Examples:The following is a simple definition, as to what will be different effects, the future
parsing AnnotationsSection will explain.
[Java]View Plain Copy
- Package Jtzeng;
- Import java.lang.annotation.Retention;
- Import Java.lang.annotation.RetentionPolicy;
- @Retention (Retentionpolicy.runtime) //runtime present, can be read by reflection
- @Retention (Retentionpolicy.source)//is only displayed in the source code and will be lost at compile time
- @Retention (Retentionpolicy.class)//compile time is recorded in CLASS, run-time ignored
- Public @interface Description {
- String desc ();
- String author () default "Jtzeng";
- int Age () default 21;
- }
Package Jtzeng; Import java.lang.annotation.Retention; Import Java.lang.annotation.RetentionPolicy; @Retention (Retentionpolicy.runtime)//runtime present, can be read through reflection//@Retention (Retentionpolicy.source)//Only in the source display, compile will be lost//@ Retention (Retentionpolicy.class)//compile time is recorded in CLASS, the runtime ignores public @interface Description {String desc (); String author () default "Jtzeng"; int age () default 21; }
2.3. @Inherited1. is a markup annotation, without a member, that allows subclasses to inherit the annotation, that is, if a @inherited-modified annotation is used for a class, then the annotation will be inherited by the subclass of that class with 2. Annotations that use the @inherited modifier can only be inherited by the quilt class, and cannot inherit 3 from the interface it implements. When a subclass inherits annotations from a parent class, it cannot inherit annotations from the methods it overloads
usage Examples:
[Java]View Plain Copy
- PACKAGE JTZENG;  
- IMPORT JAVA.LANG.ANNOTATION.INHERITED;  
-   
- @Inherited
- public @interface description {
- string desc ();
- string author () default "Jtzeng";
- int age () default 21;  
- }  
Package Jtzeng; Import java.lang.annotation.Inherited; @Inherited public @interface Description {String desc (); String author () default "Jtzeng"; int age () default 21; }
2.4. @Documented @documented is a markup annotation with no members. 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. (A little abstract, see example)
usage Examples:
[Java]View Plain Copy
- /*
- * Test the function of @documented
- */
- Package Jtzeng;
- Import java.lang.annotation.Documented;
- Import Java.lang.annotation.ElementType;
- Import java.lang.annotation.Inherited;
- Import java.lang.annotation.Retention;
- Import Java.lang.annotation.RetentionPolicy;
- Import Java.lang.annotation.Target;
- @Target ({elementtype.method,elementtype.type})
- @Retention (Retentionpolicy.runtime)
- @Inherited
- @Documented
- Public @interface Description {
- String desc ();
- String author () default "Jtzeng";
- int Age () default 21;
- }
- /*
- * Define a test class with annotations for classes and methods
- */
- Package Jtzeng;
- @Description (desc="This is type annotation", author="Jtzeng", age=)
- Public class Test {
- private String field = "Custom annotation";
- @Description (desc="This is the method annotation", author="Jtzeng", age=)
- public void print () {
- System.out.println (field);
- }
- }
/* * Test @documented's function */package Jtzeng; Import java.lang.annotation.Documented; Import Java.lang.annotation.ElementType; Import java.lang.annotation.Inherited; Import java.lang.annotation.Retention; Import Java.lang.annotation.RetentionPolicy; Import Java.lang.annotation.Target; @Target ({elementtype.method,elementtype.type}) @Retention (retentionpolicy.runtime) @Inherited @Documented public @ Interface Description {String desc (); String author () default "Jtzeng"; int age () default 21; }/* Defines a test class, with annotations for classes and methods */package Jtzeng; @Description (desc= "This is type annotation", author= "Jtzeng", age=21) public class Test {private String field = "Custom Annotation"; @Description ( Desc= "This is the method annotation", author= "Jtzeng", age=21) public void print () {System.out.println (field);}}
Then, in Eclipse, right-click the project name, select Export, select Java-->javadoc, Next, finish. Look at the result, the left side is added @documented effect, the right is not added effect. Custom Annotations End ~ ~
Java Annotations (Annotation) detailed