"Java tool class" BeanCopyUtil object replication tool class

Keywords: Java

Introduction

This number is mainly the sharing of common key technology points and common tool classes of Java; And technology sharing of integration frameworks such as springboot+springcloud+Mybatisplus+druid+mysql+redis+swagger+maven+docker; Technology sharing of big data processing frameworks such as datax, kafka and flink. The article will be updated constantly. Code friends are welcome to pay attention, like collection and forwarding!

I hope all code friends can click to pay attention and make 1000 powder. Some video tutorials will be recorded later, combining graphics and video, such as Book Introduction website system, rush purchase system, big data middle desk system, etc. Technology is the favorite of program apes. Code friends rush

If the coder thinks the code is too long, he can quickly scan it from beginning to end and understand it. If you find it useful, you can forward it for collection in case of need.

Text:

Object replication tool class, simple and easy to use. However, there are not many usage scenarios. Collect them first

Example 1

Copy student class to another student class

import java.time.LocalDate;
 ​
 public class BeanUtilTest {
     public static void main(String[] args) {
         Student student = new Student("zhang three",
                 12, "Huaguo Mountain", LocalDate.of(1990, 2, 26));
         Student studentCopy = new Student();
         BeanCopyUtil.merge(student, studentCopy);
 ​
         System.out.println(studentCopy);
     }
 }
 ​
 // Console printing
 Student(name=zhang three, age=12, address=Huaguo Mountain, birthday=1990-02-26)

Entity class used in the example

import lombok.AllArgsConstructor;
 import lombok.Data;
 import lombok.NoArgsConstructor;
 ​
 import java.time.LocalDate;
 ​
 @Data
 @AllArgsConstructor
 @NoArgsConstructor
 public class Student {
     private String name;
     private int age;
     private String address;
     private LocalDate birthday;
 }

Tool source code:

import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 ​
 import java.lang.reflect.Field;
 ​
 /**
  * Object assignment tool class
  *
  * @author liangxn
  */
 public class BeanCopyUtil {
 ​
     private static final Logger logger = LoggerFactory.getLogger(BeanCopyUtil.class);
 ​
     /**
      * Merge the same objects and assign the value of the non empty attribute of the original object to the target object
      *
      * @param origin      Source object
      * @param destination Target object
      * @param <T>         Type of object
      */
     public static <T> void merge(T origin, T destination) {
         if (origin == null || destination == null) {
             return;
         }
         if (!origin.getClass().equals(destination.getClass())) {
             return;
         }
 ​
         Field[] fields = origin.getClass().getDeclaredFields();
         for (Field field : fields) {
             field.setAccessible(true);
             try {
                 Object value = field.get(origin);
                 if (null != value) {
                     field.set(destination, value);
                 }
             } catch (IllegalAccessException e) {
                 logger.error("Exception accessing object", e);
             }
             field.setAccessible(false);
         }
     }
 }

I have been coding for more than ten years and have accumulated some tool classes in the project. Many tool classes are used in every project and are very practical. Most of them are encapsulated by me. Some tool classes are encapsulated by colleagues. Some tool classes don't remember whether they are ctrl+c or encapsulated by themselves. Now I will summarize most of the tool classes in the project and share them with you when I am free. If the code involved in the article has infringement, please notify me for handling.

The plan is to sort out the tools first. As the saying goes, if you want to do well, you must first sharpen the tools. In the project, whether it is an ordinary single project, a multi module maven project or a distributed microservice, some functional modules can be reused, and the tool module is one of them.

Posted by xuelun on Sun, 31 Oct 2021 03:53:10 -0700