mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
Merge branch 'main' of https://github.com/mybatis-flex/mybatis-flex
This commit is contained in:
commit
12bd776c9a
@ -39,8 +39,8 @@ public class CompositeEnumTypeHandler<E extends Enum<E>> implements TypeHandler<
|
||||
boolean isNotFound = false;
|
||||
List<Field> enumDbValueFields = ClassUtil.getAllFields(enumClass, f -> f.getAnnotation(EnumValue.class) != null);
|
||||
if (enumDbValueFields.isEmpty()) {
|
||||
List<Method> enumDbValueMethods = ClassUtil.getAllMethods(enumClass, m -> m.getAnnotation(EnumValue.class) != null);
|
||||
if (enumDbValueMethods.isEmpty()) {
|
||||
Method enumDbValueMethod = ClassUtil.getFirstMethodByAnnotation(enumClass, EnumValue.class);
|
||||
if (enumDbValueMethod == null) {
|
||||
isNotFound = true;
|
||||
}
|
||||
}
|
||||
|
||||
@ -26,7 +26,7 @@ import java.sql.SQLException;
|
||||
|
||||
public class FlexEnumTypeHandler<E extends Enum<E>> extends BaseTypeHandler<E> {
|
||||
|
||||
private EnumWrapper<E> enumWrapper;
|
||||
private final EnumWrapper<E> enumWrapper;
|
||||
|
||||
public FlexEnumTypeHandler(Class<E> enumClass) {
|
||||
enumWrapper = EnumWrapper.of(enumClass);
|
||||
|
||||
@ -18,10 +18,9 @@ package com.mybatisflex.core.util;
|
||||
|
||||
import org.apache.ibatis.javassist.util.proxy.ProxyObject;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
@ -286,6 +285,34 @@ public class ClassUtil {
|
||||
return methods.isEmpty() ? null : methods.get(0);
|
||||
}
|
||||
|
||||
public static Method getFirstMethodByAnnotation(Class<?> clazz, Class<? extends Annotation> annotationClass) {
|
||||
Set<Class<?>> visited = new HashSet<>();
|
||||
return findMethod(clazz, annotationClass, visited);
|
||||
}
|
||||
|
||||
private static Method findMethod(Class<?> clazz, Class<? extends Annotation> annotationClass, Set<Class<?>> visited) {
|
||||
if (clazz == null || clazz == Object.class || visited.contains(clazz)) {
|
||||
return null;
|
||||
}
|
||||
visited.add(clazz);
|
||||
|
||||
Method[] methods = clazz.getDeclaredMethods();
|
||||
for (Method method : methods) {
|
||||
if (method.isAnnotationPresent(annotationClass)) {
|
||||
return method;
|
||||
}
|
||||
}
|
||||
|
||||
for (Class<?> inter : clazz.getInterfaces()) {
|
||||
Method method = findMethod(inter, annotationClass, visited);
|
||||
if (method != null) {
|
||||
return method;
|
||||
}
|
||||
}
|
||||
|
||||
return findMethod(clazz.getSuperclass(), annotationClass, visited);
|
||||
}
|
||||
|
||||
|
||||
private static void doGetMethods(Class<?> clazz, List<Method> methods, Predicate<Method> predicate, boolean firstOnly) {
|
||||
applyAllClass(clazz, currentClass -> {
|
||||
@ -318,7 +345,7 @@ public class ClassUtil {
|
||||
for (Class<?> anInterface : interfaces) {
|
||||
doGetMethods(anInterface, methods, predicate, firstOnly);
|
||||
// 只获取第一个并且集合不为空就结束遍历
|
||||
if (firstOnly && !methods.isEmpty()){
|
||||
if (firstOnly && !methods.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@ -30,8 +30,8 @@ public class EnumWrapper<E extends Enum<E>> {
|
||||
|
||||
private boolean hasEnumValueAnnotation = false;
|
||||
|
||||
private Class<?> enumClass;
|
||||
private E[] enums;
|
||||
private final Class<?> enumClass;
|
||||
private final E[] enums;
|
||||
private Field property;
|
||||
private Class<?> propertyType;
|
||||
private Method getterMethod;
|
||||
@ -71,7 +71,7 @@ public class EnumWrapper<E extends Enum<E>> {
|
||||
}
|
||||
|
||||
if (!hasEnumValueAnnotation) {
|
||||
Method enumValueMethod = ClassUtil.getFirstMethod(enumClass, method -> method.getAnnotation(EnumValue.class) != null);
|
||||
Method enumValueMethod = ClassUtil.getFirstMethodByAnnotation(enumClass, EnumValue.class);
|
||||
if (enumValueMethod != null) {
|
||||
String methodName = enumValueMethod.getName();
|
||||
if (!(methodName.startsWith("get") && methodName.length() > 3)) {
|
||||
@ -102,11 +102,11 @@ public class EnumWrapper<E extends Enum<E>> {
|
||||
try {
|
||||
if (getterMethod != null) {
|
||||
return getterMethod.invoke(object);
|
||||
} else if(property != null){
|
||||
} else if (property != null) {
|
||||
return property.get(object);
|
||||
} else {
|
||||
//noinspection unchecked
|
||||
return ((E)object).name();
|
||||
return ((E) object).name();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw FlexExceptions.wrap(e);
|
||||
|
||||
@ -0,0 +1,8 @@
|
||||
package com.mybatisflex.test;
|
||||
|
||||
import com.mybatisflex.annotation.EnumValue;
|
||||
|
||||
public interface IBaseEnum {
|
||||
@EnumValue
|
||||
int getCode();
|
||||
}
|
||||
@ -18,7 +18,7 @@ package com.mybatisflex.test;
|
||||
|
||||
import com.mybatisflex.annotation.EnumValue;
|
||||
|
||||
public enum SexEnum {
|
||||
public enum SexEnum implements IBaseEnum{
|
||||
TYPE1(0, "女"),
|
||||
TYPE2(1, "男"),
|
||||
TYPE3(2, "未知"),
|
||||
@ -35,7 +35,7 @@ public enum SexEnum {
|
||||
this.desc = desc;
|
||||
}
|
||||
|
||||
@EnumValue
|
||||
// @EnumValue
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user