Thinking about copying one object to another in Java

Shengjin 2021-02-23 14:34:46
thinking copying object java


Spring Ecology is in Java It is widely used in the project , From architecture to technology application to common basic functions ,Spring It brings great convenience to our development . Today, when we turn to the export report function in the project , Find ways to copy objects frequently :

 BeanUtils.copyProperties;

Assign the attribute value of the source object to the target object ,Spring and Apache And some other frameworks provide us with copy methods of object properties :

org.springframework.beans.BeanUtils.copyProperties(Object source, Object target)

org.apache.commons.beanutils.BeanUtils.copyProperties(Object dest, Object orig)

When using, pay attention to the parameter order of these two methods , The source and the target are opposite . Reflection is used in both methods , On a whim, I built my own wheel for copying objects , The code is as follows :

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
public class ObjConvertor {
private static HashMap<String,HashMap<String,Method>> mapSetMethods=new HashMap<String,HashMap<String,Method>>();
private static HashMap<String,HashMap<String,Method>> mapGetMethods=new HashMap<String,HashMap<String,Method>>();
static
{
}
public static boolean Convert(Object src,Object dst) {
HashMap<String,Method> mapSrc=new HashMap<String,Method>();
HashMap<String,Method> mapDst=new HashMap<String,Method>();
if(mapGetMethods.containsKey(src.getClass().getTypeName()))
mapSrc=mapGetMethods.get(src.getClass().getTypeName());
else
{
Method srcMethods[]=src.getClass().getMethods();
mapGetMethods.put(src.getClass().getTypeName(),mapSrc);
for(Method method:srcMethods)
{
String name=method.getName();
if(name.startsWith("get"))
{
String prop=name.substring(3).toLowerCase();
mapSrc.put(prop, method);
}
}
}
if(mapSetMethods.containsKey(dst.getClass().getTypeName()))
mapDst=mapSetMethods.get(dst.getClass().getTypeName());
else
{
Method dstMethods[]=dst.getClass().getMethods();
mapSetMethods.put(dst.getClass().getTypeName(),mapDst);
for(Method method:dstMethods)
{
String name=method.getName();
if(name.startsWith("set"))
{
String prop=name.substring(3).toLowerCase();
mapDst.put(prop, method);
}
}
}
for(String prop:mapSrc.keySet())
{
if(mapDst.containsKey(prop)){
Method setMethod=mapDst.get(prop);
Method getMethod=mapSrc.get(prop);
try {
Object data=getMethod.invoke(src);
if(data==null)
continue;
if(data instanceof java.sql.Time)
{
if(setMethod.getParameterTypes()[0]==String.class)
data=data.toString();
}
else if(data instanceof java.sql.Date)
{
if(setMethod.getParameterTypes()[0]==String.class)
data=new SimpleDateFormat("yyyy-MM-dd").format(data);
}
else if(data instanceof java.util.Date)
{
if(setMethod.getParameterTypes()[0]==String.class)
data=new SimpleDateFormat("yyyy-MM-dd").format((java.util.Date)data);
}
else if(data instanceof java.sql.Timestamp)
{
if(setMethod.getParameterTypes()[0]==String.class)
data=data.toString();
}
if(setMethod.getParameterTypes()[0]==java.sql.Time.class)
{
if(data instanceof String)
data=java.sql.Time.valueOf((String)data);
}
else if(setMethod.getParameterTypes()[0]==java.sql.Date.class)
{
if(data instanceof String)
data=java.sql.Date.valueOf((String)data);
}
else if(setMethod.getParameterTypes()[0]==java.util.Date.class)
{
if(data instanceof String)
{
data=(new SimpleDateFormat("yyyy-MM-dd")).parse((String)data);
}
}
else if(setMethod.getParameterTypes()[0]==java.sql.Timestamp.class)
{
if(data instanceof String)
data=java.sql.Timestamp.valueOf((String)data);
}
if(data==null)
continue;
setMethod.invoke(dst, data);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
 e.printStackTrace();
return false;
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
 e.printStackTrace();
return false;
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
 e.printStackTrace();
return false;
} catch (ParseException e) {
// TODO Auto-generated catch block
 e.printStackTrace();
return false;
}
}
}
return true;
}
}

This is the end of the object copy function , If the project needs some special format data , Consider making your own wheels , It depends on the business needs .

Let's talk about object copying , If you are copying this object , Only the basic data types are copied , And the reference data type is just the transfer of reference , Instead of actually creating a new object , It's a shallow copy , such as Spring and Apache Of copyProperties And the copy method above . In turn, , When copying reference data types , Created a new object , And copy the member variables in it , It's a deep copy .

It's not difficult to find out by comparison : Deep copy creates a new object , It's just all the content of this object , It's as like as two peas , That is to say, the modification of the two is isolated , There is no influence on each other ; Shallow copy also creates an object , But something about this object ( such as A) It's still the copied object , That is, by modifying either of these two objects A, two-object A Will be affected .

Spring and Apache Of copyProperties They're all shallow copies , It can be used in daily development . It should be noted that , stay Java The development manual mentions , Not recommended for use Apache Of BeanUtils Tool class , Because there are a lot of tests 、 Type conversion 、 Log printing, etc , High implementation complexity , Poor performance .

So if we want to achieve high-performance and secure deep copy ? Deep copy means that all variables of the copied object have the same value as the original object , Remove variables that refer to other objects , Variables that refer to other objects will point to new objects that have been copied , Instead of trying the original referenced objects again , That is to say , Deep copy copies all the objects referenced by the object to be copied . I think the most reliable deep copy is to write stream after serialization , This approach needs to be implemented Serializable Interface , When you copy multiple copies , All reference types have to be implemented Serializable Interface . The code is as follows :

import java.io.*;
import java.util.Date;
public class CopyDemo {
class User implements Serializable {
private int id;
private String username;// User name 
private String sex;// Gender 
private Date birthday;// Birthday 
private String address;// Address 
private Person person; // Reference type 
public User myColon(){
User copy=null;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(this);
// Sequence streams into objects 
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
copy = (User) ois.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return copy;
}
// Omit get-set method code 
 }
class Person implements Serializable {
private int id;
private String userName ;
private int age ;
private String mobilePhone ;
public Person(){}
public Person(int id,String userName, int age, String mobilePhone) {
this.id = id;
this.userName = userName;
this.age = age;
this.mobilePhone = mobilePhone;
}
// Omit get-set Method 
 }
}

stay Java Deep copy of an object in a language , First, make the object implement Serializable Interface , And then put the object ( It's really just a copy of the object ) In a stream , Read it out of the stream , You can rebuild the object . You can see this process more clearly through the code . The process of writing objects into a stream is serialization (Serilization) The process , And reading objects out of the stream is parallelization (Deserialization) The process . It should be noted that , Objects that need to be copied may contain multiple reference types , Multi layer copy not only implements the serialization interface for the copy object , Reference objects also need to implement the serialization interface .

 

 

 

 

 

 

 

 


版权声明
本文为[Shengjin]所创,转载请带上原文链接,感谢
https://javamana.com/2021/02/20210223143351444H.html

  1. Add filter and execute filter in excel by Java
  2. Dialogue in spring
  3. 解决Docker MySQL无法被宿主机访问的问题
  4. Oracle OCP 19c 认证1Z0-083考试题库(第1题)
  5. Solve the problem that docker MySQL cannot be accessed by the host
  6. Oracle OCP 19C certification 1z0-083 examination question bank (question 1)
  7. 在 2021 年你需要掌握的 7 种关于 JavaScript 的数组方法
  8. Seven array methods for JavaScript you need to master in 2021
  9. 在 2021 年你需要掌握的 7 种关于 JavaScript 的数组方法
  10. Struts2 + Json _ 配置,异常解决及深入了解Struts2返回JSON数据的原理及具体应用范例
  11. Seven array methods for JavaScript you need to master in 2021
  12. Struts2 + Json _ Configuration, exception resolution and in-depth understanding of Struts2 return JSON data principle and specific application examples
  13. (三)MySQL锁机制 + 事务
  14. (3) MySQL lock mechanism + transaction
  15. 在 2021 年你需要掌握的 7 种关于 JavaScript 的数组方法
  16. Seven array methods for JavaScript you need to master in 2021
  17. 基于Kafka和Elasticsearch构建实时站内搜索功能的实践
  18. Practice of building real time search function in the website based on Kafka and elasticsearch
  19. Golang 实现 Redis(9): 使用GeoHash 搜索附近的人
  20. RxHttp - 轻量级、可扩展、易使用、完美兼容MVVM、MVC架构的网络封装类库
  21. Golang 实现 Redis(9): 使用GeoHash 搜索附近的人
  22. RxHttp - 轻量级、可扩展、易使用、完美兼容MVVM、MVC架构的网络封装类库
  23. Golang realizes redis (9): using geohash to search nearby people
  24. Rxhttp - lightweight, extensible, easy to use, perfectly compatible with MVVM, MVC architecture network encapsulation class library
  25. Golang realizes redis (9): using geohash to search nearby people
  26. Rxhttp - lightweight, extensible, easy to use, perfectly compatible with MVVM, MVC architecture network encapsulation class library
  27. 读懂框架设计的灵魂 — Java 反射机制
  28. 治疗磁盘空间不足焦虑症,释放容器占用空间——Win10+docker篇
  29. 别再用jodatime了!全网最权威Java8日期时间类LocalDate、LocalDateTime详解
  30. Understanding the soul of framework design java reflection mechanism
  31. 配置客户端以安全连接到Apache Kafka集群4:TLS客户端身份验证
  32. Treating anxiety of insufficient disk space and releasing space occupied by containers -- win10 + docker
  33. Don't use jodatime any more! The most authoritative java 8 date and time classes in the whole network: detailed explanation of localdate and localdatetime
  34. Configure clients to connect securely to Apache Kafka Cluster 4: TLS client authentication
  35. Spring break
  36. 高性能MySQL(三):Schema与数据类型优化
  37. High performance mysql (3): schema and data type optimization
  38. redis解决缓存、击穿、雪崩
  39. redis
  40. 骑士卡:基于Kafka搭建消息中心,上亿消息推送轻松完成
  41. Redis solves cache, breakdown and avalanche
  42. redis
  43. Knight card: build a message center based on Kafka, and push hundreds of millions of messages easily
  44. Oracle OCP 19c 认证1Z0-083考试题库(第2题)
  45. redis的三种模式
  46. kubernetes和docker----2.学习Pod资源
  47. 谈一谈如何远程访问MySQL(腾讯云,云主机)
  48. Linux(五):Linux的文档编辑器Vi
  49. Oracle OCP 19C certification 1z0-083 examination question bank (question 2)
  50. 云原生项目实践DevOps(GitOps)+K8S+BPF+SRE,从0到1使用Golang开发生产级麻将游戏服务器—第6篇
  51. kubernetes和docker----2.学习Pod资源
  52. JSP基于Java开发Web应用程序特点有哪些?
  53. Three modes of redis
  54. Kubernetes and docker -- 2. Learning pod resources
  55. Linux (5): the document editor VI of Linux
  56. Cloud native project practice Devops (gitops) + k8s + BPF + SRE, using golang to develop production level mahjong game server from 0 to 1
  57. Kubernetes and docker -- 2. Learning pod resources
  58. What are the characteristics of JSP developing web application based on Java?
  59. Lottie error: java.lang.AssertionError : android.util.JsonReader .peek
  60. Rxhttp - lightweight, extensible, easy to use, perfectly compatible with MVVM, MVC architecture network encapsulation class library