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();
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有