一种java拷贝属性的方法

来源:互联网 发布:python多线程selenium 编辑:程序博客网 时间:2024/05/18 01:51
  1. <pre code_snippet_id="129671" snippet_file_name="blog_20131226_1_6439347" name="code" class="java">import java.lang.annotation.Documented;  
  2. import java.lang.annotation.ElementType;  
  3. import java.lang.annotation.Inherited;  
  4. import java.lang.annotation.Retention;  
  5. import java.lang.annotation.RetentionPolicy;  
  6. import java.lang.annotation.Target;  
  7.   
  8.   
  9. @Target(ElementType.METHOD)  
  10. @Retention(RetentionPolicy.RUNTIME)  
  11. @Documented  
  12. @Inherited  
  13. public @interface Convert {  
  14.     public String from();  
  15.     public String to();  
  16. }  
  17.   
  18. /** 
  19.      * 将fromObject对象转换为toObject对象,fromObject必须在对应的get方法上有Convert注解 
  20.      */  
  21.     public static void convertTo(Object fromObject, Object toObject)  {  
  22.         if (fromObject != null) {  
  23.             Method[] methods = fromObject.getClass().getMethods();  
  24.             for (Method method : methods) {  
  25.                 if (method.isAnnotationPresent(Convert.class)) {  
  26.                     Convert annotation = method.getAnnotation(Convert.class);  
  27.                     String express = annotation.to();  
  28.                     Object value;  
  29.                     try {  
  30.                         value = method.invoke(fromObject);  
  31.                         if (value != null) {  
  32.                             logger.debug(fromObject.getClass().getName() + " " + express + " is not null");  
  33.                             Ognl.setValue(express, toObject, value);  
  34.                         }  
  35.                     } catch (OgnlException e) {  
  36.                         logger.error(fromObject.getClass().getName() + " " + express + " convert ognl exception:", e);  
  37.                     } catch (IllegalArgumentException e) {  
  38.                         e.printStackTrace();  
  39.                         logger.error(fromObject.getClass().getName() + " " + express + " convert argument exception:", e);  
  40.   
  41.                     } catch (IllegalAccessException e) {  
  42.                         logger.error(fromObject.getClass().getName() + " " + express + " convert access exception:", e);  
  43.                           
  44.                     } catch (InvocationTargetException e) {  
  45.                         logger.error(fromObject.getClass().getName() + " " + express + " convert invocation exception:", e);  
  46.                           
  47.                     }  
  48.                 }  
  49.             }  
  50.         }  
  51.     }  
  52.       
  53.       
  54.       
  55.     /** 
  56.      * 将fromObject对象转换为toObject对象,toObject必须在对应的set方法上有Convert注解 
  57.      */  
  58.     public static void convert(Object fromObject, Object toObject)  {  
  59.         if (fromObject != null) {  
  60.             Method[] methods = toObject.getClass().getMethods();  
  61.             for (Method method : methods) {  
  62.                 if (method.isAnnotationPresent(Convert.class)) {  
  63.                     Convert annotation = method.getAnnotation(Convert.class);  
  64.                     String express = annotation.from();  
  65.                     Object value;  
  66.                     try {  
  67.                         value = Ognl.getValue(express, fromObject);  
  68.                         if (value != null) {  
  69.                             logger.debug(fromObject.getClass().getName() + " " + express + " is not null");  
  70.                             method.invoke(toObject, value);  
  71.                         }  
  72.                     } catch (OgnlException e) {  
  73.                         logger.error(fromObject.getClass().getName() + " " + express + " convert ognl exception:", e);  
  74.                           
  75.                     } catch (IllegalArgumentException e) {  
  76.                         logger.error(fromObject.getClass().getName() + " " + express + " convert argument exception:", e);  
  77.                           
  78.                     } catch (IllegalAccessException e) {  
  79.                         logger.error(fromObject.getClass().getName() + " " + express + " convert access exception:", e);  
  80.                           
  81.                     } catch (InvocationTargetException e) {  
  82.                         logger.error(fromObject.getClass().getName() + " " + express + " convert invocation exception:", e);  
  83.                           
  84.                     }  
  85.                 }  
  86.             }  
  87.         }  
  88.     }  
  89.       
  90.   
  91.       
  92.       
  93.       
  94.       
  95.       
  96.       
  97.       
  98.       
  99.       
  100.       
  101.       
  102.       
  103.       
  104.       
  105.       
  106.       
  107.       
  108.       
  109.     </pre><br>  
  110. <br>  
  111. <pre></pre>  
  112.      
0 0
原创粉丝点击