JAVAssist---动态修改注解

来源:互联网 发布:阿里云客服是什么 编辑:程序博客网 时间:2024/06/04 18:40
 先来看看我们需要修改注解的代码:
[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  * EntityManager的实例化 
  3.  * @author 陈丽娜 
  4.  * @version 1.0.0 , 2015年3月30日 下午8:43:27 
  5.  * @param <T> 
  6.  */  
  7. public class CollectionBase<T> extends BaseEaoImpl<T> {  
  8.     /** 
  9.      * 注入实体单元 
  10.      */  
  11.     @PersistenceContext(unitName="collection-entity")  
  12.     protected EntityManager em;  
  13.     /**EntityManger 
  14.      * 实例化 
  15.      */  
  16.     @Override  
  17.     protected EntityManager getEntityManager() {          
  18.         return this.em;  
  19.     }  
  20. }  


需要修改的是unitName.

     那么该如何修改呢?开始是没有任何思路的,但当时的我总有种感觉一定可以修改,所以就查了一下,发现了JAVAssist:开源的分析。编辑和创建java字节码的类库。更深入的认识可以百度了解一下,下面一个小的demo来修改一下注解:
      首先来看是如何获得这个注解的:
[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. @Test  
  2. public void ReadTest() throws NotFoundException{  
  3.     ClassPool pool = ClassPool.getDefault();  
  4.     //获取要修改的类的所有信息  
  5.     CtClass ct = pool.get("com.tgb.itoo.collection.base.CollectionBase");     
  6.     //获取类中的方法  
  7.     CtMethod[] cms = ct.getDeclaredMethods();         
  8.      //获取第一个方法(因为只有一个方法)  
  9.      CtMethod cm = cms[0];        
  10.      System.out.println("方法名称====" + cm.getName());        
  11.      //获取方法信息  
  12.      MethodInfo methodInfo = cm.getMethodInfo();           
  13.      //获取类里的em属性  
  14.      CtField cf = ct.getField("em");  
  15.      //获取属性信息  
  16.      FieldInfo fieldInfo = cf.getFieldInfo();          
  17.      System.out.println("属性名称===" + cf.getName());  
  18.        
  19.      //获取注解属性  
  20.      AnnotationsAttribute attribute = (AnnotationsAttribute) fieldInfo.getAttribute(AnnotationsAttribute.visibleTag);  
  21.      System.out.println(attribute);   
  22.      //获取注解  
  23.      Annotation annotation = attribute.getAnnotation("javax.persistence.PersistenceContext");         
  24.      System.out.println(annotation);  
  25.      //获取注解的值  
  26.      String text =((StringMemberValue) annotation.getMemberValue("unitName")).getValue() ;         
  27.      System.out.println("注解名称===" + text);  
  28.        
  29. }  


运行结果:
[plain] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. 方法名称====getEntityManager  
  2. 属性名称===em  
  3. @javax.persistence.PersistenceContext(unitName="collection-entity")  
  4. @javax.persistence.PersistenceContext(unitName="collection-entity")  
  5. 注解名称===collection-entity  

        修改注解的方法与获取的一样,只是需要为获取的注解赋值,代码如下:
[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. @Test  
  2.     public void UpdateTest() throws NotFoundException{  
  3.            ClassPool pool = ClassPool.getDefault();      
  4.            //获取需要修改的类  
  5.            CtClass ct = pool.get("com.tgb.itoo.collection.base.CollectionBase");   
  6.              
  7.            //获取类里的所有方法  
  8.            CtMethod[] cms = ct.getDeclaredMethods();  
  9.            CtMethod cm = cms[0];      
  10.            System.out.println("方法名称====" + cm.getName());  
  11.              
  12.            MethodInfo minInfo = cm.getMethodInfo();  
  13.            //获取类里的em属性  
  14.            CtField cf = ct.getField("em");  
  15.            FieldInfo fieldInfo = cf.getFieldInfo();    
  16.              
  17.            System.out.println("属性名称===" + cf.getName());  
  18.              
  19.            ConstPool cp = fieldInfo.getConstPool();  
  20.            //获取注解信息  
  21.            AnnotationsAttribute attribute2 = new AnnotationsAttribute(cp, AnnotationsAttribute.visibleTag);  
  22.            Annotation annotation = new Annotation("javax.persistence.PersistenceContext", cp);  
  23.            
  24.            //修改名称为unitName的注解  
  25.            annotation.addMemberValue("unitName"new StringMemberValue("basic-entity", cp));  
  26.            attribute2.setAnnotation(annotation);  
  27.            minInfo.addAttribute(attribute2);  
  28.              
  29.            //打印修改后方法  
  30.            Annotation annotation2 = attribute2.getAnnotation("javax.persistence.PersistenceContext");  
  31.            String text = ((StringMemberValue)annotation2.getMemberValue("unitName")).getValue();  
  32.              
  33.            System.out.println("修改后的注解名称===" + text);  
  34.     }  


运行结果:
[plain] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. 方法名称====getEntityManager  
  2. 属性名称===em  
  3. 修改后的注解名称===basic-entity  

多么神奇的动态修改注解,运行时修改很多问题就迎刃而解了!
原创粉丝点击