利用反射 将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`来实现这一功能。 首先,让我们理解什么是对象序列化。对象序列化是将对象的状态转换为可存储或可传输的形式的过程。这通常涉及到将对象的...
通常,这个库会提供一个名为 `objectToArray` 的函数,如下所示: ```javascript const objectToArray = require('object-to-array'); const arr = objectToArray(obj); ``` 你也可以根据需要自定义转换逻辑,...
在物联网(IoT)领域, Lightweight Machine to Machine (LWM2M) 协议作为一种轻量级的通信协议,广泛应用于设备管理和服务提供。它允许远程访问和管理各种资源,如传感器、执行器等,以实现高效的数据交换和设备控制...
const objectToMap = require('object-to-map'); // 示例对象 const obj = { key1: 'value1', key2: 'value2', nested: { subKey: 'subValue' } }; // 转换为Map const myMap = objectToMap(obj); console....
ObjectToXml,object类型转换为xml类型,xml类型转换为object类型
An Object Oriented Approach to 3D Graphics--part1 <br>Object Oriented Approach 3D Graphics
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 <br>Object Oriented Approach 3D Graphics
例如,可以使用 Convert.ToByte() 方法将object转换为byte[],但是这只适用于简单的对象,例如字符串。 将object转换为byte[]需要选择合适的序列化方式, BinaryFormatter 和 JsonSerializer 是两个常用的选择。
1. **LINQ to SQL**: LINQ to SQL是.NET Framework 3.5引入的,用于处理SQL Server关系数据库的一种数据访问技术。它允许开发人员将数据库模式映射到.NET类,从而可以在C#或VB.NET代码中直接编写针对数据库的查询...
### LDAP提示Object Class Violation详解 #### 一、问题背景 在LDAP(Lightweight Directory Access Protocol,轻量目录访问协议)的使用过程中,有时会遇到一个常见的错误提示:“object class violation”。这一...
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 ...
对于要封装的PO,如果查询的不是所有列信息,则需要传递需要封装的列信息
the document describes how to build a cascade classifier for object detection with haar features
An Object Oriented Approach to 3D Graphics--part5 <br>Object Oriented Approach 3D Graphics
An Object Oriented Approach to 3D Graphics--part3 <br>Object Oriented Approach 3D Graphics
json 生成 解析 , ios ,android ,json , object to json
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 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 (Paper Explained),来自需要你懂得的网站视频,生肉版本。