optimize ConvertUtil.java

This commit is contained in:
开源海哥 2023-05-24 15:42:32 +08:00
parent 581cf938f2
commit 06c756d861

View File

@ -15,11 +15,6 @@
*/ */
package com.mybatisflex.core.util; package com.mybatisflex.core.util;
import com.mybatisflex.annotation.EnumValue;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger; import java.math.BigInteger;
import java.time.LocalDate; import java.time.LocalDate;
@ -27,8 +22,6 @@ import java.time.LocalDateTime;
import java.time.LocalTime; import java.time.LocalTime;
import java.time.temporal.Temporal; import java.time.temporal.Temporal;
import java.util.Date; import java.util.Date;
import java.util.List;
import java.util.Objects;
public class ConvertUtil { public class ConvertUtil {
@ -99,40 +92,9 @@ public class ConvertUtil {
} }
return Short.parseShort(value.toString()); return Short.parseShort(value.toString());
} else if (targetClass.isEnum()) { } else if (targetClass.isEnum()) {
Object[] enumConstants = targetClass.getEnumConstants(); EnumWrapper enumWrapper = EnumWrapper.of(targetClass);
List<Field> allFields = ClassUtil.getAllFields(targetClass, field -> field.getAnnotation(EnumValue.class) != null); if (enumWrapper.hasEnumValueAnnotation()) {
if (allFields.size() == 1) { return enumWrapper.toEnum(value);
Field field = allFields.get(0);
String fieldGetterName = "get" + StringUtil.firstCharToUpperCase(field.getName());
List<Method> allMethods = ClassUtil.getAllMethods(targetClass, method -> {
String methodName = method.getName();
return methodName.equals(fieldGetterName);
});
//getter
if (allMethods.size() == 1) {
Method getter = allMethods.get(0);
for (Object enumConstant : enumConstants) {
try {
Object enumValue = getter.invoke(enumConstant);
if (Objects.equals(enumValue, value)) {
return enumConstant;
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
//public field
else if (Modifier.isPublic(field.getModifiers())) {
for (Object enumConstant : enumConstants) {
if (Objects.equals(readPublicField(field, enumConstant), value)) {
return enumConstant;
}
}
}
} else if (value instanceof String) { } else if (value instanceof String) {
return Enum.valueOf(targetClass, value.toString()); return Enum.valueOf(targetClass, value.toString());
} }
@ -146,15 +108,6 @@ public class ConvertUtil {
} }
private static Object readPublicField(Field field, Object target) {
try {
return field.get(target);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
//Boolean.TYPE, Character.TYPE, Byte.TYPE, Short.TYPE, Integer.TYPE, Long.TYPE, Float.TYPE, Double.TYPE, Void.TYPE //Boolean.TYPE, Character.TYPE, Byte.TYPE, Short.TYPE, Integer.TYPE, Long.TYPE, Float.TYPE, Double.TYPE, Void.TYPE
public static Object getPrimitiveDefaultValue(Class<?> paraClass) { public static Object getPrimitiveDefaultValue(Class<?> paraClass) {
if (paraClass == int.class || paraClass == long.class || paraClass == float.class || paraClass == double.class) { if (paraClass == int.class || paraClass == long.class || paraClass == float.class || paraClass == double.class) {