類型注解
原來注解只能用于聲明滋迈,從JDK 8開始,注解還可以用于大多數(shù)使用類型的地方户誓,這種注解稱為類型注解饼灿。類型注解允許工具對(duì)代碼執(zhí)行額外的檢查,從而幫助避免錯(cuò)誤帝美。javac本身一般不執(zhí)行這些檢查碍彭,這種工具需要作為編譯器插件發(fā)揮作用。
例子
@Target(ElementType.TYPE_USE)
public @interface TypeAnno {
}
@Target(ElementType.TYPE_USE)
public @interface MaxLen {
int value();
}
@Target(ElementType.TYPE_USE)
public @interface Unique {
}
@Target(ElementType.TYPE_PARAMETER)
public @interface What {
String description();
}
@Target(ElementType.FIELD)
public @interface NotNull {
}
@Target(ElementType.METHOD)
public @interface Recommended {
}
// 注解類型參數(shù)
public class TypeTest<@What(description = "Generic data type") T> {
// 注解字段類型
private @TypeAnno String first;
// 注解字段聲明
private @NotNull String second;
// 注解數(shù)組元素類型
private @TypeAnno Integer[] nums;
// 注解數(shù)組級(jí)別
private String @MaxLen(5) [] @MaxLen(10) [] data;
// 注解構(gòu)造函數(shù)
public @Unique TypeTest() {
}
// 注解this的類型
public int f1(@TypeAnno TypeTest<T> this, int x) {
return 10;
}
// 注解方法返回類型
public @TypeAnno Integer f2(int j, int k) {
return j + k;
}
// 注解方法聲明
public @Recommended Integer f3(String str) {
return str.length() / 2;
}
// 注解異常類型
public void f4() throws @TypeAnno NullPointerException {
// ...
}
public static void test() {
TypeTest<@TypeAnno Integer> obj1 = new TypeTest<>(); // 注解實(shí)際類型參數(shù)
@TypeAnno TypeTest<Integer> obj2 = new @TypeAnno TypeTest<>(); // 注解實(shí)例類型
Object x = new Integer(10);
Integer y;
y = (@TypeAnno Integer) x; // 注解強(qiáng)制類型轉(zhuǎn)換
}
}
// 注解被繼承的類型
public class SubTypeTest extends @TypeAnno TypeTest<Integer> {
// ...
}
注意
- 類型注解必須包含ElementType.TYPE_USE作為目標(biāo)证舟。
- 類型注解需要放到應(yīng)用該注解的類型前面硕旗。
- 不能對(duì)void返回類型添加注解。
- @Target可以用于消除聲明注解和類型注解的模糊性問題女责,比如上面例子中@TypeAnno注解字段類型而@NotNull注解字段聲明漆枚;@TypeAnno注解方法返回類型而@Recommended注解方法聲明。
this對(duì)象
this是所有實(shí)例方法的隱式參數(shù)抵知,它的類型必須是其類的類型墙基。類型注解可以注解this的類型,但是需要使用JDK 8的一個(gè)新特性刷喜,從JDK 8開始可以顯式地將this聲明為方法的第一個(gè)參數(shù)残制。
除非是要注解this的類型,否則沒必要聲明this掖疮。并且顯示聲明this沒有改變方法簽名初茶,因?yàn)槟J(rèn)也會(huì)隱式聲明this。
重復(fù)注解
JDK 8新增了另一種注解特性浊闪,這種特性稱為重復(fù)注解恼布,它允許在相同元素上重復(fù)應(yīng)用同一個(gè)注解。
@Repeatable注解
可重復(fù)的注解必須用@Repeatable進(jìn)行注解搁宾。@Repeatable定義在java.lang.annotation中折汞,它的value域指定了重復(fù)注解的容器類型。源碼如下所示:
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Repeatable {
/**
* Indicates the <em>containing annotation type</em> for the
* repeatable annotation type.
* @return the containing annotation type
*/
Class<? extends Annotation> value();
}
容器注解
重復(fù)注解的容器類型被指定為注解盖腿。容器注解的value域是重復(fù)注解類型的數(shù)組爽待。要?jiǎng)?chuàng)建重復(fù)注解,必須創(chuàng)建容器注解翩腐,然后將該容器注解的類型作為@Repeatable注解的參數(shù)鸟款。
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(RepeatedAnnos.class)
public @interface RepeatableAnno {
String name() default "test";
int value() default 100;
}
@Retention(RetentionPolicy.RUNTIME)
public @interface RepeatedAnnos {
RepeatableAnno[] value();
}
獲取重復(fù)注解的方式
- 使用getAnnotation()方法獲取重復(fù)注解,要使用容器注解作為參數(shù)茂卦,而不是重復(fù)注解何什。
public class RepeatableTest {
@RepeatableAnno(name = "first annotation", value = 10)
@RepeatableAnno(name = "second annotation", value = 20)
public static void test() {
RepeatableTest obj = new RepeatableTest();
try {
Class<?> c = obj.getClass();
Method m = c.getMethod("test");
Annotation anno = m.getAnnotation(RepeatedAnnos.class);
System.out.println(anno);
} catch (NoSuchMethodException e) {
System.out.println("Method Not Found.");
}
}
public static void main(String[] args) throws Exception {
test();
}
}
上面例子的輸出如下:
@RepeatedAnnos(value=[@RepeatableAnno(name=first annotation, value=10), @RepeatableAnno(name=second annotation, value=20)])
- 獲取重復(fù)注解的另一種方式是使用JDK 8添加到AnnotatedElement中的新方法,它們能夠直接操作重復(fù)注解疙筹。這些方法包括getAnnotationsByType()和getDeclaredAnnotationsByType()富俄。
Annotation[] annos = m.getAnnotationsByType(RepeatableAnno.class);
for (Annotation anno : annos) {
System.out.println(anno);
}
一些限制
使用注解聲明有一些限制:
- 一個(gè)注解不能繼承另一個(gè)注解禁炒。
- 注解聲明的成員方法不能帶參數(shù)。
- 注解聲明的成員方法不能指定throws子句霍比。
- 注解不能被泛型化幕袱。