Meaning of Java annotations retention, documented, and target

Source: Internet
Author: User

retention annotations

Retention (reserved) Note that this type of annotation is retained at that stage. There are three values:
1.retentionpolicy.source--This type of annotations is preserved only at the source level and is ignored at compile time
2.retentionpolicy.class--This type of annotations is preserved at compile time and exists in the CLASS file, but the JVM ignores
3.retentionpolicy.runtime--This type of annotations will be retained by the JVM, so they can be read and used by the JVM or other code that uses the reflection mechanism at runtime.

In the following example, the @Retention (retentionpolicy.runtime) Note indicates that the test_retention annotation will be retained by the virtual machine so that it can be read by reflection at run time.

Documented annotations

Documented annotations indicate that this annotation should be recorded by the Javadoc tool. By default, Javadoc is not included in annotations. However, if a @Documented is specified when the annotation is declared, it is processed by a tool such as Javadoc, so the annotation type information is also included in the generated document. (Personal point of view: not the focus, understand it.) Do not spray)

Target annotations

@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

inherited annotations

This is a slightly more complex type of annotation. It indicates that the annotated class is automatically inherited. More specifically, if you define annotations with a @Inherited tag and then label another parent class with a defined annotation, and the parent class has a subclass (subclass), all the properties of the parent class are inherited to its subclasses.

Meaning of Java annotations retention, documented, and target

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.