`

Object1 to Object2

 
阅读更多

利用反射 将object1 转成 object2

package com.test.Date;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Date;
 
import org.apache.commons.lang.StringUtils;
public class ReflectUtils {
	/**
	* 
	* 
	* Property is obj1 to obj2
	* 
	* @param obj
	* @param <T>
	* @return
	* @throws Exception
	*/
	public static <T> T copyToProperties(Object obj, Class<T> clazz) throws Exception {
		if (obj != null) {
			Class<?> objClass = obj.getClass();
			T newObj = clazz.newInstance();
			Field[] newObjFields = clazz.getDeclaredFields();
			Field[] objFields = objClass.getDeclaredFields();
			for (Field newObjField : newObjFields) {
				if (!isNormalType(newObjField.getType())) {
					continue;
				}
				String newObjFieldName = newObjField.getName();
				for (Field objField : objFields) {
					if (!isNormalType(objField.getType())) {
						continue;
					}
					String objFieldName = objField.getName();
					if (newObjFieldName.equals(objFieldName)) {
						String upCase = StringUtils.capitalize(newObjFieldName);
						Method objMethod = objClass.getMethod("get" + upCase);
						Method newObjMethod = clazz.getMethod("set" + upCase, newObjField.getType());
						Object value = objMethod.invoke(obj);
						if (value != null && newObjMethod != null) {
							newObjMethod.invoke(newObj, value);
						}
					}
				}
			}
			return newObj;
		} else {
			return null;
		}
	}
	
	/**
	* copy properties from obj to target
	* @param oldObject
	* @param newObject
	* @throws Exception
	*/
	public static <T> void copyToProperties(Object oldObject, Object newObject) throws Exception {
		if (oldObject != null) {
			Class<?> oldClass = oldObject.getClass();
			Class<?> newClass = newObject.getClass();
			//T newObj = clazz.newInstance();
			Field[] newObjFields = newClass.getDeclaredFields();
			Field[] oldObjFields = oldClass.getDeclaredFields();
			for (Field newObjField : newObjFields) {
				if (!isNormalType(newObjField.getType())) {
				continue;
				}
				String newObjFieldName = newObjField.getName();
				for (Field objField : oldObjFields) {
					if (!isNormalType(objField.getType())) {
						continue;
					}
					String objFieldName = objField.getName();
					if (newObjFieldName.equals(objFieldName)) {
						/*
						* Object value = objField.get(obj);
						* newObjField.set(newObj, value);
						*/
						String upCase = StringUtils.capitalize(newObjFieldName);
						Method objMethod = oldClass.getMethod("get" + upCase);
						Method newObjMethod = newClass.getMethod("set" + upCase, newObjField.getType());
						Object value = objMethod.invoke(oldObject);
						if (value != null && newObjMethod != null) {
							newObjMethod.invoke(newObject, value);
						}
					}
				}
			}
		}
	}
 
	public static boolean isNormalType(Class<?> clazz) {
	if (clazz.equals(String.class) || clazz.equals(Integer.class) || clazz.equals(Integer.TYPE)
	|| clazz.equals(Boolean.class) || clazz.equals(Boolean.TYPE) || clazz.equals(Long.class)
	|| clazz.equals(Long.TYPE) || clazz.equals(Short.class) || clazz.equals(Short.TYPE)
	|| clazz.equals(Double.class) || clazz.equals(Double.TYPE) || clazz.equals(Float.class)
	|| clazz.equals(Float.TYPE) || clazz.equals(Date.class)) {
		return true;
	}
		return false;
	}
}

  

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics