Java Annotations (Annotation) detailed

Source: Internet
Author: User

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
  1. Package diydescription;
  2. Import java.lang.annotation.Documented;
  3. Import Java.lang.annotation.ElementType;
  4. Import java.lang.annotation.Inherited;
  5. Import java.lang.annotation.Retention;
  6. Import Java.lang.annotation.RetentionPolicy;
  7. Import Java.lang.annotation.Target;
  8. @Target ({elementtype.method,elementtype.type})
  9. @Retention (Retentionpolicy.runtime)
  10. @Inherited
  11. @Documented
  12. Public @interface Description { //Use @interface keyword to define annotations
  13. //members declared with no parameters and no exception
  14. String desc ();
  15. /* String desc (int a);
  16. * String Desc () throws Exception;
  17. * All the wrong way of declaring
  18. */
  19. String author ();
  20. //String author () default ""; Legal Statements
  21. //You can specify a default value for a member with default
  22. int Age () default 18;
  23. /* 
  24. * If stated: Map map (); The error will be:
  25. * Invalid type Map for the annotation attribute description.map;
  26. * Only primitive type, String, Class, annotation, enumeration
  27. * is permitted or 1-dimensional arrays thereof
  28. *
  29. * Only the original type and string, Class, annotation, enumeration can
  30. */
  31. }
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
  1. Package Jtzeng;
  2. Import Java.lang.annotation.ElementType;
  3. Import Java.lang.annotation.Target;
  4. @Target ({elementtype.type})
  5. Public @interface SingleValue1 {
  6. String desc ();
  7. }
  8. Package Jtzeng;
  9. Import Java.lang.annotation.ElementType;
  10. Import Java.lang.annotation.Target;
  11. @Target ({Elementtype.method})
  12. Public @interface SingleValue2 {
  13. String value ();
  14. }
  15. Package Jtzeng;
  16. @SingleValue1 (desc = "This is a type annotation") //Use to indicate the member name and assignment number "="
  17. Public class Test {
  18. @SingleValue2 ("This is a method note") //can be ignored when using the member name and assignment number "="
  19. public void print () {
  20. System.out.println ();
  21. }
  22. }
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
    1. PACKAGE JTZENG;  
    2. IMPORT JAVA.LANG.ANNOTATION.ELEMENTTYPE;  
    3. IMPORT JAVA.LANG.ANNOTATION.TARGET;  
    4.   
    5. @Target ({Elementtype.type,elementtype.field})   
    6. public  @interface  Description {    
    7.   
    8.     string desc ();   
    9.     string author ();   
    10.      int age ()  default  21;  
    11. }   
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
  1. Package Jtzeng;
  2. Import java.lang.annotation.Retention;
  3. Import Java.lang.annotation.RetentionPolicy;
  4. @Retention (Retentionpolicy.runtime) //runtime present, can be read by reflection
  5. @Retention (Retentionpolicy.source)//is only displayed in the source code and will be lost at compile time
  6. @Retention (Retentionpolicy.class)//compile time is recorded in CLASS, run-time ignored
  7. Public @interface Description {
  8. String desc ();
  9. String author () default "Jtzeng";
  10. int Age () default 21;
  11. }
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
    1. PACKAGE JTZENG;  
    2. IMPORT JAVA.LANG.ANNOTATION.INHERITED;  
    3.   
    4. @Inherited   
    5. public  @interface  description {    
    6.     string desc ();   
    7.     string author ()  default  "Jtzeng";   
    8.     int  age ()  default 21;  
    9. }  
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
  1. /*
  2. * Test the function of @documented
  3. */
  4. Package Jtzeng;
  5. Import java.lang.annotation.Documented;
  6. Import Java.lang.annotation.ElementType;
  7. Import java.lang.annotation.Inherited;
  8. Import java.lang.annotation.Retention;
  9. Import Java.lang.annotation.RetentionPolicy;
  10. Import Java.lang.annotation.Target;
  11. @Target ({elementtype.method,elementtype.type})
  12. @Retention (Retentionpolicy.runtime)
  13. @Inherited
  14. @Documented
  15. Public @interface Description {
  16. String desc ();
  17. String author () default "Jtzeng";
  18. int Age () default 21;
  19. }
  20. /*
  21. * Define a test class with annotations for classes and methods
  22. */
  23. Package Jtzeng;
  24. @Description (desc="This is type annotation", author="Jtzeng", age=)
  25. Public class Test {
  26. private String field = "Custom annotation";
  27. @Description (desc="This is the method annotation", author="Jtzeng", age=)
  28. public void print () {
  29. System.out.println (field);
  30. }
  31. }
/* * 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.