利用反射获取类或者方法或者字段上的注解的值

本文通过具体示例介绍如何在Java中使用注解,并演示了如何获取类、方法及字段上的注解值。涵盖注解定义、注解类型及其在反射中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

从JDK1.5之后,注解在各大框架上得到了广泛的应用。下面这个例子中,你可以判断一个类或者方法或者字段上有没有注解,以及怎么获取上面的注解值。话不多说,代码如下:

AnnotationTest01.java

 

Java代码  收藏代码
  1. package com.zkn.newlearn.annotation;  
  2.   
  3. import java.lang.annotation.ElementType;  
  4. import java.lang.annotation.Retention;  
  5. import java.lang.annotation.RetentionPolicy;  
  6. import java.lang.annotation.Target;  
  7.   
  8. @Retention(RetentionPolicy.RUNTIME)  
  9. @Target({ElementType.TYPE,ElementType.METHOD,ElementType.PARAMETER})  
  10. public @interface AnnotationTest01 {  
  11.   
  12.     String color();  
  13. }  

AnnotationTest02.java

 

Java代码  收藏代码
  1. package com.zkn.newlearn.annotation;  
  2.   
  3. import java.lang.annotation.ElementType;  
  4. import java.lang.annotation.Retention;  
  5. import java.lang.annotation.RetentionPolicy;  
  6. import java.lang.annotation.Target;  
  7.   
  8. @Retention(RetentionPolicy.RUNTIME)  
  9. @Target(ElementType.TYPE)  
  10. public @interface AnnotationTest02 {  
  11.       
  12.     String getUserName();  
  13. }  


AnnotationTest04.java

 

Java代码  收藏代码
  1. package com.zkn.newlearn.annotation;  
  2.   
  3. import java.lang.annotation.ElementType;  
  4. import java.lang.annotation.Retention;  
  5. import java.lang.annotation.RetentionPolicy;  
  6. import java.lang.annotation.Target;  
  7.   
  8. @Retention(RetentionPolicy.RUNTIME)  
  9. @Target({ElementType.TYPE,ElementType.FIELD})  
  10. public @interface AnnotationTest04 {  
  11.       
  12.     String getAddress();  
  13. }  

AnnotationTest03.java

 

Java代码  收藏代码
  1. package com.zkn.newlearn.annotation;  
  2.   
  3. import java.lang.reflect.Field;  
  4. import java.lang.reflect.InvocationTargetException;  
  5. import java.lang.reflect.Method;  
  6.   
  7. /** 
  8.  * 测试Annotation 
  9.  * @author zkn 
  10.  * 
  11.  */  
  12. @AnnotationTest02(getUserName="zhangsan")  
  13. public class AnnotationTest03 {  
  14.   
  15.     @AnnotationTest01(color="red")  
  16.     public static String testColor(String color){  
  17.         System.out.println(color);  
  18.         return color;  
  19.     }  
  20.       
  21.     @AnnotationTest04(getAddress="北京市海淀区")  
  22.     String address;  
  23.       
  24.     public static void main(String[] args) {  
  25.         //获取方法上的注解值  
  26.         Method[] methods = AnnotationTest03.class.getDeclaredMethods();  
  27.         if(methods != null){  
  28.             for(Method method : methods){  
  29.                 AnnotationTest01 annotation = method.getAnnotation(AnnotationTest01.class);  
  30.                 if(annotation == null)  
  31.                     continue;  
  32.                 Method[] me = annotation.annotationType().getDeclaredMethods();  
  33.                 for(Method meth : me){  
  34.                     try {  
  35.                         String color = (String) meth.invoke(annotation,null);  
  36.                         System.out.println(color);  
  37.                     } catch (IllegalAccessException e) {  
  38.                         e.printStackTrace();  
  39.                     } catch (IllegalArgumentException e) {  
  40.                         e.printStackTrace();  
  41.                     } catch (InvocationTargetException e) {  
  42.                         e.printStackTrace();  
  43.                     }  
  44.                 }  
  45.             }  
  46.         }  
  47.   
  48.         //获取类上的注解值  
  49.         AnnotationTest02 anno = AnnotationTest03.class.getAnnotation(AnnotationTest02.class);  
  50.         if(anno != null){  
  51.             Method[] met = anno.annotationType().getDeclaredMethods();  
  52.             for(Method me : met ){  
  53.                 if(!me.isAccessible()){  
  54.                     me.setAccessible(true);  
  55.                 }  
  56.                 try {  
  57.                     System.out.println(me.invoke(anno, null));  
  58.                 } catch (IllegalAccessException e) {  
  59.                     e.printStackTrace();  
  60.                 } catch (IllegalArgumentException e) {  
  61.                     e.printStackTrace();  
  62.                 } catch (InvocationTargetException e) {  
  63.                     e.printStackTrace();  
  64.                 }  
  65.             }  
  66.         }  
  67.           
  68.         //获取字段上的注解值  
  69.         AnnotationTest03 noon = new AnnotationTest03();  
  70.         Field[] field = AnnotationTest03.class.getDeclaredFields();  
  71.         if(field != null){  
  72.             for(Field fie : field){  
  73.                 if(!fie.isAccessible()){  
  74.                     fie.setAccessible(true);  
  75.                 }  
  76.                 AnnotationTest04 annon = fie.getAnnotation(AnnotationTest04.class);  
  77.                 Method[] meth = annon.annotationType().getDeclaredMethods();  
  78.                 for(Method me : meth){  
  79.                     if(!me.isAccessible()){  
  80.                         me.setAccessible(true);  
  81.                     }  
  82.                     try {  
  83.                         //给字段重新赋值  
  84.                         fie.set(noon, me.invoke(annon, null));  
  85.                         System.out.println(fie.get(noon));  
  86.                     } catch (IllegalAccessException e) {  
  87.                         e.printStackTrace();  
  88.                     } catch (IllegalArgumentException e) {  
  89.                         e.printStackTrace();  
  90.                     } catch (InvocationTargetException e) {  
  91.                         e.printStackTrace();  
  92.                     }  
  93.                 }  
  94.                   
  95.             }  
  96.         }  
  97.     }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值