1.2.8 新增ObjUtil 对类(对象)进行处理,提供深度克隆

This commit is contained in:
maxf 2018-12-28 20:15:10 +08:00
parent 5b79c1b383
commit 25ddef3bbf
5 changed files with 124 additions and 2 deletions

View File

@ -17,7 +17,7 @@
### 使用
>yexuejc.base.version=1.2.6
>yexuejc.base.version=1.2.8
pom.xml
```

View File

@ -1,6 +1,20 @@
yexuejc-base 更新记录
------------------
#### version 1.2.8
**time2018-12-28 20:10:14** <br/>
**branch** master <br/>
**update** <br/>
>1. 新增[ObjUtil](src/main/java/com/yexuejc/base/util/ObjUtil.java) 对类(对象)进行处理,提供深度克隆
#
#### version 1.2.6
**time2018-12-21 14:58:49** <br/>
**branch** master <br/>
**update** <br/>
>1. RSA 验签增加初始化方法
#
#### version 1.2.7
**time2018-12-24 15:31:01** <br/>
**branch** master <br/>

View File

@ -24,5 +24,12 @@ yexuejc-base 文档
##### 17. RegexUtils 常用正则
##### 18. StrUtil 字符串工具
##### 19. SysUtils 常用java系统操作封装
##### 20. ObjUtil 对象的操作(深度克隆)
> com.yexuejc.base.encrypt 加密相关
##### 21. RSA 加密
##### 22. RSA2 RSA加密获取文件密钥
##### 23. RSACoder RSA工具
##### 24. SignAlgorithm 签名算法类型
待完善......

View File

@ -6,7 +6,7 @@
<groupId>com.yexuejc.base</groupId>
<artifactId>yexuejc-base</artifactId>
<version>1.2.7</version>
<version>1.2.8</version>
<name>${project.artifactId}</name>

View File

@ -0,0 +1,101 @@
package com.yexuejc.base.util;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.sun.org.apache.xml.internal.serializer.OutputPropertyUtils;
import java.io.*;
/**
* 对象工具对类的操作
*
* @author maxf
* @version 1.0
* @ClassName ObjUtil
* @Description
* @date 2018/12/28 15:54
*/
public class ObjUtil {
private ObjUtil() {
}
/**
* <h2>深度克隆对象</h2>
* <p>
* 将该对象序列化成流,因为写在流里的是对象的一个拷贝而原对象仍然存在于JVM里面所以利用这个特性可以实现对象的深拷贝
* </p>
* <i>克隆对象必须序列化</i>
*
* @param t
* @param <T>
* @return
*/
public static <T> T depthClone(T t) {
T outer = null;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(t);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
outer = (T) ois.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return outer;
}
// public static void main(String[] args) {
//// test1();
//// test2();
//
// }
//
// private static void test2() {
// B t = new B();
// t.sex = "";
// t.age = 18;
// A test = new A();
// test.name = "张三";
// t.test = test;
// B b = depthClone(t);
// System.out.println(JsonUtil.obj2Json(b));
// }
//
// static class A implements Serializable {
// private static final long serialVersionUID = -8462118058721865488L;
// public String name;
// }
//
// static class B implements Serializable {
// private static final long serialVersionUID = 3297717505428005316L;
// public int age;
// public String sex;
// public A test;
// }
//
// static class C implements Serializable {
// private static final long serialVersionUID = 3297717505428005316L;
// public int age;
// public String sex;
// public A test;
// }
//
// private static void test1() {
// ApiVO apiVO = new ApiVO(ApiVO.STATUS.S);
// apiVO.setMsg("asdsadsad");
// apiVO.setObject1("sadsadsad");
//
// Resps<String> obj = new Resps<>();
// obj.setSucc("安达圣斗士", "ok");
// System.out.println(obj);
// apiVO.setObject2(obj);
// ApiVO apiVO1 = depthClone(apiVO);
// System.out.println(apiVO == apiVO1);
// System.out.println(JsonUtil.obj2Json(apiVO1));
// System.out.println(JsonUtil.obj2Json(apiVO1.getObject1(String.class)));
// System.out.println(JsonUtil.obj2Json(apiVO1.getObject2(Resps.class)));
// System.out.println(apiVO1.getObject2(Resps.class) == obj);
// }
}