UtilObject.java 10.1 KB
package com.taover.util;

import java.lang.reflect.Field;
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<destField.length; ++i){
			destFieldNameArray[i] = destField[i].getName();
			destFieldTypeArray[i] = destField[i].getType().getName();
		}
	
		Object newDest = null;
		//注入属性
		if(isNewInstance){
			try {
				newDest = dest.getClass().newInstance();
				for(int i=0; i<destField.length; ++i){
					destField[i].setAccessible(true);
					Object tempValue = destField[i].get(dest);
					destField[i].set(newDest, tempValue);
				}
			} catch (Exception e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}
		}
		
		//遍历源field数组,处理源field数组
		for(int i=0; i<sourceField.length; ++i){
			//获取源field相关信息
			Object sourceFieldValue = null;
			String sourceFieldName = sourceField[i].getName();
			String sourceFieldType = sourceField[i].getType().getName();
			
			//获取sourceFieldValue值
			sourceField[i].setAccessible(true);
			try {
				sourceFieldValue = sourceField[i].get(source);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			//检查是否在exceptFieldName中
			if(-1 != UtilString.getStringIndex(sourceFieldName, exceptFieldNameArray)){
				continue;
			}
			
			//检验sourceFieldValue是否为NULL
			if(!allowNull && sourceFieldValue == null){
				continue;
			}
			
			//检查源field字段名是否在目标field组中存在
			int nameIndex = UtilString.getStringIndex(sourceFieldName, destFieldNameArray);
			if(nameIndex == -1){
				continue;
			}else{
				//如果源field与目标field数据类型相同,则将sourceFieldValue赋值给目标对象
				if(sourceFieldType.equals(destFieldTypeArray[nameIndex])){
					destField[nameIndex].setAccessible(true);
					try {
						destField[nameIndex].set(dest, sourceFieldValue);
						if(isNewInstance){
							destField[nameIndex].set(newDest, sourceFieldValue);	
						}
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
		
		return newDest;
	}
	
	/**
	 * 将源对象source字段赋值到对象dest字段,条件1字段名相同,条件2字段类型相同;
	 * 注:source与dest可以是两个没有关系的类,不接受基本封装类型,例如Integer
	 * @param source
	 * @param destClass
	 * @param allowNull
	 * @param exceptFieldNameArray
	 * @return 如果source==null或者destClass==null,则返回false
	 */
	public static <E> E fieldCopy(Object source, Class<E> 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<destField.length; ++i){
			destFieldNameArray[i] = destField[i].getName();
			destFieldTypeArray[i] = destField[i].getType().getName();
		}
	
		E newDest = destClass.newInstance();
				
		//遍历源field数组,处理源field数组
		for(int i=0; i<sourceField.length; ++i){
			//获取源field相关信息
			Object sourceFieldValue = null;
			String sourceFieldName = sourceField[i].getName();
			String sourceFieldType = sourceField[i].getType().getName();
			
			//获取sourceFieldValue值
			sourceField[i].setAccessible(true);
			try {
				sourceFieldValue = sourceField[i].get(source);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			
			//检查是否在exceptFieldName中
			if(-1 != UtilString.getStringIndex(sourceFieldName, exceptFieldNameArray)){
				continue;
			}
			
			//检验sourceFieldValue是否为NULL
			if(!allowNull && sourceFieldValue == null){
				continue;
			}
			
			//检查源field字段名是否在目标field组中存在
			int nameIndex = UtilString.getStringIndex(sourceFieldName, destFieldNameArray);
			if(nameIndex == -1){
				continue;
			}else{
				//如果源field与目标field数据类型相同,则将sourceFieldValue赋值给目标对象
				if(sourceFieldType.equals(destFieldTypeArray[nameIndex])){
					destField[nameIndex].setAccessible(true);
					try {
						destField[nameIndex].set(newDest, sourceFieldValue);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}
		
		return newDest;
	}
	
	/**
	 * 将对象的所有属性转换到list数组中以objce数组存储,数组长度为2,第一个为属性名,第二个为属性值,如果对象属性值为null,则跳过该属性
	 * 在字符串驼峰命名转化成下划线形式
	 * 如 helloWord -> hello_word
	 * @param source
	 * @param isCopyNull
	 * @param exceptFieldNameArray
	 * @return
	 */
	public static List<Object[]> objecFieldToListToLowerCase(Object source, boolean isCopyNull, String exceptFieldNameArray[]){
		List<Object[]> result = new ArrayList<Object[]>();
		//参数检验
		if(source == null){
			return result;
		}
		
		//获取Field数组
		Field[] sourceField = source.getClass().getDeclaredFields();
		//遍历源field数组,处理源field数组
		for(int i=0; i<sourceField.length; ++i){
			//获取源field相关信息
			Object sourceFieldValue = null;
			String sourceFieldName = sourceField[i].getName();
			
			//获取sourceFieldValue值
			sourceField[i].setAccessible(true);
			try {
				sourceFieldValue = sourceField[i].get(source);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			//检查是否在exceptFieldName中
			if(-1 != UtilString.getStringIndex(sourceFieldName, exceptFieldNameArray)){
				continue;
			}
			
			//检验sourceFieldValue是否为NULL
			if(!isCopyNull && sourceFieldValue == null){
				continue;
			}
			sourceFieldName = UtilString.underscoreName(sourceFieldName).toLowerCase();
			//添加属性
			result.add(new Object[]{sourceFieldName, sourceFieldValue});
		}
		
		return result;
	}
	
	/**
	 * 将对象的所有属性转换到list数组中以objce数组存储,数组长度为2,第一个为属性名,第二个为属性值,如果对象属性值为null,则跳过该属性
	 * @param source
	 * @param allowNull
	 * @param exceptFieldNameArray
	 * @return
	 */
	public static List<Object[]> fieldToArray(Object source, boolean allowNull, String exceptFieldNameArray[]){
		List<Object[]> result = new ArrayList<Object[]>();
		//参数检验
		if(source == null){
			return result;
		}
		
		//获取Field数组
		Field[] sourceField = source.getClass().getDeclaredFields();
		//遍历源field数组,处理源field数组
		for(int i=0; i<sourceField.length; ++i){
			//获取源field相关信息
			Object sourceFieldValue = null;
			String sourceFieldName = sourceField[i].getName();
			
			//获取sourceFieldValue值
			sourceField[i].setAccessible(true);
			try {
				sourceFieldValue = sourceField[i].get(source);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			//检查是否在exceptFieldName中
			if(-1 != UtilString.getStringIndex(sourceFieldName, exceptFieldNameArray)){
				continue;
			}
			
			//检验sourceFieldValue是否为NULL
			if(!allowNull && sourceFieldValue == null){
				continue;
			}
			
			//添加属性
			result.add(new Object[]{sourceFieldName, sourceFieldValue});
		}
		
		return result;
	}	
	
	public static <E> E mapToObject(Map<String, Object> dataMap, Class<E> beanClass, boolean isCopyNull, String exceptFieldNameArray[]) throws Exception{
		if(beanClass == null){
			return null;
		}
		E result = beanClass.newInstance();
		Iterator<String> 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();
			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 void main(String args[]){
		
		class Temp2{
			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);
		
		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<Object[]> temp = UtilObject.fieldToArray(temp1, false, new String[]{"b"});
		for(int i=0; i<temp.size(); ++i){
			System.out.println(temp.get(i)[0]+":"+temp.get(i)[1]);	
		}		
	}
}