From 36bcc65cd17e3a543cd2789f1287ef1b233b7a9a Mon Sep 17 00:00:00 2001 From: thinkgem Date: Sun, 29 Mar 2020 19:04:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9=E8=B1=A1=E5=BA=8F=E5=88=97=E5=8C=96?= =?UTF-8?q?=E7=BB=9F=E4=B8=80=E7=AE=A1=E7=90=86=EF=BC=8C=E5=8D=87=E7=BA=A7?= =?UTF-8?q?=E6=B3=A8=E6=84=8F=EF=BC=8C=E9=9C=80=E6=9B=B4=E6=96=B0=E6=AD=A4?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/jeesite/common/lang/ObjectUtils.java | 170 +++++++++++------- 1 file changed, 104 insertions(+), 66 deletions(-) diff --git a/common/src/main/java/com/jeesite/common/lang/ObjectUtils.java b/common/src/main/java/com/jeesite/common/lang/ObjectUtils.java index 657d10bf..cc6b6020 100644 --- a/common/src/main/java/com/jeesite/common/lang/ObjectUtils.java +++ b/common/src/main/java/com/jeesite/common/lang/ObjectUtils.java @@ -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 fst = new NamedThreadLocal("FSTConfiguration") { + private static ThreadLocal fstConfiguration = + new NamedThreadLocal("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 kryoPool = new Pool(true, false, 8) { +// protected Kryo create() { +// Kryo kryo = new Kryo(); +// kryo.setRegistrationRequired(false); +// kryo.setReferences(false); +// return kryo; +// } +// }; +// private static Pool outputPool = new Pool(true, false, 16) { +// protected Output create() { +// return new Output(1024, -1); +// } +// }; +// private static Pool inputPool = new Pool(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); +// } +// } }