动态修改实体class类注释中的值

时间:2025-03-19 09:55:36
import ;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

/**  
 * @Title: 
 * @Package 
 * @Description: 动态修改实体类注释中的值(不提倡经常使用)
 * @author TangCai
 * @date 2016年11月16日 下午3:08:56
 * @version V1.0  
 */
public class ClassPoolUtils {
	/** 
     * 运行时动态ORM表映射 
     * @param entityClass   实体类
     * @param annotation    注释类
     * @param atMethodName  注释类方法
     * @param atMethodVal   注释类方法的更新值
     * @return              修改注释值后实体类 
     */  
    @SuppressWarnings("unchecked")
	public static Class<?> setPool(Class<?> entityClass, Class<?> annotation,
    		String atMethodName, Object atMethodVal){  
        Class<?> c = null;
        try {  
            ClassPool classPool = ();  

            CtClass clazz = (());
            ClassFile classFile = ();
            //();//冻结

            ConstPool constPool = ();  
            Class<> cl = (Class<>)annotation;
            Annotation newAnnotation = createJavassistAnnotation(constPool,(cl),atMethodName,atMethodVal);
            
            AnnotationsAttribute attribute = (AnnotationsAttribute)();  
            (newAnnotation);  
            (attribute); 
            ClassLoader parent = ();
            EntityClassLoader loader = new ClassPoolUtils().new EntityClassLoader(parent);
            //();//同一个ClassLoader不能多次加载同一个类
            c = (loader , null);//替换Class的时候,  加载该Class的ClassLoader也必须用新的
            //();//解除冻结
            (); //注销class
        } catch (Exception e) {  
            ();  
        }  
        return c;  
    }
    public static Annotation createJavassistAnnotation(ConstPool constpool,
    		 annotation,String atMethodName, Object atMethodVal) throws Exception {
    	Annotation annotationCopy = new Annotation(().getName(), constpool);
    	String typeName = null;
    	MemberValue mv = null;
    	for (Method m : ().getDeclaredMethods()) {
    			Object value = (annotation);
    			typeName = ().getSimpleName();
    			if((())) value = atMethodVal;
    			if(("int"))
    				mv = new IntegerMemberValue(constpool,(Integer)value);
    			else if(("String[]")){
    				String[] stres = (String[])value;
    	            StringMemberValue[] elements = new StringMemberValue[];
    	            for(int i=0;i<;i++)
    	            	elements[i] = new StringMemberValue(stres[i],constpool);
    	            ArrayMemberValue amv = new ArrayMemberValue(constpool);
    	            (elements);
    	            mv = amv;
    			}
    			else if(("boolean"))
    				mv = new BooleanMemberValue((Boolean)value,constpool);
    			else mv = new StringMemberValue((),constpool);
    			((),mv);
    	}
    	return annotationCopy;
    }
    /**  
     * @Title: 
     * @Package 
     * @Description:  实体类加载器  
     * 该加载器主要用于运行时动态修改实体后,重新装载实体 
     * @author TangCai
     * @date 2016年11月16日 下午3:18:55
     * @version V1.0  
     */
    public class EntityClassLoader extends ClassLoader {
    	 private ClassLoader parent;
         
    	 public EntityClassLoader(ClassLoader parent){  
              = parent;  
         }  

         @Override  
         public Class<?> loadClass(String name) throws ClassNotFoundException {  
             return (name, false);  
         }  
         
         @Override  
         protected synchronized Class<?> loadClass(String name, boolean resolve)  
                 throws ClassNotFoundException {
             Class<?> clazz = (name);  
             if(null != parent)
                 clazz = (name);    
             if(null == clazz)
                 (name);  
             if(null == clazz)
                 throw new ClassNotFoundException();  
             if(null != clazz && resolve)
                 (clazz);
             return clazz;  
         }  
    }
}

相关文章