mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 09:08:24 +08:00
optimize EnumWrapper.java
This commit is contained in:
parent
db17b832a4
commit
ccf9d38b37
@ -45,7 +45,7 @@ public class FlexEnumTypeHandler<E extends Enum<E>> extends BaseTypeHandler<E> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public E getNullableResult(ResultSet rs, String columnName) throws SQLException {
|
public E getNullableResult(ResultSet rs, String columnName) throws SQLException {
|
||||||
Object value = rs.getObject(columnName, enumWrapper.getEnumPropertyType());
|
Object value = rs.getObject(columnName, enumWrapper.getPropertyType());
|
||||||
if (null == value && rs.wasNull()) {
|
if (null == value && rs.wasNull()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -55,7 +55,7 @@ public class FlexEnumTypeHandler<E extends Enum<E>> extends BaseTypeHandler<E> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public E getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
|
public E getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
|
||||||
Object value = rs.getObject(columnIndex, enumWrapper.getEnumPropertyType());
|
Object value = rs.getObject(columnIndex, enumWrapper.getPropertyType());
|
||||||
if (null == value && rs.wasNull()) {
|
if (null == value && rs.wasNull()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -65,7 +65,7 @@ public class FlexEnumTypeHandler<E extends Enum<E>> extends BaseTypeHandler<E> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public E getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
|
public E getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
|
||||||
Object value = cs.getObject(columnIndex, enumWrapper.getEnumPropertyType());
|
Object value = cs.getObject(columnIndex, enumWrapper.getPropertyType());
|
||||||
if (null == value && cs.wasNull()) {
|
if (null == value && cs.wasNull()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -29,13 +29,13 @@ public class EnumWrapper<E extends Enum<E>> {
|
|||||||
|
|
||||||
private static final Map<Class, EnumWrapper> cache = new ConcurrentHashMap<>();
|
private static final Map<Class, EnumWrapper> cache = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
private Class<?> enumClass;
|
private boolean hasEnumValueAnnotation = false;
|
||||||
|
|
||||||
private Class<?> enumPropertyType;
|
private Class<?> enumClass;
|
||||||
private E[] enums;
|
private E[] enums;
|
||||||
private Field property;
|
private Field property;
|
||||||
private Method getter;
|
private Class<?> propertyType;
|
||||||
private boolean hasEnumValueAnnotation = false;
|
private Method getterMethod;
|
||||||
|
|
||||||
public static <R extends Enum<R>> EnumWrapper<R> of(Class<?> enumClass) {
|
public static <R extends Enum<R>> EnumWrapper<R> of(Class<?> enumClass) {
|
||||||
return MapUtil.computeIfAbsent(cache, enumClass, EnumWrapper::new);
|
return MapUtil.computeIfAbsent(cache, enumClass, EnumWrapper::new);
|
||||||
@ -43,6 +43,7 @@ public class EnumWrapper<E extends Enum<E>> {
|
|||||||
|
|
||||||
public EnumWrapper(Class<E> enumClass) {
|
public EnumWrapper(Class<E> enumClass) {
|
||||||
this.enumClass = enumClass;
|
this.enumClass = enumClass;
|
||||||
|
this.enums = enumClass.getEnumConstants();
|
||||||
|
|
||||||
Field enumValueField = ClassUtil.getFirstField(enumClass, field -> field.getAnnotation(EnumValue.class) != null);
|
Field enumValueField = ClassUtil.getFirstField(enumClass, field -> field.getAnnotation(EnumValue.class) != null);
|
||||||
if (enumValueField != null) {
|
if (enumValueField != null) {
|
||||||
@ -50,24 +51,23 @@ public class EnumWrapper<E extends Enum<E>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (hasEnumValueAnnotation) {
|
if (hasEnumValueAnnotation) {
|
||||||
String fieldGetterName = "get" + StringUtil.firstCharToUpperCase(enumValueField.getName());
|
String getterMethodName = "get" + StringUtil.firstCharToUpperCase(enumValueField.getName());
|
||||||
|
|
||||||
Method getterMethod = ClassUtil.getFirstMethod(enumClass, method -> {
|
Method getter = ClassUtil.getFirstMethod(enumClass, method -> {
|
||||||
String methodName = method.getName();
|
String methodName = method.getName();
|
||||||
return methodName.equals(fieldGetterName) && Modifier.isPublic(method.getModifiers());
|
return methodName.equals(getterMethodName) && Modifier.isPublic(method.getModifiers());
|
||||||
});
|
});
|
||||||
|
|
||||||
enumPropertyType = ClassUtil.getWrapType(enumValueField.getType());
|
propertyType = ClassUtil.getWrapType(enumValueField.getType());
|
||||||
enums = enumClass.getEnumConstants();
|
|
||||||
|
|
||||||
if (getterMethod == null) {
|
if (getter == null) {
|
||||||
if (Modifier.isPublic(enumValueField.getModifiers())) {
|
if (Modifier.isPublic(enumValueField.getModifiers())) {
|
||||||
property = enumValueField;
|
property = enumValueField;
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalStateException("Can not find \"" + fieldGetterName + "()\" method in enum: " + enumClass.getName());
|
throw new IllegalStateException("Can not find method \"" + getterMethodName + "()\" in enum: " + enumClass.getName());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
getter = getterMethod;
|
this.getterMethod = getter;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -75,9 +75,7 @@ public class EnumWrapper<E extends Enum<E>> {
|
|||||||
|
|
||||||
public Object getEnumValue(E object) {
|
public Object getEnumValue(E object) {
|
||||||
try {
|
try {
|
||||||
return getter != null
|
return getterMethod != null ? getterMethod.invoke(object) : property.get(object);
|
||||||
? getter.invoke(object)
|
|
||||||
: property.get(object);
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw FlexExceptions.wrap(e);
|
throw FlexExceptions.wrap(e);
|
||||||
}
|
}
|
||||||
@ -93,12 +91,12 @@ public class EnumWrapper<E extends Enum<E>> {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Class<?> getEnumClass() {
|
public boolean hasEnumValueAnnotation() {
|
||||||
return enumClass;
|
return hasEnumValueAnnotation;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Class<?> getEnumPropertyType() {
|
public Class<?> getEnumClass() {
|
||||||
return enumPropertyType;
|
return enumClass;
|
||||||
}
|
}
|
||||||
|
|
||||||
public E[] getEnums() {
|
public E[] getEnums() {
|
||||||
@ -109,11 +107,11 @@ public class EnumWrapper<E extends Enum<E>> {
|
|||||||
return property;
|
return property;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Method getGetter() {
|
public Class<?> getPropertyType() {
|
||||||
return getter;
|
return propertyType;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasEnumValueAnnotation() {
|
public Method getGetterMethod() {
|
||||||
return hasEnumValueAnnotation;
|
return getterMethod;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user