How to define custom annotations in Java

Source: Internet
Author: User

Understanding Annotations

Annotations are java1.5,jdk5.0 referenced techniques, with classes, interfaces, and enumerations at the same level. It can be declared in front of packages, classes, fields, methods, local variables, method parameters, and so on, to explain these elements, comments.

In Java, there are three annotations, these three annotations exist in the Java.lang package, first of all we talk about these annotations

    The
    • override--its role is to mark methods that override methods in the superclass, and if the tagged class does not actually overwrite the superclass, the compiler issues an error warning.  
    • deprecated-- Its role is to add annotations to methods that should not be reused, and when programmers use these methods, they will display a hint message at compile time  
    • suppresswarnings--This simply tells the compiler to ignore specific warning messages, such as using the native data type   in generics;

      The parameter values commonly used in this comment are: deprecation (ignoring the use of outdated classes or methods), unchecked (ignoring the failure to perform an unchecked reload warning), Fallthrough ( Ignoring switch directly points to the next case block without a break warning), path (ignoring the classpath, warning if there is no path in the source file path), serial (ignoring the warning when no serialversionuid is present in the Serializable class), finally ( Any warning when finally does not execute correctly), all (above)
notes on customizing annotations

First of all, custom annotations We must understand four meta-annotations, what is meta-annotations? Meta-annotations refer to the meta-data or meta-information that acts on the annotations, which is simple and popular, and the meta-annotations are annotations.

    • documented--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.
    • inherited--indicates that the annotation type is automatically inherited. If the user queries the meta-annotation type in the current class and the declaration of the current class does not contain the meta-annotation type, the current class's parent class is automatically queried for the existence of the inherited element annotation, which is repeated to know that the callout type is found, or the parent class that queries the top level.
    • retention--indicates at what level this annotation is displayed
    • target--indicates the range of program elements to which annotations of this type can be annotated

Documented and inherited are typical identifiers, meaning there is no member variable inside the annotation, and annotations without member variables are called identity annotations
Target main parameter types include the following

    • Elementtype.type for classes, interfaces, enumerations, but not annotations
    • Elementtype.field action on a field that contains an enumeration value
    • Elementtype.method acting on methods, not including construction methods
    • Elementtype.parameter parameters that act on a method
    • Elementtype.constructor acting on the construction method
    • Elementtype.local_veriable action on local variables or catch statements
    • Elementtype.annotation_type Acting on annotations
    • Elementtype.package acting on the package

Retention The main parameter types include the following

    1. Retentionpolicy.source annotations are present in the source code and are discarded at compile time
    2. Retentionpolicy.class annotations are compiled into the CLASS file, but the JVM ignores
    3. The Retentionpolicy.runtime JVM reads annotations and is saved to the class file

Custom annotations

Definition of annotations

The data types that can be defined in annotations are restricted, except for the primitive types, string,enums,annotation,class and arrays of these types

Use of annotations

Parsing of annotations

How to define 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.