java 类,变量,方法上注解值的获取

首先定义三个注解类, 分别适用于类,成员变量, 方法

[java]  view plain  copy
  1. @Target(ElementType.TYPE)  
  2. @Retention(RetentionPolicy.RUNTIME)  
  3. public @interface LeiMode {  
  4.     public int value() default 1;  
  5. }  

[java]  view plain  copy
  1. @Target(ElementType.FIELD)  
  2. @Retention(RetentionPolicy.RUNTIME)  
  3. public @interface FiledMode {  
  4.     public int value() default 1;  
  5. }  

[java]  view plain  copy
  1. @Target(ElementType.METHOD)  
  2. @Retention(RetentionPolicy.RUNTIME)  
  3. public @interface TreahMode {  
  4.     public int value() default 1;  
  5. }  

然后,定义一个类,使用了注解

[java]  view plain  copy
  1. @LeiMode(5)  
  2. public class AnnotationDemo {  
  3.       
  4.     @FiledMode(10)  
  5.     private int itest;  
  6.       
  7.     @TreahMode()  
  8.     private void test(){}  
  9. }  


1.获取类上的注解值

[java]  view plain  copy
  1. LeiMode annotation = AnnotationDemo.class.getAnnotation(LeiMode.class);  
  2. System.out.println(annotation.value());  


2.获取所有变量,并获取指定方法上的注解信息

[html]  view plain  copy
  1. Field[] fields = AnnotationDemo.class.getDeclaredFields();  
  2.         Field field = null;  
  3.         for(Field f : fields){  
  4.             if(f.getName().equals("itest")){  
  5.                 field = f;  
  6.                 break;  
  7.             }  
  8.         }  
  9.           
  10.         FiledMode annotation = field.getAnnotation(FiledMode.class);  
  11.         System.out.println(annotation.value());  


3.获取指定变量上的注解信息 

[java]  view plain  copy
  1. Field field = AnnotationDemo.class.getDeclaredField("itest");  
  2.         FiledMode annotation = field.getAnnotation(FiledMode.class);  
  3.           
  4.         System.out.println(annotation.value());  

4.获取所有方法,并获取指定方法上的注解信息

[java]  view plain  copy
  1. Method[] methods = AnnotationDemo.class.getDeclaredMethods(); //可以获取私有方法和公有方法, getMethods() 获取公有方法  
  2.         Method meth = null;  
  3.         for(Method method : methods){  
  4.             if(method.getName().equals("test")){  
  5.                 meth = method;  
  6.                 break;  
  7.             }  
  8.         }  
  9.         Annotation annotation = meth.getAnnotations()[0];  
  10.         TreahMode mode = (TreahMode) annotation;  
  11.         System.out.println(mode.value());  

5.获取指定方法上的注解信息

[java]  view plain  copy
  1. Method method = AnnotationDemo.class.getDeclaredMethod("test"null);//可以获取私有方法和公有方法  
  2.         System.out.println(method);  
  3.         Annotation[] annotations = method.getAnnotations();  
  4.         Annotation annotation = annotations[0];  
  5.         System.out.println(annotation);  
  6.           
  7.         TreahMode mode = (TreahMode) annotation;  
  8.         System.out.println(mode.value());  
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Java中的反射是指可以在运行时获取或处理任意对象的信息以及操作对象的能力。获取字段上的注解也是反射的一种应用。注解Java中的一种特殊标记,它可以用于给代码添加元数据,比如说用于描述方法、参数、变量等信息。注解可以通过反射获取。 要获取字段上的注解,首先需要获取字段对象。可以使用Class的getField()或getDeclaredField()方法获取字段对象,并且可以通过Field的getAnnotations()方法获取字段上的所有注解。 getAnnotations()方法返回一个Annotation型的数组,可以使用foreach循环遍历这个数组,获取注解的信息。每个注解都有自己的型,所以需要使用getClass()方法获取注解型。 下面是示例代码: ``` public class Example { @MyAnnotation(value = "example", count = 2) public String name; public static void main(String[] args) throws NoSuchFieldException { Example example = new Example(); Field field = example.getClass().getField("name"); Annotation[] annotations = field.getAnnotations(); for (Annotation annotation : annotations) { System.out.println(annotation.getClass()); } } } @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) @interface MyAnnotation { String value(); int count() default 1; } ``` 在这个示例代码中,定义了一个MyAnnotation注解,并且使用这个注解标记了Example中的一个字段。在main()方法中,首先通过getField()方法获取字段对象,然后使用getAnnotations()方法获取字段上的注解。 通过运行代码可以看到,输出了MyAnnotation.class,即成功获取了字段上的注解型。通过注解型可以进一步获取注解的信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值