Custom annotations in Java

Source: Internet
Author: User

syntax requirements for custom annotations

  

The keyword defining a custom annotation is @interface

Members are declared with no exception, can assign a default value to the member (default), the type of the member is restricted, the legal type has the basic data type and string,class,annotation,enumeration; If the annotation has only one member, it must be named value (), and the member name and "=" can be ignored when used

Note Classes can have no members, which is called identity annotations

__________________________________________________________________________

Meta annotations

Java5.0 defines 4 standard meta-annotation types, which are used to provide descriptions of other annotation types.

The defined meta annotations are (java.lang.annotation):

@Target

@Retertion

@Inherited

@Documented

1) @Target: The scope of the annotation's modified random thoughts, that is, the scope of the modified annotations

The value of the ElementType is

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

PS: Separated by "," between each parameter

2) @Retertion: Defines the length of time that the annotation is retained, that is, the extent to which the described annotation is valid

Retentionpoicy) are:

1.SOURCE: Valid in source file (i.e., source file retention)
2.CLASS: Valid in class file (that is, class reserved)
3.RUNTIME: Valid at run time (that is, runtime retention)

3) @Inherited: Allow subclasses to be inherited

4) @Documented: 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. Documented is a markup annotation with no members.

__________________________________________________________________________

Working with custom annotations

Syntax:@< annotation name > (< member name 1>=< member value 1>,< member name 2>=< member value 3>,...)

  

Custom annotations in Java

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.