| 在定義Annotation型態時,您使用java.lang.annotation.Target可以定義其適
用之時機,在定義時可以指定java.lang.annotation.ElementType的列舉值:
package java.lang.annotation;
public enum ElementType { TYPE, // 適用 class, interface, enum FIELD, // 適用 field METHOD, // 適用 method PARAMETER, // 適用 method 上之 parameter CONSTRUCTOR, // 適用 constructor LOCAL_VARIABLE, // 適用區域變數 ANNOTATION_TYPE, // 適用 annotation 型態 PACKAGE // 適用 package } 舉個例子來說,假設您定義Annotation只能適用於constructor與method:
package onlyfun.caterpillar; 如果您嘗試將Debug用於class上:
package onlyfun.caterpillar; 則在編譯時會發生以下的錯誤: SomeObject.java:3: annotation type not applicable to this kind of declaration
@Debug ^ 1 error |