[Additional Points for the interview] java custom annotation declarative annotation, java custom

Source: Internet
Author: User

[Additional Points for the interview] java custom annotation declarative annotation, java custom

Previous blog posts.
First, we need to declare an annotation. The declaration code is as follows:

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. TYPE) // The scope of the annotation, that is, where the annotation is used @ Retention (RetentionPolicy. RUNTIME) // The annotation level, that is, when the annotation can be stored @ brief ented @ Inheritedpublic @ interface MyAnnaation {public String value (); // The parameter that the annotation can receive}

There are four annotations on the annotations we declare. What are they doing?

@ Target:

@ Target indicates the range of objects modified by Annotation: Annotation can be used for packages, types (class, interface, enumeration, Annotation type) type members (methods, constructor methods, member variables, enumerated values), method parameters, and local variables (such as cyclic variables and catch parameters ). In the Annotation type declaration, target can be used to better clarify the object to be modified.

Purpose: Describe the scope of use of the annotation (that is, where the description can be used)

Values:

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 the Retention duration of the Annotation: Some Annotation only appears in the source code and is discarded by the compiler; others are compiled in the class file; annotation compiled in the class file may be ignored by the virtual machine, while others will be read when the class is loaded (Please note that the execution of the class is not affected, because Annotation and class are separated in use ). This meta-Annotation can be used to limit the "Life Cycle" of Annotation.

Purpose: indicates the level at which the annotation information needs to be saved and used to describe the lifecycle of the annotation (that is, within the scope of the description)

Values (RetentionPoicy) include:

1. SOURCE: valid in the SOURCE file (that is, the SOURCE file is retained)
2. CLASS: valid in the class file (that is, the class is retained)
3. RUNTIME: valid during RUNTIME (that is, reserved during RUNTIME)
  

@ Brief ented:

@ Brief ented is used to describe other types of annotation which should be used as the public API of the labeled Program Member. Therefore, it can be Documented by tools such as javadoc. Incluented is a tag Annotation with no members.

@ Inherited:

@ Inherited meta annotation is a tag annotation. @ Inherited describes that a labeled type is Inherited. If an annotation type modified with @ Inherited is used for a class, this annotation will be used for the subclass of this class.
NOTE: Annotations on interfaces and methods cannot be inherited.

Next let's take a look at the acceptable parameter types of the annotation. In the above Code, we only have one parameter. Here we wantNote that only one parameter is named value by default, so you do not need to thank the parameter name when using it again.. The following describes the parameter types:
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. All types of arrays above
The annotation statement is here. Next we will discuss how we implement the functions we want after the annotation statement. At that time, we need to use the java reflection mechanism. If you are interested, you can first understand it.

Copyright Disclaimer: This article is an original article by the blogger. For more information, see the source.

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.