1. 程式人生 > >兩個實體復制

兩個實體復制

ret static access package chm ace ignore source 小寫

技術交流群:233513714

package com.datad.dream.utils;

import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.DynaProperty;
import
org.apache.commons.beanutils.PropertyUtils; import org.apache.commons.beanutils.PropertyUtilsBean; import org.apache.commons.collections.map.LinkedMap; /** * <p> * Title:JavaBean對象轉換工具類 * </p> * <p> * Description:復制兩個不同Bean的相同屬性值; * </p> * * @author Lion *
@version 2.0 */ public class MyBeanUtils extends PropertyUtilsBean { public MyBeanUtils() { super(); } private static void convert(Object dest, Object orig) throws IllegalAccessException, InvocationTargetException { if (dest == null) { throw new IllegalArgumentException("No destination bean specified"); }
if (orig == null) { throw new IllegalArgumentException("No origin bean specified"); } if (orig instanceof DynaBean) { DynaProperty origDescriptors[] = ((DynaBean) orig).getDynaClass() .getDynaProperties(); for (int i = 0; i < origDescriptors.length; i++) { String name = origDescriptors[i].getName(); if (PropertyUtils.isWriteable(dest, name)) { Object value = ((DynaBean) orig).get(name); try { getInstance().setSimpleProperty(dest, name, value); } catch (Exception e) { ; // Should not happen } } } } else if (orig instanceof Map) { Iterator names = ((Map) orig).keySet().iterator(); while (names.hasNext()) { String name = (String) names.next(); if (PropertyUtils.isWriteable(dest, name)) { Object value = ((Map) orig).get(name); try { getInstance().setSimpleProperty(dest, name, value); } catch (Exception e) { ; // Should not happen } } } } else { PropertyDescriptor origDescriptors[] = PropertyUtils .getPropertyDescriptors(orig); for (int i = 0; i < origDescriptors.length; i++) { String name = origDescriptors[i].getName(); if ("class".equals(name)) { continue; } if (PropertyUtils.isReadable(orig, name) && PropertyUtils.isWriteable(dest, name)) { try { Object value = PropertyUtils.getSimpleProperty(orig, name); getInstance().setSimpleProperty(dest, name, value); } catch (IllegalArgumentException ie) { ; // Should not happen } catch (Exception e) { } } } } } /** * 對象拷貝 數據對象空值不拷貝到目標對象 * * @param dataObject * @param toObject * @throws NoSuchMethodException copy */ public static void copyBeanNotNull2Bean(Object databean, Object tobean) throws Exception { PropertyDescriptor origDescriptors[] = PropertyUtils .getPropertyDescriptors(databean); for (int i = 0; i < origDescriptors.length; i++) { String name = origDescriptors[i].getName(); if ("class".equals(name)) { continue; } if (PropertyUtils.isReadable(databean, name) && PropertyUtils.isWriteable(tobean, name)) { try { Object value = PropertyUtils.getSimpleProperty(databean, name); if (value != null) { getInstance().setSimpleProperty(tobean, name, value); } } catch (IllegalArgumentException ie) { ; // Should not happen } catch (Exception e) { ; // Should not happen } } } } /** * 把orig和dest相同屬性的value復制到dest中 * * @param dest * @param orig * @throws IllegalAccessException * @throws InvocationTargetException */ public static void copyBean2Bean(Object dest, Object orig) throws Exception { if (orig == null) { dest = null; return; } convert(dest, orig); } public static void copyBean2Map(Map map, Object bean) { PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(bean); for (int i = 0; i < pds.length; i++) { PropertyDescriptor pd = pds[i]; String propname = pd.getName(); try { Object propvalue = PropertyUtils.getSimpleProperty(bean, propname); map.put(propname, propvalue); } catch (IllegalAccessException e) { // e.printStackTrace(); } catch (InvocationTargetException e) { // e.printStackTrace(); } catch (NoSuchMethodException e) { // e.printStackTrace(); } } } /** * 將Map內的key與Bean中屬性相同的內容復制到BEAN中 * * @param bean Object * @param properties Map * @throws IllegalAccessException * @throws InvocationTargetException */ public static void copyMap2Bean(Object bean, Map properties) throws IllegalAccessException, InvocationTargetException { if ((bean == null) || (properties == null)) { return; } Iterator names = properties.keySet().iterator(); while (names.hasNext()) { String name = (String) names.next(); if (name == null) { continue; } Object value = properties.get(name); try { Class clazz = PropertyUtils.getPropertyType(bean, name); if (null == clazz) { continue; } String className = clazz.getName(); if (className.equalsIgnoreCase("java.sql.Timestamp")) { if (value == null || value.equals("")) { continue; } } getInstance().setSimpleProperty(bean, name, value); } catch (NoSuchMethodException e) { continue; } } } /** * 自動轉Map key值大寫 將Map內的key與Bean中屬性相同的內容復制到BEAN中 * * @param bean Object * @param properties Map * @throws IllegalAccessException * @throws InvocationTargetException */ public static void copyMap2Bean_Nobig(Object bean, Map properties) throws IllegalAccessException, InvocationTargetException { if ((bean == null) || (properties == null)) { return; } Iterator names = properties.keySet().iterator(); while (names.hasNext()) { String name = (String) names.next(); if (name == null) { continue; } Object value = properties.get(name); // 命名應該大小寫應該敏感(否則取不到對象的屬性) try { if (value == null) { // 不光Date類型,好多類型在null時會出錯 continue; // 如果為null不用設 (對象如果有特殊初始值也可以保留?) } Class clazz = PropertyUtils.getPropertyType(bean, name); if (null == clazz) { // 在bean中這個屬性不存在 continue; } String className = clazz.getName(); // 臨時對策(如果不處理默認的類型轉換時會出錯) if (className.equalsIgnoreCase("java.util.Date")) { value = new java.util.Date( ((java.sql.Timestamp) value).getTime());// wait to // do:貌似有時區問題, // 待進一步確認 } getInstance().setSimpleProperty(bean, name, value); } catch (NoSuchMethodException e) { continue; } } } /** * Map內的key與Bean中屬性相同的內容復制到BEAN中 對於存在空值的取默認值 * * @param bean Object * @param properties Map * @param defaultValue String * @throws IllegalAccessException * @throws InvocationTargetException */ public static void copyMap2Bean(Object bean, Map properties, String defaultValue) throws IllegalAccessException, InvocationTargetException { if ((bean == null) || (properties == null)) { return; } Iterator names = properties.keySet().iterator(); while (names.hasNext()) { String name = (String) names.next(); if (name == null) { continue; } Object value = properties.get(name); try { Class clazz = PropertyUtils.getPropertyType(bean, name); if (null == clazz) { continue; } String className = clazz.getName(); if (className.equalsIgnoreCase("java.sql.Timestamp")) { if (value == null || value.equals("")) { continue; } } if (className.equalsIgnoreCase("java.lang.String")) { if (value == null) { value = defaultValue; } } getInstance().setSimpleProperty(bean, name, value); } catch (NoSuchMethodException e) { continue; } } } /** * 對象屬性深度拷貝(JACKSON轉換) * * @param dest * @param source * @return * @throws Exception */ @SuppressWarnings("unchecked") public static void copyDeepBean2Bean(Object dest, Object source) throws Exception { if (source == null) { return; } Map<String, Object> map = new LinkedMap(); MyBeanUtils.copyBean2Map(map, source); MyBeanUtils.copyMap2Bean(dest, map); } }

原創不易,您的支持是我前進的動力

技術分享圖片技術分享圖片

兩個實體復制