Java Annotations Detailed

Source: Internet
Author: User

Java1.5 introduced annotations, which are embedded in the program as metadata for the program. Annotations can be parsed by a number of parsing tools or compilation tools. We can also declare annotations to work during compilation or execution.

To create a Java custom annotation:

 Packagecom.click369;Importjava.lang.annotation.Documented;ImportJava.lang.annotation.ElementType;Importjava.lang.annotation.Inherited;Importjava.lang.annotation.Retention;ImportJava.lang.annotation.RetentionPolicy;ImportJava.lang.annotation.Target, @Documented @target (Elementtype.method) @Inherited @retention ( Retentionpolicy.runtime) Public@InterfaceMethodInfo {String author ()default"Author";        String date (); intVersion ()default1; String comments ();}

1. Annotation methods cannot have parameters.

2. The annotation method returns a value type qualified as: base type, String, Enums, annotation, or an array of these types.

3. The annotation method can have a default value.

4. Annotations themselves can contain meta-annotations, and meta-annotations are used to annotate other annotations.

Four types of meta-annotations:

[Email protected] indicates that the element that owns the annotation can be documented by Javadoc such a tool. This type should be used to annotate types that affect customers ' use of annotated element declarations.

If a declaration is annotated with documented, this type of annotation is used as the public API for the annotated program member.

[Email protected] indicates the scope of the program element to which annotations of that type can be annotated. The value of this meta-annotation can be type,method,constructor,field and so on. If the target element note

The solution does not appear, then the defined annotations can be applied to any element of the program.

[Email protected] indicates that the annotation type is automatically inherited. If the user queries this meta-annotation type in the current class and the declaration of the current class does not contain the meta-annotation type, it will also automatically check

Queries the parent class of the current class for the existence of a inherited element annotation, which will be repeated until the callout type is found, or the parent class of the top level is queried.

[Email protected] Indicates the length of time that the note was retained. The Retentionpolicy value is source, CLASS, RUNTIME.

Java built-in annotations:

[Email protected] when we want to make a copy of the parent class, we need to use annotations to tell the compiler that we want to replicate this method. This allows the compiler to prompt for an error message when a method in the parent class is removed or a change occurs.

[Email protected] we should use this annotation when we want the compiler to know that a method is not recommended for use. Java recommends the use of this annotation in Javadoc, and we should provide an alternative to why the method is not recommended for use.

[Email protected] This is just telling the compiler to ignore specific warning messages, such as using native data types in generics. His retention policy is source and is discarded by the compiler.

Java Annotations Detailed

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.