Forráskód Böngészése

优化自定义枚举实例初始化逻辑

Woody 6 napja
szülő
commit
2ec642a18e

+ 54 - 10
framework-common/src/main/java/com/chelvc/framework/common/model/Enumeration.java

@@ -5,7 +5,9 @@ import java.io.Serializable;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Field;
 import java.lang.reflect.Modifier;
+import java.util.Arrays;
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
@@ -138,6 +140,56 @@ public abstract class Enumeration implements Serializable {
         INSTANCES.computeIfAbsent(clazz, k -> Maps.newConcurrentMap()).putIfAbsent(this.code, this);
     }
 
+    /**
+     * 生成枚举实例
+     *
+     * @param type 枚举类型
+     * @param <T>  对象类型
+     * @return 枚举实例
+     */
+    @SuppressWarnings("unchecked")
+    private static <T extends Enumeration> T generateInstance(Class<T> type) {
+        Constructor<?>[] constructors = type.getDeclaredConstructors();
+        if (constructors.length > 1) {
+            Arrays.sort(constructors, Comparator.comparingInt(Constructor::getParameterCount));
+        }
+        Constructor<?> constructor = constructors[0];
+        Object[] parameters = new Object[constructor.getParameterCount()];
+        if (parameters.length > 0) {
+            Class<?>[] types = constructor.getParameterTypes();
+            for (int i = 0; i < parameters.length; i++) {
+                Class<?> clazz = types[i];
+                if (clazz == char.class) {
+                    parameters[i] = (char) 0;
+                } else if (clazz == byte.class) {
+                    parameters[i] = (byte) 0;
+                } else if (clazz == int.class) {
+                    parameters[i] = 0;
+                } else if (clazz == short.class) {
+                    parameters[i] = (short) 0;
+                } else if (clazz == long.class) {
+                    parameters[i] = 0L;
+                } else if (clazz == float.class) {
+                    parameters[i] = 0.0F;
+                } else if (clazz == double.class) {
+                    parameters[i] = 0.00D;
+                } else if (clazz == boolean.class) {
+                    parameters[i] = false;
+                } else {
+                    parameters[i] = null;
+                }
+            }
+        }
+        if (!constructor.isAccessible()) {
+            constructor.setAccessible(true);
+        }
+        try {
+            return (T) constructor.newInstance(parameters);
+        } catch (ReflectiveOperationException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
     /**
      * 获取枚举值,需要返回一个可更新列表
      *
@@ -194,16 +246,8 @@ public abstract class Enumeration implements Serializable {
                     return (T) instance;
                 }
 
-                // 使用无参构造方法初始化枚举实例
-                try {
-                    Constructor<T> constructor = type.getDeclaredConstructor();
-                    if (!constructor.isAccessible()) {
-                        constructor.setAccessible(true);
-                    }
-                    instance = constructor.newInstance();
-                } catch (ReflectiveOperationException e) {
-                    throw new RuntimeException(e);
-                }
+                // 初始化枚举实例
+                instance = generateInstance(type);
             }
 
             // 如果不是动态枚举则重新获取,保证一致性