@Target(...{ElementType.METHOD})
@interface MyAnnotation ...{}
![]()
@MyAnnotation // 错误的使用
public class Class1
...{
@MyAnnotation // 正确的使用
public void myMethod1() ...{}
}
@Target(...{ElementType.METHOD, ElementType.CONSTRUCTOR})
@interface MyAnnotation ...{}
![]()
Retention
@Retention(RetentionPolicy.SOURCE)
@interface MyAnnotation1 ...{ }
![]()
@Retention(RetentionPolicy.CLASS)
@interface MyAnnotation2 ...{}
![]()
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation3 ...{}
@interface MyAnnotation...{ }
![]()
@MyAnnotation
class Class1
...{
public void myMethod() ...{ }
}
class Class1extends java.lang.Object
![]()
而如果这样定义MyAnnotation将会出现另一个结果。
@Documented
@interface MyAnnotation ...{}
![]()
生成的文档:
@MyAnnotation // 这行是在加上@Documented后被加上的
class Class1extends java.lang.Object
![]()
Inherited
@Inherited
@interface MyAnnotation ...{ }
![]()
@MyAnnotation
public class ParentClass ...{}
![]()
public class ChildClass extends ParentClass ...{ }
![]()
在以上代码中ChildClass和ParentClass一样都已被MyAnnotation注释了。