mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-08 01:28:24 +08:00
optimize FlexEnumTypeHandler.java
This commit is contained in:
parent
930f796812
commit
7ac8a4f056
@ -24,6 +24,7 @@ import org.apache.ibatis.type.JdbcType;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.sql.CallableStatement;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
@ -34,6 +35,7 @@ public class FlexEnumTypeHandler<E extends Enum<E>> extends BaseTypeHandler<E> {
|
||||
|
||||
private Class<?> enumPropertyType;
|
||||
private E[] enums;
|
||||
private Field property;
|
||||
private Method getter;
|
||||
|
||||
public FlexEnumTypeHandler(Class<E> enumClass) {
|
||||
@ -50,12 +52,17 @@ public class FlexEnumTypeHandler<E extends Enum<E>> extends BaseTypeHandler<E> {
|
||||
enums = enumClass.getEnumConstants();
|
||||
|
||||
if (allMethods.isEmpty()) {
|
||||
if (Modifier.isPublic(field.getModifiers())) {
|
||||
property = field;
|
||||
} else {
|
||||
throw new IllegalStateException("Can not find \"" + fieldGetterName + "()\" method in enum: " + enumClass.getName());
|
||||
}
|
||||
|
||||
} else {
|
||||
getter = allMethods.get(0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void setNonNullParameter(PreparedStatement ps, int i, E parameter, JdbcType jdbcType) throws SQLException {
|
||||
@ -68,9 +75,11 @@ public class FlexEnumTypeHandler<E extends Enum<E>> extends BaseTypeHandler<E> {
|
||||
}
|
||||
|
||||
|
||||
private Object getValue(Object object) {
|
||||
private Object getValue(E object) {
|
||||
try {
|
||||
return this.getter.invoke(object);
|
||||
return getter != null
|
||||
? getter.invoke(object)
|
||||
: property.get(object);
|
||||
} catch (Exception e) {
|
||||
throw FlexExceptions.wrap(e);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user