1. 程式人生 > 其它 >What's the difference between interface and @interface in java? and defining an annotation type

What's the difference between interface and @interface in java? and defining an annotation type

今天細看了@PathVarible

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.springframework.web.bind.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import org.springframework.core.annotation.AliasFor; @Target({ElementType.PARAMETER}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface PathVariable { @AliasFor("name") String value() default ""; @AliasFor("value") String name() default
""; boolean required() default true; }
上面程式碼是annotation的定義,它類似於介面的定義。

That means it isnotreally an interface, but rather a new annotation type -- to be used as a function modifier, such as@override.

Many APIs require a fair amount of boilerplateboiler+platen.(可供模仿的)樣板檔案 code. For example, in order to write a JAX-RPC(Java APIs for XML-based Remote Procedure Call (

JAX-RPC)) web service, you must provide a paired interface and implementation. This boilerplate could be generated automatically by a tool if the program were “decorated” with annotations indicating which methods were remotely accessible.

Other APIs require “side files” to be maintained in parallel with programs. For example JavaBeans requires aBeanInfoclass to be maintained in parallel with a bean, and Enterprise JavaBeans (EJB) requires adeployment descriptor. It would be more convenient and less error-prone if the information in these side files were maintained as annotations in the program itself.

The Java platform has always had various ad hoc([ˌæd ˈhɑk]adj.臨時安排的;特別的;專門的) annotation mechanisms. For example thetransientmodifier is an ad hoc annotation indicating that a field should be ignored by the serialization subsystem, and the@deprecatedjavadoc tag is an ad hoc annotation indicating that the method should no longer be used. As of release 5.0, the platform has a general purpose annotation (also known asmetadata) facility that permits you to define and use your own annotation types. The facility consists of a syntax for declaring annotation types, a syntax for annotating declarations, APIs for reading annotations, a class file representation for annotations, and anannotation processing tool.

Annotations do not directly affect program semantics[səˈmæntɪks]n.語義學;(單詞、短語或其他符號系統的)含義

, but they do affect the way programs are treated by tools and libraries, which can in turn affect the semantics of the running program. Annotations can be read from source files, class files, or reflectively at run time.

Annotations complement javadoc tags. In general, if the markup is intended to affect or produce documentation, it should probably be a javadoc tag; otherwise, it should be an annotation.

Typical application programmers will never have to define an annotation type, but it is not hard to do so. Annotation type declarations are similar to normal interface declarations. An at-sign (@) precedes theinterfacekeyword. Each method declaration defines anelementof the annotation type. Method declarations must not have any parameters or athrowsclause. Return types are restricted to primitives,String,Class,enums, annotations, and arrays of the preceding types. Methods can havedefault values. Here is an example annotation type declaration:

/**
 * 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]"; 
}

Once an annotation type is defined, you can use it to annotate declarations. An annotation is a special kind of modifier, and can be used anywhere that other modifiers (such aspublic,static, orfinal) can be used. By convention, annotations precede other modifiers. Annotations consist of an at-sign (@) followed by an annotation type and a parenthesized[pə'renθəˌsaɪz]把…括入圓括號內 list of element-value pairs. The values must be compile-time constants. Here is a method declaration with an annotation corresponding to the annotation type declared above:

@RequestForEnhancement(
    id       = 2868724,
    synopsis = "Enable time-travel",
    engineer = "Mr. Peabody",
    date     = "4/1/3007"
)
public static void travelThroughTime(Date destination) { ... }

An annotation type with no elements is termed amarkerannotation type, for example:

/**
 * Indicates that the specification of the annotated API element
 * is preliminary and subject to change.
 */
public @interface Preliminary { }

It is permissible to omit the parentheses in marker annotations, as shown below:

@Preliminary public class TimeTravel { ... }

In annotations with a single element, the element should be namedvalue, as shown below:

/**
 * Associates a copyright notice with the annotated API element.
 */
public @interface Copyright {
    String value();
}