Summary of basic concepts of Java annotations

Source: Internet
Author: User

The concept of annotations

Annotations (Annotation), also called metadata (Metadata), are a new feature of Java5, and JDK5 introduces Metadata easily to invoke annotations. Annotations are used in the same hierarchy as classes, interfaces, enumerations, and can be applied to packages, types, construction methods, methods, member variables, parameters, and declarations of local variables to annotate these elements.

Syntax and definition form of annotations

(1) define with @interface keyword
(2) annotations contain members, and members are declared in the form of a parameterless method. Its method name and return value define the name and type of the member.
(3) The member assignment is in the form of @annotation (Name=value).
(4) Annotations need to indicate the life cycle of the annotations, the modified target of the annotations, etc., which are realized through meta annotations.

The Target annotations defined in Java.lang.annotation illustrate:

[Java]View Plain copy
    1. @Retention (value = retentionpolicy.runtime)
    2. @Target (value = {Elementtype.annotation_type})
    3. Public @interface Target {
    4. Elementtype[] Value ();
    5. }

The source code analysis is as follows:
First: Meta annotation @retention, the value of member value is retentionpolicy.runtime.
Second: meta-annotation @target, member value is a number of groups, with the form of {}, the value is Elementtype.annotation_type
Third: The member name is value and the type is elementtype[]
Also, be aware that if the member name is value, it can be abbreviated during the assignment. You can also shorthand if the member type is an array, but only one element is assigned. As the abbreviated form above is:

[Java]View Plain copy
    1. @Retention (Retentionpolicy.runtime)
    2. @Target (Elementtype.annotation_type)
    3. Public @interface Target {
    4. Elementtype[] Value ();
    5. }

Classification of annotations

There are two types of classification of annotations:

The first method of division

1, the basic built-in annotations, refers to Java comes with a few annotation, such as @override, Deprecated, @SuppressWarnings, etc.;

2, meta-annotations (meta-annotation), is responsible for annotating the annotations of other annotations, JDK 1.5 and later versions define 4 standard meta-annotation types, as follows:

    • @Target
    • @Retention
    • @Documented
    • @Inherited

3, custom annotations, as needed can be customized annotations, custom annotations need to use the above meta-annotation

The second method of division

Annotations need to indicate the life cycle of the annotations, which are implemented through the meta-annotations @Retention , and the values of the annotations are retentionpolicyof the enum type, including the following:

[Java]View Plain copy
  1. Public enum Retentionpolicy {
  2. /** 
  3. * Annotations are retained only in the source file, and the annotations are discarded when the Java file is compiled into a class file.
  4. * This means: annotation only exists during compiler processing, and after the compiler finishes processing, the annotation is useless.
  5. */
  6. SOURCE,
  7. /** 
  8. * Annotations are persisted to the class file, but the JVM is discarded when loading the class file, which is the default life cycle.
  9. */
  10. CLASS,
  11. /** 
  12. * Annotations are not only saved to the class file, but are still present after the JVM loads the class file.
  13. * Saved to class object, can be obtained by reflection
  14. */
  15. RUNTIME
  16. }

Meta annotations

As described above, Java defines 4 standard meta-annotations:

@Documented: Markup annotations, which describe other types of annotations that should be used as public APIs for annotated program members, can be documented by tools such as Javadoc.

[Java]View Plain copy
    1. @Documented
    2. @Retention (Retentionpolicy.runtime)
    3. @Target (Elementtype.annotation_type)
    4. Public @interface documented {
    5. }

@Inherited: Tagging annotations , allowing subclasses to inherit annotations from the parent class

[Java]View Plain copy
    1. @Documented
    2. @Retention (Retentionpolicy.runtime)
    3. @Target (Elementtype.annotation_type)
    4. Public @interface inherited {
    5. }

@Retention: Refers to the length of time annotation is retained, indicating the life cycle of the annotation, 3 kinds of retentionpolicy value meaning above to explain

[Java]View Plain copy
    1. @Documented
    2. @Retention (Retentionpolicy.runtime)
    3. @Target (Elementtype.annotation_type)
    4. Public @interface Retention {
    5. Retentionpolicy value ();
    6. }

@Target: A decorated target that indicates the annotation, a common

[Java]View Plain copy
    1. @Documented
    2. @Retention (Retentionpolicy.runtime)
    3. @Target (Elementtype.annotation_type)
    4. Public @interface Target {
    5. Elementtype[] Value ();
    6. }
    7. ElementType value
    8. Public enum ElementType {
    9. /** class, interface (including annotation type) or enumeration */
    10. TYPE,
    11. /** field property, also includes annotations used by enum constants * /
    12. FIELD,
    13. /** method * /
    14. METHOD,
    15. /** Parameters * /
    16. PARAMETER,
    17. /** Constructor * /
    18. CONSTRUCTOR,
    19. /** Local Variables * /
    20. Local_variable,
    21. meta-annotations used on/** annotations * /
    22. Annotation_type,
    23. /** bag * /
    24. Package
    25. }

Summary of basic concepts of Java annotations

Related Article

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.