Browse Source

代码优化

woody 3 months ago
parent
commit
bbd99eb4b7

+ 4 - 2
framework-base/src/main/java/com/chelvc/framework/base/crypto/AbstractCipherHandler.java

@@ -40,7 +40,8 @@ public abstract class AbstractCipherHandler implements CipherHandler {
         if (StringUtils.isEmpty(plaintext)) {
             return plaintext;
         }
-        return Base64.encodeBase64String(this.encrypt(plaintext.getBytes(StandardCharsets.UTF_8)));
+        byte[] bytes = plaintext.getBytes(StandardCharsets.UTF_8);
+        return Base64.encodeBase64String(this.encrypt(bytes));
     }
 
     @Override
@@ -54,6 +55,7 @@ public abstract class AbstractCipherHandler implements CipherHandler {
         if (StringUtils.isEmpty(ciphertext)) {
             return ciphertext;
         }
-        return new String(this.decrypt(CodecUtils.decodeBase64(ciphertext)), StandardCharsets.UTF_8);
+        byte[] bytes = CodecUtils.decodeBase64(ciphertext);
+        return new String(this.decrypt(bytes), StandardCharsets.UTF_8);
     }
 }

+ 11 - 34
framework-common/src/main/java/com/chelvc/framework/common/util/ObjectUtils.java

@@ -599,22 +599,6 @@ public final class ObjectUtils {
         }
     }
 
-    /**
-     * 将对象转换成长整形数字
-     *
-     * @param object 目标对象
-     * @return 长整形数字
-     */
-    public static Long object2long(Object object) {
-        if (object == null || StringUtils.isEmpty(object)) {
-            return null;
-        } else if (object instanceof Byte || object instanceof Short || object instanceof Integer
-                || object instanceof Long) {
-            return ((Number) object).longValue();
-        }
-        return Long.parseLong(object.toString());
-    }
-
     /**
      * 将对象转换成属性名/值映射表
      *
@@ -622,40 +606,33 @@ public final class ObjectUtils {
      * @return 对象属性名/值映射表
      */
     public static Map<String, Object> object2map(Object object) {
-        if (object == null) {
-            return null;
-        }
-        Map<String, Field> fields = getClassFields(object.getClass());
-        if (isEmpty(fields)) {
-            return Collections.emptyMap();
-        }
-        Map<String, Object> map = Maps.newHashMapWithExpectedSize(fields.size());
-        fields.forEach((name, field) -> map.put(name, getObjectValue(object, field)));
-        return map;
+        return object == null ? null : object2map(object.getClass(), object);
     }
 
     /**
      * 将对象转换成属性名/值映射表
      *
-     * @param type   目标类型
+     * @param target 目标类型
      * @param object 对象实例
      * @return 对象属性名/值映射表
      */
-    public static Map<String, Object> object2map(@NonNull Class<?> type, Object object) {
+    public static Map<String, Object> object2map(@NonNull Class<?> target, Object object) {
         if (object == null) {
             return null;
         }
-        Map<String, Field> targets = getClassFields(type);
+        Map<String, Field> targets = getClassFields(target);
         if (isEmpty(targets)) {
             return Collections.emptyMap();
         }
-        Map<String, Field> fields = getClassFields(object.getClass());
-        if (isEmpty(fields)) {
+        Class<?> original = object.getClass();
+        boolean identical = original == target;
+        Map<String, Field> originals = identical ? targets : getClassFields(original);
+        if (isEmpty(originals)) {
             return Collections.emptyMap();
         }
-        Map<String, Object> map = Maps.newHashMapWithExpectedSize(fields.size());
-        fields.forEach((name, field) -> {
-            if (targets.containsKey(name)) {
+        Map<String, Object> map = Maps.newHashMapWithExpectedSize(targets.size());
+        targets.forEach((name, field) -> {
+            if (identical || (field = originals.get(name)) != null) {
                 map.put(name, getObjectValue(object, field));
             }
         });