`

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;
	}
}

  

 

 

分享到:
评论

相关推荐

    简单对象序列化 扩展Object.ToJson

    本文将深入探讨“简单对象序列化”以及如何通过扩展方法`Object.ToJson`来实现这一功能。 首先,让我们理解什么是对象序列化。对象序列化是将对象的状态转换为可存储或可传输的形式的过程。这通常涉及到将对象的...

    前端开源库-object-to-array

    通常,这个库会提供一个名为 `objectToArray` 的函数,如下所示: ```javascript const objectToArray = require('object-to-array'); const arr = objectToArray(obj); ``` 你也可以根据需要自定义转换逻辑,...

    lwm2m下object和resource定义_OBJECT和RESOURCE_lwm2m_LWM2Mobj_lwm2mobje

    在物联网(IoT)领域, Lightweight Machine to Machine (LWM2M) 协议作为一种轻量级的通信协议,广泛应用于设备管理和服务提供。它允许远程访问和管理各种资源,如传感器、执行器等,以实现高效的数据交换和设备控制...

    前端开源库-object-to-map

    const objectToMap = require('object-to-map'); // 示例对象 const obj = { key1: 'value1', key2: 'value2', nested: { subKey: 'subValue' } }; // 转换为Map const myMap = objectToMap(obj); console....

    ObjectToXml

    ObjectToXml,object类型转换为xml类型,xml类型转换为object类型

    An Object Oriented Approach to 3D Graphics--part1

    An Object Oriented Approach to 3D Graphics--part1 &lt;br&gt;Object Oriented Approach 3D Graphics

    VTK_The Visualization Toolkit An Object Oriented Approach to 3D Graphics(1)

    VTK_The Visualization Toolkit An Object Oriented Approach to 3D Graphics, 3rd Edition - Kitware Inc.part1.rar 一共有3个压缩包,70多MB ,完整版, 学习VTK必读书目

    An Object Oriented Approach to 3D Graphics--part2

    An Object Oriented Approach to 3D Graphics--part2 &lt;br&gt;Object Oriented Approach 3D Graphics

    c#实现object与byte[]互转

    例如,可以使用 Convert.ToByte() 方法将object转换为byte[],但是这只适用于简单的对象,例如字符串。 将object转换为byte[]需要选择合适的序列化方式, BinaryFormatter 和 JsonSerializer 是两个常用的选择。

    Linq 大全 Linq To Sql Linq To DataSet Linq To Object Linq to Xml

    1. **LINQ to SQL**: LINQ to SQL是.NET Framework 3.5引入的,用于处理SQL Server关系数据库的一种数据访问技术。它允许开发人员将数据库模式映射到.NET类,从而可以在C#或VB.NET代码中直接编写针对数据库的查询...

    A Guide to MATLAB Object-Oriented Programming

    A Guide to MATLAB Object-Oriented Programming is the first book to deliver broad coverage of the documented and undocumented object-oriented features of MATLAB®. Unlike the typical approach of other ...

    ToObject工具类

    对于要封装的PO,如果查询的不是所有列信息,则需要传递需要封装的列信息

    ldap提示object class violation

    ### LDAP提示Object Class Violation详解 #### 一、问题背景 在LDAP(Lightweight Directory Access Protocol,轻量目录访问协议)的使用过程中,有时会遇到一个常见的错误提示:“object class violation”。这一...

    An Object Oriented Approach to 3D Graphics--part5

    An Object Oriented Approach to 3D Graphics--part5 &lt;br&gt;Object Oriented Approach 3D Graphics

    An Object Oriented Approach to 3D Graphics--part3

    An Object Oriented Approach to 3D Graphics--part3 &lt;br&gt;Object Oriented Approach 3D Graphics

    jsontoobject

    json 生成 解析 , ios ,android ,json , object to json

    Object Tracking A Survey

    Difficulties in tracking objects can arise due to abrupt object motion, changing appearance patterns of both the object and the scene, nonrigid object structures, object-to-object and object-to-scene...

    linq to object

    LINQ to OBJECT string[] strOne = new string[] { "123", "123", "abc", "qaz", "wsx", "yhn" }; string[] strTwo = new string[] { "123", "345", "abc", "ujm" }; var v_Inner = from tempOne in strOne join ...

    DETR- End-to-End Object Detection with Transformers 论文解析Yannic Kilcher版本

    DETR- End-to-End Object Detection with Transformers (Paper Explained),来自需要你懂得的网站视频,生肉版本。

Global site tag (gtag.js) - Google Analytics