Java Annotation Manual

Source: Internet
Author: User

Objective:

The introduction to Java annotation in the previous article outlines the definition and use of annotation, which covers a wide range, but is not deep enough. So the author in the "Java annotation," after continuing to organize the concept of annotation and knowledge, and like to share with the friends of the researchers.

Reading tip: The program member or program element mentioned in this article is a concept, refers to the unit that composes the program code: such as class, method, member variable.

What exactly is annotation?

Annotation provides a way to correlate any information or any metadata (metadata) with a program element. In some ways, annotation is used like a modifier and applies to packages, types, construction methods, methods, member variables, parameters, and declarations of local variables. This information is stored in the annotation "name=value" structure pair. The annotation type is an interface that provides access to its information in the form of a Java reflection API.

Annotation can be used to correlate any information to a program element (class, method, member variable, and so on). It should be noted that there is a basic unspoken rule: annotaion can not affect the execution of program code, regardless of adding, deleting annotation, the code is consistent execution. In addition, while some annotation are accessed through the Java Reflection API method at run time, the Java language interpreter ignores these annotation at work. It is because the Java virtual machine ignores the annotation that the annotation type is "non-functional" in the code, and only through some kind of matching tool will the information in the annotation type be accessed and processed. The standard annotation and meta-annotation types are covered in this article, and the tools that accompany these annotation types are the Java compilers (of course they are handled in a special way).

For the above reasons, annotation is very easy to use. A local variable can be annotated with a nonnull-named annotation type as an assertion that the local variable cannot be assigned a null value. And we could write a annotation code analysis tool that we could use to parse the code with the preceding variables and try to validate the assertion. Of course, the code doesn't have to be written by itself. After the JDK is installed, a tool named "apt" can be found in the Jdk/bin directory, which provides a framework for handling annotation: It scans annotation in the source code after it starts, and call our well-defined annotation processor to do the work we're going to do (like validating the assertions in the previous example). Speaking of this, annotation's powerful features seem to replace the tools of Xdoclet, and as we go deeper, you will be more convinced.

Note: Please refer to JSR250 specification for detailed description:

Http://www.jcp.org/aboutJava/communityprocess/pfd/jsr250/

Second, the definition of annotation:

This text begins to introduce annotation related technology. Here you will see the standard annotation type of java5.0, which is the "builtin" type described in the previous article, which can be directly supported by Javac. Thankfully, the Javac in the Java6.0beta version has added support for custom annotation.

1. The concept and syntax of annotation:

First, the key concept is to understand that annotation is associated with a program element or annotated with metadata. It never affects the execution of Java programs, but it has an impact on, for example, compiler warnings or assistive tools like document generators.

Here is a list of commonly used annotation, and we should note the differences between annotation and annotation types:

A.annotation:

annotation uses the new syntax that comes with java5.0, which behaves much like public and final modifiers. Each annotation has a >=0 name and number of members. The members of each annotation have names and values called Name=value pairs (just like JavaBean), Name=value loaded with annotation information.

B.annotation Type:

The annotation type defines the annotation name, type, and member default values. A annotation type can be said to be a special Java interface, its member variables are restricted, and the new syntax is required when declaring annotation types. When we access annotation through the Java Reflection API, the return value will be an object that implements the annotation type interface, and we can easily access its annotation members by accessing the object. The following sections refer to the 3 standard annotation types included in the Java.lang package in java5.0.

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.