import java.lang.annotation.*;
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE,ElementType.METHOD,ElementType.FIELD})
public @interface MyAnnotation {
String value() default "hejiaxuan";
}
@MyAnnotation
public class OneClass {
@MyAnnotation
public String name;
}
//获取类上的所有的注解
Annotation[] annotations = OneClass.class.getAnnotations();
//根据类型获取注解
MyAnnotation annotation = OneClass.class.getAnnotation(MyAnnotation.class);
//获取本类的注解(忽略继承)
Annotation[] annotations = TwoClass.class.getDeclaredAnnotations();
//根据类型获取属性上的注解
Field name = OneClass.class.getField("name");
MyAnnotation annotation = name.getAnnotation(MyAnnotation.class);
//获取属性上的所有注解
Annotation[] annotations = name.getAnnotations();