對于Annotation,是Java5的新特性,下面是Sun的Tutorial的描述,因為是英文,這里我翻譯下,希望能夠比較清晰的描述一下Annotation的語法以及思想。
Annotation:
Release 5.0 of the JDK introduced a metadata facility called annotations. Annotations provide data about a program that is not part of the program, such as naming the author of a piece of code or instructing the compiler to suppress specific errors. An annotation has no effect on how the code performs.
Annotations use the form @annotation and may be applied to a program‘s declarations: its classes, fields, methods, and so on. The annotation appears first and often (by convention) on its own line, and may include optional arguments:
JDK5引入了Metedata(元數據)很容易的就能夠調用Annotations.Annotations提供一些本來不屬于程序的數據,比如:一段代碼的作者或者告訴編譯器禁止一些特殊的錯誤。An annotation 對代碼的執(zhí)行沒有什么影響。Annotations使用@annotation的形勢應用于代碼:類(class),屬性(field),方法(method)等等。一個Annotation出現在上面提到的開始位置,而且一般只有一行,也可以包含有任意的參數。
@Author("MyName")
class myClass() { }
or
@SuppressWarnings("unchecked")
void MyMethod() { }
Defining your own annotation is an advanced technique that won‘t be described here, but there are three built-in annotations that every Java programmer should know: @Deprecated, @Override, and @SuppressWarnings. The following example illustrates all three annotation types, applied to methods:
定義自己的Annotation是一個比較高級的技巧,這里我們不做討論,這里我們僅僅討論每一個Java programer都應該知道的內置的annotations:@Deprecated, @Override, and @SuppressWarnings。下面的程序闡述了這三種annotation如何應用于methods。
import java.util.List;
class Food {}
class Hay extends Food {}
class Animal {
Food getPreferredFood() {
return null;
}
/**
* @deprecated document why the method was deprecated
*/
@Deprecated
static void deprecatedMethod() { }
}
class Horse extends Animal {
Horse() {
return;
}
@Override
Hay getPreferredFood() {
return new Hay();
}
@SuppressWarnings("deprecation")
void useDeprecatedMethod() {
Animal.deprecateMethod(); //deprecation warning - suppressed
}
}
@Deprecated
The @Deprecated annotation indicates that the marked method should no longer be used. The compiler generates a warning whenever a program uses a deprecated method, class, or variable. When an element is deprecated, it should be documented using the corresponding @deprecated tag, as shown in the preceding example. Notice that the tag starts with a lowercase "d" and the annotation starts with an uppercase "D". In general, you should avoid using deprecated methods — consult the documentation to see what to use instead.
@Deprecated
@Deprecated annotation標注一個method不再被使用。編譯器在一個program(程序?)使用了不贊成的方法,類,變量的時候會產生警告(warning)。如果一個元素(element:method, class, or variable)不贊成被使用,應該像前面的例子里使用相應的@deprecated 標簽,并且注意標簽的首字母是小寫的"d",而annotation時大寫的"D"。一般情況下,我們應該避免使用不贊成使用的方法(deprecated methods),而應該考慮替代的方法。
@Override
The @Override annotation informs the compiler that the element is meant to override an element declared in a superclass. In the preceding example, the override annotation is used to indicate that the getPreferredFood method in the Horse class overrides the same method in the Animal class. If a method marked with @Override fails to override a method in one of its superclasses, the compiler generates an error.
While it‘s not required to use this annotation when overriding a method, it can be useful to call the fact out explicitly, especially when the method returns a subtype of the return type of the overridden method. This practice, called covariant return types, is used in the previous example: Animal.getPreferredFood returns a Food instance. Horse.getPreferredFood (Horse is a subclass of Animal) returns an instance of Hay (a subclass of Food). For more information, see Overriding and Hiding Methods.
@Override
@Override annotation 告訴編譯器當前元素是重寫(override)自父類的一個元素。在前面的例子中,override annotation用來說明Horse類中的getPreferredFood這個方法重寫(override)自Animal類中相同的方法。如果一個方法被標注了@Override,但是其父類中沒有這個方法時,編譯器將會報錯。
但是并不是說我們一定要使用這個annotation,但是它能夠很明顯的給出實際行為,尤其是在方法返回一個被重寫的方法返回類型的子類型的時候。上面的例子中,Animal.getPreferredFood 返回一個 Food實例,Horse.getPreferredFood 返回一個Hay實例,這里Horse是Animal的子類,Hay是Food的子類。
@SuppressWarnings
The @SuppressWarnings annotation tells the compiler to suppress specific warnings that it would otherwise generate. In the previous example, the useDeprecatedMethod calls a deprecated method of Animal. Normally, the compiler generates a warning but, in this case, it is suppressed.
Every compiler warning belongs to a category. The Java Language Specification lists two categories: "deprecation" and "unchecked". The "unchecked" warning can occur when interfacing with legacy code written before the advent of generics. To suppress more than one category of warnings, use the following syntax:
@SuppressWarnings
@SuppressWarnings annotation 告訴編譯器禁止別的元素產生的特殊的警告(warnings),在前面的例子里,useDeprecatedMethod調用了Animal的不贊成使用的一個方法。一般情況下,編譯器會給出一個警告(warning),但是在這種情況下,不會產生這個警告,也就是說被suppress。
每個編譯器的警告都屬于一個類型。Java Language Specification列出了兩種類型:"deprecation" 和 "unchecked"。"unchecked" warning 發(fā)生在使用非generic的舊代碼交互的generic collection類時。為了禁止不止一種的警告時,使用下面的語法:
@SuppressWarnings({"unchecked", "deprecation"})
關于Annotation還可以參考下面的文章:
作者:cleverpig(作者的Blog:http://blog./page/cleverpig)
原文: http://www./resource/article/44/44048_Java+Annotation.html
http://www./resource/article/44/44055_Java+Annotation+Reflect.html