Getting Started with Java annotation

Source: Internet
Author: User

Summary:

In this paper, the usage, definition and classification of annotation are explained comprehensively for the beginners of Java or the annotation first users. Beginners can use the above instructions to make simple annotation program, but for some advanced annotation applications (such as the use of custom annotation generate JavaBean mapping XML file) need further research and discussion. In the context of the in-depth annotation, the author will talk about it in the later "Java annotation advanced Application".

At the same time, there are two ways for annotation to run: runtime, compile-time. The annotation application at runtime is discussed above, but the annotation application at compile time is not yet involved,

First, why use annotation:

In Java applications, we often encounter some need to use template code. For example, in order to write a JAX-RPC Web service, we must provide a pair of interfaces and implementations as template code. If you use annotation to modify the method code for remote access, this template can be automatically generated using tools.

In addition, some APIs need to use ancillary files that are maintained at the same time as the program code. For example, JavaBeans requires a BeanInfo class to be used/maintained at the same time as a bean, while EJBS need a deployment descriptor as well. Using annotation in your program at this time to maintain information about these ancillary files is convenient and reduces errors.

Second, annotation work mode:

The Java platform prior to version 5.0 already has some ad hoc annotation mechanisms. For example, using the transient modifier to identify a member variable should be ignored in the serialization subsystem. and @deprecated This Javadoc tag is also an ad hoc annotation used to illustrate that a method is obsolete. Since the release of the Java5.0 version, the 5.0 platform provides a formal annotation feature that allows developers to define and use their own annoatation types. This functionality consists of a syntax defining the annotation type and a syntax for describing the annotation declaration, reading the Annotaion API, a class file decorated with annotation, and a annotation processing tool (APT).

annotation does not directly affect code semantics, but the way it works is viewed as a tool for similar programs or as a class library, which in turn affects the running program semantics. Annotation can be read from a source file, a class file, or a variety of ways to reflect at run time.

Of course annotation makes Javadoc tag more complete in a way. In general, this tag should be used as a Javadoc tag if it has an impact on the Java document or for generating Java documents;

Three, annotation use method:

1. Type declaration method:

Typically, an application does not have to define a annotation type, but it is not difficult to define a annotation type. The annotation type is very similar to a generic declaration of an interface, except that it uses the "@" symbol before the interface keyword.

Each method declaration of the annotation type defines a annotation type member, but the method declaration does not necessarily have a parameter or an exception declaration; the type of the method return value is limited to the following ranges: Primitives, String, Class, enums, Annotation and arrays of the preceding types; methods can have default values.

The following is a simple annotation type declaration:

Listing 1:

/**
   * Describes the Request-For-Enhancement(RFE) that led
   * to the presence of the annotated API element.
   */
   public @interface RequestForEnhancement {
     int   id();
     String synopsis();
     String engineer() default "[unassigned]";
     String date();   default "[unimplemented]";
   }

Only one annotation type requestforenhancement is defined in the code.

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.