optimize: 优化 Kotlin Lambda 兼容。

This commit is contained in:
Suomm 2024-05-12 20:52:53 +08:00
parent 168b7da948
commit 0321324521

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). * Copyright (c) 2022-2024, Mybatis-Flex (fuhai999@gmail.com).
* <p> * <p>
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -22,7 +22,6 @@ import com.mybatisflex.core.table.TableInfoFactory;
import java.io.Serializable; import java.io.Serializable;
import java.lang.invoke.SerializedLambda; import java.lang.invoke.SerializedLambda;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
@ -37,17 +36,18 @@ public class LambdaUtil {
public static <T> String getFieldName(LambdaGetter<T> getter) { public static <T> String getFieldName(LambdaGetter<T> getter) {
SerializedLambda lambda = getSerializedLambda(getter); SerializedLambda lambda = getSerializedLambda(getter);
String methodName = lambda.getImplMethodName(); // 兼容 Kotlin KProperty Lambda 解析
if (lambda.getCapturedArgCount() == 1) {
if (methodName.contains("$lambda$") && lambda.getCapturedArgCount() == 1) { Object capturedArg = lambda.getCapturedArg(0);
Object arg = lambda.getCapturedArg(0); try {
if (arg.getClass().getSuperclass().getName().equals("kotlin.jvm.internal.MutablePropertyReference1Impl")) { return (String) capturedArg.getClass()
try { .getMethod("getName")
return (String) arg.getClass().getMethod("getName").invoke(arg); .invoke(capturedArg);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ignored) { } catch (Exception e) {
} // 忽略这个异常使用其他方式获取方法名
} }
} }
String methodName = lambda.getImplMethodName();
return StringUtil.methodToProperty(methodName); return StringUtil.methodToProperty(methodName);
} }