mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 09:08:24 +08:00
!453 修复在Kotlin中 LambdaUtil 无法通过对象的Lambda正确获取到属性名称的问题
Merge pull request !453 from Haru/fix-kotlin-lambda-get-property-error
This commit is contained in:
commit
168b7da948
@ -22,6 +22,7 @@ import com.mybatisflex.core.table.TableInfoFactory;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.invoke.SerializedLambda;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
@ -37,6 +38,16 @@ public class LambdaUtil {
|
||||
public static <T> String getFieldName(LambdaGetter<T> getter) {
|
||||
SerializedLambda lambda = getSerializedLambda(getter);
|
||||
String methodName = lambda.getImplMethodName();
|
||||
|
||||
if (methodName.contains("$lambda$") && lambda.getCapturedArgCount() == 1) {
|
||||
Object arg = lambda.getCapturedArg(0);
|
||||
if (arg.getClass().getSuperclass().getName().equals("kotlin.jvm.internal.MutablePropertyReference1Impl")) {
|
||||
try {
|
||||
return (String) arg.getClass().getMethod("getName").invoke(arg);
|
||||
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
return StringUtil.methodToProperty(methodName);
|
||||
}
|
||||
|
||||
@ -60,10 +71,10 @@ public class LambdaUtil {
|
||||
public static <T> QueryColumn getQueryColumn(LambdaGetter<T> getter) {
|
||||
ClassLoader classLoader = getter.getClass().getClassLoader();
|
||||
SerializedLambda lambda = getSerializedLambda(getter);
|
||||
String methodName = lambda.getImplMethodName();
|
||||
Class<?> entityClass = getImplClass(lambda, classLoader);
|
||||
TableInfo tableInfo = TableInfoFactory.ofEntityClass(entityClass);
|
||||
return tableInfo.getQueryColumnByProperty(StringUtil.methodToProperty(methodName));
|
||||
String propertyName = getFieldName(getter);
|
||||
return tableInfo.getQueryColumnByProperty(propertyName);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user