Java Programming thought Lesson (VII) 20th chapter-Annotations

Source: Internet
Author: User

Annotations (also known as metadata ) provide a formalized way for us to add information to our code so that we can use it very conveniently at a later point in time.

1 Basic syntax

The annotated method does not differ from the other methods. Annotations can work with any modifier on a method, such as public, static, or void. From a grammatical point of view, annotations are used almost exactly the same way as modifiers.

1.1 Defining annotations

The definition of an annotation looks much like the definition of an interface. In fact, as with any other Java interface, annotations will be compiled into class files .

Some meta annotations (meta-annotation)are required when defining annotations, such @Target as @Retention .

In annotations, some elements are typically included to represent certain values . These values are available to the program or tool when parsing the annotations. The annotated element looks like the method of the interface, and the only difference is that you can specify a default value for it. There are restrictions on the types of elements.

Annotations without elements are called tag annotations (marker annotation).

All annotations are inherited annotation .

Package Net.mrliuli.annotations;import java.lang.annotation.*; @Target (Elementtype.method) @Retention ( retentionpolicy.runtime) public @interface Test {}//(marker annotation)

1.2 Three standard annotations and four types of meta annotations

java.langthree standard annotations defined in:

    • @Overrided

    • @Deprecated

    • @SuppressWarnings

Four types of meta-annotations:

    • @Target indicates where the annotation can be used.

    • @Retension indicates at what level the annotation information needs to be saved.

    • @Documented include this annotation in Javadoc.

    • @Inherited allow subclasses to inherit annotations from the parent class.

1.3 About annotations

    • There is a restriction on the type of the annotation element, which cannot be any type, and a compiler with a type other than the allowed type will give an error.

    • Default value limit:

      @Target (Elementtype.method) @Retention (retentionpolicy.runtime) public @interface the simulationnull{public    int ID ( ) default-1;    Public String description () Default "";}
      • First, the element cannot have an indeterminate value. In other words, the element must either have a default value or provide the value of the element when using annotations.

      • Second, for elements of a non-primitive type, either when declared in the source code or when a default value is defined in the annotation interface, it cannot be null used as its value. This constraint makes it difficult for the annotation processor to represent the presence or absence of an element, because in the declaration of each annotation, all elements are present and have corresponding values. To circumvent this constraint, we can only define some special values, such as an empty string or a negative number, to indicate that an element does not exist:

    • Generates an external file. Some frameworks require some additional information to work with your source code, which is best suited for annotations to show their value. Technologies such as enterprise JavaBean need to deploy a profile. Web Service, custom tag libraries, and object/relational mapping tools, such as TopLink and hibernate, typically require XML profiles that are detached from the source code.

2 Writing the annotation processor

An important part of the process of using annotations is creating and using the note processor , which is used to read the annotations .

    • The Reflection mechanism API can construct the annotation processor

    • External tool apt can parse Java source code with annotations

Related articles:

Java Programming Thought Lesson (v) 18th Chapter-java IO system

Java Programming thought Lesson (vi) 19th-enumeration type

Related Article

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.