Getting Started with java-annotation annotations

Source: Internet
Author: User

Java annotations provide some information about the code, but do not directly work on the code content that it is annotated with. In this tutorial, we will learn about Java annotations, how to customize annotations, how annotations are used, and how to interpret annotations through reflection.

Java1.5 introduces annotations, many of which are currently used in numerous Java frameworks, such as Hibernate, Jersey, and Spring. Annotations are embedded in the program as metadata for the program. Annotations can be parsed by some parsing tools or by a compilation tool. We can also declare annotations to work during compilation or execution.

Before using annotations, the program source data is only available through Java annotations and Javadoc, but annotations provide far more functionality than these. Annotations not only contain metadata, it can also be used in the process of running the program, the annotation interpreter can use annotations to determine the order of execution of the program. For example, in Jersey WebService we add a URI string as a **path** annotation in the form of a method, the Jerser interpreter determines the method to invoke the given URI during the program's run.

Create Java Custom annotations

Creating custom annotations is similar to creating an interface, but the interface keyword for annotations needs to start with the @ sign. We can declare methods for annotations.

    • Annotation methods cannot have parameters;
    • The annotation method return value type is limited to: base type, String, Enums, annotation, or an array of these types;
    • Annotation methods can have default values;
    • Annotations themselves can contain meta-annotations, and meta-annotations are used to annotate other annotations.

Here are four types of meta-annotations:

1. @Documented -Indicates that the element that owns this 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.

2. @Target-Indicates the scope of the program element to which the annotation 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 annotations do not appear, then the defined annotations can be applied to any element of the program.

3. @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 label type is found, Or a query to the top-level parent class.

4.@Retention-Indicates the length of time that the annotation was retained. The Retentionpolicy value is source,class,runtime.

Java built-in annotations

Java provides three built-in annotations.

1. @Override-when we want to replicate a method in a parent class, we need to use that note 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.

2. @Deprecated-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.

3. @SuppressWarnings-This simply tells the compiler to ignore specific warning messages, such as using native data types in generics. Its retention policy is source (the translator notes: Valid in the source file) and is discarded by the compiler.

Getting Started with java-annotation 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.