Introduction to custom annotation based on Java annotation

To deeply study annotations, we must be able to define our own annotations and use them. Before defining our own annotations, we must understand the meta annotations and related syntax for defining annotations provided by Java.

--------------------------------------------------------------------------------

Meta annotation:

The function of meta annotation is to annotate other annotations. Java5. 0 defines four standard meta annotation types, which are used to describe other annotation types. Java5. Meta annotation defined by 0: 1@ Target,     2.@ Retention,     3.@ Documented,     4.@ Inherited these types and the classes they support are in Java You can find it in the lang.annotation package. Let's take a look at the function of each meta annotation and the instructions for the use of the corresponding sub parameters.

--------------------------------------------------------------------------------

  @Target:

@ target describes the object range modified by annotation: annotation can be used for packages Types (class, interface, enumeration, annotation type), type members (method, constructor, member variable, value), method parameters and local variables (such as loop variable and catch parameter). Using target in the declaration of annotation type can clarify the target of its modification.

Function: used to describe the scope of use of annotations (i.e. where the described annotations can be used)

The values (ElementType) are:

    1. Constructor: used to describe constructor 2 Field: used to describe field 3 LOCAL_ Variable: used to describe local variables 4 Method: used to describe method 5 Package: used to describe package 6 Parameter: used to describe parameter 7 Type: used to describe class, interface (including annotation type) or enum declaration

Usage example:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>