Java Custom Annotations

Source: Internet
Author: User

It is included in the Java.lang.annotation package.

1. Meta-annotations refer to annotations. including @Retention @Target @Document @Inherited four kinds.

1.1. @Retention: Define retention policies for annotations

@Retention (Retentionpolicy.source)//annotations only exist in the source code and do not contain @Retention (retentionpolicy.class)//default retention policy in the CLASS bytecode file. Annotations exist in a class bytecode file, but are not available at runtime, @Retention (Retentionpolicy.runtime)//annotations exist in a class bytecode file and can be obtained at runtime by reflection at 1.2, @ Target: Defines the purpose of the annotations @target (Elementtype.type)//interfaces, classes, enumerations, annotations @target (Elementtype.field)//fields, enumerated constants @target ( Elementtype.method)//Method @target (Elementtype.parameter)//method parameter @target (elementtype.constructor)//Constructor @target ( elementtype.local_variable)//local variable @target (elementtype.annotation_type)//annotation @target (elementtype.package)///package 1.3, @ Document: Note that the note will be included in Javadoc 1.4, @Inherited: The description subclass can inherit from the note in the parent class notice: @Inherited annotation Type is inherited by subclasses of the class being labeled. The annotation class does not inherit from the interface it implements, and the method does not inherit annotation from the method it overloads.

Custom annotations:

When you use @interface to customize annotations, the Java.lang.annotation.Annotation interface is automatically inherited and other details are automatically completed by the compiler. When you define annotations, you cannot inherit other annotations or interfaces. @interface is used to declare an annotation, in which each method actually declares a configuration parameter. The name of the method is the name of the parameter, and the return value type is the type of the parameter (the return value type can only be base type, Class, String, enum). You can declare the default value of a parameter through default.

  To define the annotation format:
Public @interface annotation name {definition body}

  Supported data types for annotation parameters:

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. Arrays of all types above

How to set the parameters of the annotation type:
First, it can only be decorated with public or default access rights. For example, String value (); Here the method is set to Defaul default type;
Second, parameter members can only use basic types Byte,short,char,int,long,float,double,boolean eight basic data types and string,enum,class,annotations data types, As well as these types of arrays. For example, String value (), where the parameter member is string;
Thirdly, if there is only one parameter member, it is best to set the parameter name to "value", followed by parentheses. Example: The following example Fruitname annotation has only one parameter member.

Default values for annotation elements:

  The annotation element must have a definite value, either specified in the default value of the definition annotation, or specified when the annotation is used, and the value of a non-basic type of annotation element cannot be null. Therefore, using an empty string or 0 as the default value is a common practice. This constraint makes it difficult for the processor to represent the presence or absence of an element, because in each annotation declaration, all elements are present and have corresponding values, in order to circumvent this constraint, we can only define special values, such as an empty string ("") or a negative number (-1), one time to indicate that an element does not exist, This has become a customary usage when defining annotations.

Java Custom Annotations

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.