javassist:增强型的java反射工具,获取方法参数名,获取方法参数标注值

java的反射是不能获取方法的参数名的。比如:

public  String concatString(String param1 ,String param2 ){
        
return  param1 + param2;
    }


想获取"param1",和"param2"这个参数名,貌似是不行的。借助第三方包javaassist 就可以获得。

 1        public   static   void  main(String[] args) {
 2           Class clazz  =  
 3   MyClass. class ;
 4            try  {
 5               ClassPool pool  =  ClassPool.getDefault();  
 6               CtClass cc  =  pool.get(clazz.getName());  
 7               CtMethod cm  =  cc.getDeclaredMethod( "concatString " );
 8               
 9                // 使用javaassist的反射方法获取方法的参数名
10               MethodInfo methodInfo  =  cm.getMethodInfo();  
11               CodeAttribute codeAttribute  =  methodInfo.getCodeAttribute();  
12               LocalVariableAttribute attr  =  (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);  
13                if  (attr  ==   null )  {
14                    // exception
15               }
16               String[] paramNames  =   new  String[cm.getParameterTypes().length];  
17                int  pos  =  Modifier.isStatic(cm.getModifiers())  ?   0  :  1 ;  
18                for  ( int  i  =   0 ; i  <  paramNames.length; i ++ )  
19                   paramNames[i]  =  attr.variableName(i  +  pos);      
20                // paramNames即参数名
21                for  ( int  i  =   0 ; i  <  paramNames.length; i ++ ) {
22                   System.out.println(paramNames[i]);
23               }
24               
25           }  catch  (NotFoundException e) {
26               e.printStackTrace();
27           } 
28       }



第一次使用觉得这个东西蛮神奇的。今天我在用反射获取标注值时,发现使用jdk本身的反射貌似也获取不到。我又想到了javassist,他果然没让我失望。

 1        public  String datefomat(@DateFormat( " yyyy-MM-dd HH " )Date date1){
 2            return  date1.toString();
 3       }
 4       
 5        public   static   void  main(String[] args) {
 6           Class clazz  = MyClass. class ;
 7            try  {
 8                // 使用jdk原生的反射方法
 9               Method m  =  clazz.getDeclaredMethod( " datefomat " new  Class[]{Date. class });
10               Annotation[][] annotations  =  m.getParameterAnnotations();
11               System.out.println(annotations[ 0 ]);
12   //             Annotation anno = annotations[0][0];  //  index outof range  exception
13           }  catch  (SecurityException e) {
14               e.printStackTrace();
15           }  catch  (NoSuchMethodException e) {
16               e.printStackTrace();
17           }
18           
19            try  {
20               ClassPool pool  =  ClassPool.getDefault();  
21               CtClass cc  =  pool.get(clazz.getName());  
22               CtMethod cm  =  cc.getDeclaredMethod( " datefomat " );
23               
24                // 使用javassist的反射方法可以获得参数标注值
25               Object[][] annotations  =  cm.getParameterAnnotations();
26               DateFormat myAnno  = (DateFormat) annotations[ 0 ][ 0 ];
27               System.out.println(myAnno.value());
28               
29               
30               
31               
32           }  catch  (NotFoundException e) {
33               e.printStackTrace();
34           }  catch  (ClassNotFoundException e) {
35               e.printStackTrace();
36           }
37           
38           
39           
40       }


Annotation的定义:

 1   package  ;
 2  
 3   import  java.lang.annotation.ElementType;
 4   import  java.lang.annotation.Target;
 5  
 6   @Target(ElementType.PARAMETER)   
 7   public  @ interface  DateFormat {
 8       String value()  default   " yyyy-MM-dd " ;
 9   }
10
我估计:
getParameterAnnotations之所以没有获得到DateFormat 的实例,是因为DateFormat 的定义没有:
@Retention(RetentionPolicy.RUNTIME)
所以运行时不会获取Annotation对象
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值