对象序列化统一管理,升级注意,需更新此文件

This commit is contained in:
thinkgem
2020-03-29 19:04:40 +08:00
parent be38ce85c1
commit 36bcc65cd1

View File

@@ -7,7 +7,7 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
@@ -17,14 +17,14 @@ import org.springframework.beans.BeanUtils;
import org.springframework.core.NamedThreadLocal;
/**
* 对象操作工具类, 继承org.apache.commons.lang3.ObjectUtils类
* 对象操作工具类继承 org.apache.commons.lang3.ObjectUtils
* @author ThinkGem
* @version 2018-08-11
* @version 2020-3-29
*/
public class ObjectUtils extends org.apache.commons.lang3.ObjectUtils {
/**
* 转换为Double类型
* 转换为 Double 类型
*/
public static Double toDouble(final Object val) {
if (val == null) {
@@ -38,28 +38,29 @@ public class ObjectUtils extends org.apache.commons.lang3.ObjectUtils {
}
/**
* 转换为Float类型
* 转换为 Float 类型
*/
public static Float toFloat(final Object val) {
return toDouble(val).floatValue();
}
/**
* 转换为Long类型
* 转换为 Long 类型
*/
public static Long toLong(final Object val) {
return toDouble(val).longValue();
}
/**
* 转换为Integer类型
* 转换为 Integer 类型
*/
public static Integer toInteger(final Object val) {
return toLong(val).intValue();
}
/**
* 转换为Boolean类型 'true', 'on', 'y', 't', 'yes' or '1' (case insensitive) will return true. Otherwise, false is returned.
* 转换为 Boolean 类型 'true', 'on', 'y', 't', 'yes' or '1'
* (case insensitive) will return true. Otherwise, false is returned.
*/
public static Boolean toBoolean(final Object val) {
if (val == null) {
@@ -70,43 +71,35 @@ public class ObjectUtils extends org.apache.commons.lang3.ObjectUtils {
/**
* 转换为字符串
* @param obj
* @return
*/
public static String toString(final Object obj) {
return toString(obj, StringUtils.EMPTY);
}
/**
* 如果对象为空则使用defaultVal值
* @param obj
* @param defaultVal
* @return
* 转换为字符串,如果对象为空,则使用 defaultVal
*/
public static String toString(final Object obj, final String defaultVal) {
return obj == null ? defaultVal : obj.toString();
}
/**
* 空转空字符串("" to "" ; null to "" ; "null" to "" ; "NULL" to "" ; "Null" to ""
* @param val 需转换的值
* @return 返回转换后的值
* 转换为字符串,忽略空值。如 null 字符串,则被认为空值,如下:
* "" to "" ; null to "" ; "null" to "" ; "NULL" to "" ; "Null" to ""
*/
public static String toStringIgnoreNull(final Object val) {
return ObjectUtils.toStringIgnoreNull(val, StringUtils.EMPTY);
}
/**
* 空对象转空字符串 "" to defaultVal ; null to defaultVal ; "null" to defaultVal ; "NULL" to defaultVal ; "Null" to defaultVal
* @param val 需转换的值
* @param defaultVal 默认值
* @return 返回转换后的值
* 转换为字符串,忽略空值。如 null 字符串,则被认为空值,如下:
* "" to defaultVal ; null to defaultVal ; "null" to defaultVal ; "NULL" to defaultVal ; "Null" to defaultVal
*/
public static String toStringIgnoreNull(final Object val, String defaultVal) {
String str = ObjectUtils.toString(val);
return !"".equals(str) && !"null".equals(str.trim().toLowerCase()) ? str : defaultVal;
}
/**
* 拷贝一个对象(但是子对象无法拷贝)
* @param source
@@ -117,40 +110,26 @@ public class ObjectUtils extends org.apache.commons.lang3.ObjectUtils {
return null;
}
try {
Object target = source.getClass().newInstance();
Object target = source.getClass().getDeclaredConstructor().newInstance();
BeanUtils.copyProperties(source, target, ignoreProperties);
return target;
} catch (InstantiationException | IllegalAccessException e) {
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
| InvocationTargetException | NoSuchMethodException | SecurityException e) {
throw ExceptionUtils.unchecked(e);
}
}
/**
* 注解到对象复制,只复制能匹配上的方法。 硕正组件用。
* @param annotation
* @param object
* 克隆一个对象(完全拷贝)
* @param source
*/
public static void annotationToObject(Object annotation, Object object) {
if (annotation != null && object != null) {
Class<?> annotationClass = annotation.getClass();
Class<?> objectClass = object.getClass();
for (Method m : objectClass.getMethods()) {
if (StringUtils.startsWith(m.getName(), "set")) {
try {
String s = StringUtils.uncapitalize(StringUtils.substring(m.getName(), 3));
Object obj = annotationClass.getMethod(s).invoke(annotation);
if (obj != null && !"".equals(obj.toString())) {
// if (object == null){
// object = objectClass.newInstance();
// }
m.invoke(object, obj);
}
} catch (Exception e) {
// 忽略所有设置失败方法
}
}
}
public static Object cloneBean(Object source){
if (source == null){
return null;
}
byte[] bytes = ObjectUtils.serialize(source);
Object target = ObjectUtils.unserialize(bytes);
return target;
}
/**
@@ -159,6 +138,24 @@ public class ObjectUtils extends org.apache.commons.lang3.ObjectUtils {
* @return
*/
public static byte[] serialize(Object object) {
return ObjectUtils.serializeFst(object);
}
/**
* 反序列化对象
* @param bytes
* @return
*/
public static Object unserialize(byte[] bytes) {
return ObjectUtils.unserializeFst(bytes);
}
/**
* 序列化对象
* @param object
* @return
*/
public static byte[] serializeJava(Object object) {
if (object == null){
return null;
}
@@ -183,7 +180,7 @@ public class ObjectUtils extends org.apache.commons.lang3.ObjectUtils {
* @param bytes
* @return
*/
public static Object unserialize(byte[] bytes) {
public static Object unserializeJava(byte[] bytes) {
if (bytes == null){
return null;
}
@@ -204,8 +201,8 @@ public class ObjectUtils extends org.apache.commons.lang3.ObjectUtils {
return object;
}
// FST序列化配置对象
private static ThreadLocal<FSTConfiguration> fst = new NamedThreadLocal<FSTConfiguration>("FSTConfiguration") {
private static ThreadLocal<FSTConfiguration> fstConfiguration =
new NamedThreadLocal<FSTConfiguration>("FSTConfiguration") {
public FSTConfiguration initialValue() {
return FSTConfiguration.createDefaultConfiguration();
}
@@ -221,7 +218,7 @@ public class ObjectUtils extends org.apache.commons.lang3.ObjectUtils {
return null;
}
long beginTime = System.currentTimeMillis();
byte[] bytes = fst.get().asByteArray(object);
byte[] bytes = fstConfiguration.get().asByteArray(object);
long totalTime = System.currentTimeMillis() - beginTime;
if (totalTime > 3000){
System.out.println("Fst serialize time: " + TimeUtils.formatDateAgo(totalTime));
@@ -239,25 +236,66 @@ public class ObjectUtils extends org.apache.commons.lang3.ObjectUtils {
return null;
}
long beginTime = System.currentTimeMillis();
Object object = fst.get().asObject(bytes);
Object object = fstConfiguration.get().asObject(bytes);
long totalTime = System.currentTimeMillis() - beginTime;
if (totalTime > 3000){
System.out.println("Fst unserialize time: " + TimeUtils.formatDateAgo(totalTime));
}
return object;
}
/**
* 克隆一个对象(完全拷贝)
* @param source
*/
public static Object cloneBean(Object source){
if (source == null){
return null;
}
byte[] bytes = ObjectUtils.serializeFst(source);
Object target = ObjectUtils.unserializeFst(bytes);
return target;
}
// private static Pool<Kryo> kryoPool = new Pool<Kryo>(true, false, 8) {
// protected Kryo create() {
// Kryo kryo = new Kryo();
// kryo.setRegistrationRequired(false);
// kryo.setReferences(false);
// return kryo;
// }
// };
// private static Pool<Output> outputPool = new Pool<Output>(true, false, 16) {
// protected Output create() {
// return new Output(1024, -1);
// }
// };
// private static Pool<Input> inputPool = new Pool<Input>(true, false, 16) {
// protected Input create() {
// return new Input(1024);
// }
// };
//
// /**
// * Kryo 序列化对象
// * @param object
// * @return
// */
// public static byte[] serializeKryo(Object object) {
// Kryo kryo = kryoPool.obtain();
// Output output = outputPool.obtain();
// try {
// output.reset();
// kryo.writeClassAndObject(output, object);
// return output.toBytes();
// } finally {
// kryoPool.free(kryo);
// outputPool.free(output);
// }
// }
//
// /**
// * Kryo 反序列化对象
// * @param bytes
// * @return
// */
// public static Object unserializeKryo(byte[] bytes) {
// Kryo kryo = kryoPool.obtain();
// Input input = inputPool.obtain();
// try {
// input.setBuffer(bytes);
// return kryo.readClassAndObject(input);
// } finally {
// kryoPool.free(kryo);
// inputPool.free(input);
// }
// }
}