package com.taover.util; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; public class UtilObject { /** * 将源对象source字段赋值到对象dest字段,条件1字段名相同,条件2字段类型相同; * 注:source与dest可以是两个没有关系的类,不接受基本封装类型,例如Integer * @param source * @param dest * @param allowNull * @param exceptFieldNameArray * @return 如果source==dest或者source==null或者dest==null,则返回false */ public static Object fieldCopy(Object source, Object dest, boolean allowNull, String exceptFieldNameArray[], boolean isNewInstance){ //引用检查 if(source==null || dest==null || source==dest){ return null; } //获取Field数组 Field[] sourceField = source.getClass().getDeclaredFields(); Field[] destField = dest.getClass().getDeclaredFields(); //获取目标对象 String[] destFieldNameArray = new String[destField.length]; String[] destFieldTypeArray = new String[destField.length]; for(int i=0; i E fieldCopy(Object source, Class destClass, boolean allowNull, String exceptFieldNameArray[]) throws Exception{ //引用检查 if(source==null || destClass==null){ return null; } //获取Field数组 Field[] sourceField = source.getClass().getDeclaredFields(); Field[] destField = destClass.getDeclaredFields(); //获取目标对象 String[] destFieldNameArray = new String[destField.length]; String[] destFieldTypeArray = new String[destField.length]; for(int i=0; i hello_word * @param source * @param isCopyNull * @param exceptFieldNameArray * @return */ public static List objecFieldToListToLowerCase(Object source, boolean isCopyNull, String exceptFieldNameArray[]){ List result = new ArrayList(); //参数检验 if(source == null){ return result; } //获取Field数组 Field[] sourceField = source.getClass().getDeclaredFields(); //遍历源field数组,处理源field数组 for(int i=0; i fieldToArray(Object source, boolean allowNull, String exceptFieldNameArray[]){ List result = new ArrayList(); //参数检验 if(source == null){ return result; } //获取Field数组 Field[] sourceField = source.getClass().getDeclaredFields(); //遍历源field数组,处理源field数组 for(int i=0; i E mapToObject(Map dataMap, Class beanClass, boolean isCopyNull, String exceptFieldNameArray[]) throws Exception{ if(beanClass == null){ return null; } E result = beanClass.newInstance(); Iterator keyIter = dataMap.keySet().iterator(); while(keyIter.hasNext()){ String keyName = keyIter.next(); String camelName = UtilString.camelName(keyName); Object keyValue = dataMap.get(keyName); if(keyValue == null){ continue; } Field keyField = beanClass.getDeclaredField(camelName); String keyTypeName = keyField.getType().getSimpleName(); //如果是final关键词修饰,则返回 if(Modifier.isFinal(keyField.getModifiers())){ continue; } if(keyTypeName.equals("String")){ keyField.set(result, keyValue.toString()); }else if(keyTypeName.equals("Integer")){ keyField.set(result, Integer.valueOf(keyValue.toString())); } } return result; } public static E mapToObject(Map dataMap, Class beanClass) throws Exception{ if(beanClass == null){ return null; } E result = beanClass.newInstance(); Iterator keyIter = dataMap.keySet().iterator(); while(keyIter.hasNext()){ String keyName = keyIter.next(); String camelName = UtilString.camelName(keyName); Object keyValue = dataMap.get(keyName); if(keyValue == null){ continue; } try { Field keyField = beanClass.getDeclaredField(camelName); //如果是final关键词修饰,则返回 if(Modifier.isFinal(keyField.getModifiers())){ continue; } keyField.setAccessible(true); keyField.set(result, keyValue); } catch (Exception e) { e.printStackTrace(); } } return result; } public static void main(String args[]){ class Temp2{ final Integer a; Integer c; public Temp2(Integer a, Integer c){ this.a = a; this.c = c; } @Override public String toString(){ return "a="+a+":c="+c; } } class Temp{ Integer a; Integer b; public Temp(Integer a, Integer b){ this.a = a; this.b = b; } @Override public String toString(){ return "a="+a+":b="+b; } } Temp temp1 = new Temp(null,2); Temp temp2 = new Temp(3,4); Temp temp3 = new Temp(5,6); Temp2 temp4 = new Temp2(7,8); try { Field a= temp4.getClass().getDeclaredField("a"); System.out.print(Modifier.isFinal(a.getModifiers())); } catch (NoSuchFieldException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } /*fieldCopy(temp1, temp2, false, null,false); System.out.println(temp1.toString()); System.out.println(temp2.toString()); fieldCopy(temp3, temp4, false, null,false); System.out.println(temp3.toString()); System.out.println(temp4.toString()); List temp = UtilObject.fieldToArray(temp1, false, new String[]{"b"}); for(int i=0; i