mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-08 01:28:24 +08:00
Merge pull request #411 from Aliothmoon/fix/issues-377
fix: 修复@EnumValue应用在接口方法上失效
This commit is contained in:
commit
8670ff20bf
@ -39,8 +39,8 @@ public class CompositeEnumTypeHandler<E extends Enum<E>> implements TypeHandler<
|
|||||||
boolean isNotFound = false;
|
boolean isNotFound = false;
|
||||||
List<Field> enumDbValueFields = ClassUtil.getAllFields(enumClass, f -> f.getAnnotation(EnumValue.class) != null);
|
List<Field> enumDbValueFields = ClassUtil.getAllFields(enumClass, f -> f.getAnnotation(EnumValue.class) != null);
|
||||||
if (enumDbValueFields.isEmpty()) {
|
if (enumDbValueFields.isEmpty()) {
|
||||||
List<Method> enumDbValueMethods = ClassUtil.getAllMethods(enumClass, m -> m.getAnnotation(EnumValue.class) != null);
|
Method enumDbValueMethod = ClassUtil.getFirstMethodByAnnotation(enumClass, EnumValue.class);
|
||||||
if (enumDbValueMethods.isEmpty()) {
|
if (enumDbValueMethod == null) {
|
||||||
isNotFound = true;
|
isNotFound = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -26,7 +26,7 @@ import java.sql.SQLException;
|
|||||||
|
|
||||||
public class FlexEnumTypeHandler<E extends Enum<E>> extends BaseTypeHandler<E> {
|
public class FlexEnumTypeHandler<E extends Enum<E>> extends BaseTypeHandler<E> {
|
||||||
|
|
||||||
private EnumWrapper<E> enumWrapper;
|
private final EnumWrapper<E> enumWrapper;
|
||||||
|
|
||||||
public FlexEnumTypeHandler(Class<E> enumClass) {
|
public FlexEnumTypeHandler(Class<E> enumClass) {
|
||||||
enumWrapper = EnumWrapper.of(enumClass);
|
enumWrapper = EnumWrapper.of(enumClass);
|
||||||
|
|||||||
@ -18,10 +18,9 @@ package com.mybatisflex.core.util;
|
|||||||
|
|
||||||
import org.apache.ibatis.javassist.util.proxy.ProxyObject;
|
import org.apache.ibatis.javassist.util.proxy.ProxyObject;
|
||||||
|
|
||||||
|
import java.lang.annotation.Annotation;
|
||||||
import java.lang.reflect.*;
|
import java.lang.reflect.*;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -241,7 +240,7 @@ public class ClassUtil {
|
|||||||
* @param checkToContinue 应用当前类并检测是否继续应用, 返回false则停止应用, 返回true继续向上取父类
|
* @param checkToContinue 应用当前类并检测是否继续应用, 返回false则停止应用, 返回true继续向上取父类
|
||||||
* @author KAMOsama
|
* @author KAMOsama
|
||||||
*/
|
*/
|
||||||
public static void applyAllClass(Class<?> clazz, Predicate<Class<?>> checkToContinue) {
|
public static void applyAllClass(Class<?> clazz, Predicate<Class<?>> checkToContinue) {
|
||||||
Class<?> currentClass = clazz;
|
Class<?> currentClass = clazz;
|
||||||
while (currentClass != null && currentClass != Object.class && checkToContinue.test(currentClass)) {
|
while (currentClass != null && currentClass != Object.class && checkToContinue.test(currentClass)) {
|
||||||
currentClass = currentClass.getSuperclass();
|
currentClass = currentClass.getSuperclass();
|
||||||
@ -286,6 +285,34 @@ public class ClassUtil {
|
|||||||
return methods.isEmpty() ? null : methods.get(0);
|
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) {
|
private static void doGetMethods(Class<?> clazz, List<Method> methods, Predicate<Method> predicate, boolean firstOnly) {
|
||||||
applyAllClass(clazz, currentClass -> {
|
applyAllClass(clazz, currentClass -> {
|
||||||
@ -318,7 +345,7 @@ public class ClassUtil {
|
|||||||
for (Class<?> anInterface : interfaces) {
|
for (Class<?> anInterface : interfaces) {
|
||||||
doGetMethods(anInterface, methods, predicate, firstOnly);
|
doGetMethods(anInterface, methods, predicate, firstOnly);
|
||||||
// 只获取第一个并且集合不为空就结束遍历
|
// 只获取第一个并且集合不为空就结束遍历
|
||||||
if (firstOnly && !methods.isEmpty()){
|
if (firstOnly && !methods.isEmpty()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -30,8 +30,8 @@ public class EnumWrapper<E extends Enum<E>> {
|
|||||||
|
|
||||||
private boolean hasEnumValueAnnotation = false;
|
private boolean hasEnumValueAnnotation = false;
|
||||||
|
|
||||||
private Class<?> enumClass;
|
private final Class<?> enumClass;
|
||||||
private E[] enums;
|
private final E[] enums;
|
||||||
private Field property;
|
private Field property;
|
||||||
private Class<?> propertyType;
|
private Class<?> propertyType;
|
||||||
private Method getterMethod;
|
private Method getterMethod;
|
||||||
@ -71,7 +71,7 @@ public class EnumWrapper<E extends Enum<E>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!hasEnumValueAnnotation) {
|
if (!hasEnumValueAnnotation) {
|
||||||
Method enumValueMethod = ClassUtil.getFirstMethod(enumClass, method -> method.getAnnotation(EnumValue.class) != null);
|
Method enumValueMethod = ClassUtil.getFirstMethodByAnnotation(enumClass, EnumValue.class);
|
||||||
if (enumValueMethod != null) {
|
if (enumValueMethod != null) {
|
||||||
String methodName = enumValueMethod.getName();
|
String methodName = enumValueMethod.getName();
|
||||||
if (!(methodName.startsWith("get") && methodName.length() > 3)) {
|
if (!(methodName.startsWith("get") && methodName.length() > 3)) {
|
||||||
@ -102,11 +102,11 @@ public class EnumWrapper<E extends Enum<E>> {
|
|||||||
try {
|
try {
|
||||||
if (getterMethod != null) {
|
if (getterMethod != null) {
|
||||||
return getterMethod.invoke(object);
|
return getterMethod.invoke(object);
|
||||||
} else if(property != null){
|
} else if (property != null) {
|
||||||
return property.get(object);
|
return property.get(object);
|
||||||
} else {
|
} else {
|
||||||
//noinspection unchecked
|
//noinspection unchecked
|
||||||
return ((E)object).name();
|
return ((E) object).name();
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw FlexExceptions.wrap(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;
|
import com.mybatisflex.annotation.EnumValue;
|
||||||
|
|
||||||
public enum SexEnum {
|
public enum SexEnum implements IBaseEnum{
|
||||||
TYPE1(0, "女"),
|
TYPE1(0, "女"),
|
||||||
TYPE2(1, "男"),
|
TYPE2(1, "男"),
|
||||||
TYPE3(2, "未知"),
|
TYPE3(2, "未知"),
|
||||||
@ -35,7 +35,7 @@ public enum SexEnum {
|
|||||||
this.desc = desc;
|
this.desc = desc;
|
||||||
}
|
}
|
||||||
|
|
||||||
@EnumValue
|
// @EnumValue
|
||||||
public int getCode() {
|
public int getCode() {
|
||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user