Class KotlinLambdaMeta

java.lang.Object
com.baomidou.mybatisplus.core.toolkit.support.KotlinLambdaMeta
All Implemented Interfaces:
LambdaMeta

public class KotlinLambdaMeta extends Object implements LambdaMeta
针对 Kotlin 可调用引用(KProperty / KFunction)适配为 Java SFunction 时的元信息。

Kotlin 把形如 User::name(属性引用)或 UserDO::getName(getter 方法引用)作为 Java 函数式接口 参数传递时,SerializedLambda.getImplMethodName() 往往是合成方法名(如 xxx$lambda$0、 xxx$getName),无法直接解析出字段名。该类改为从被引用对象本身解析:

  • 属性引用(KProperty):getName() 返回属性名,补成 getter 形式交给 PropertyNamer 还原;
  • getter 方法引用(KFunction):getName() 已是方法名(如 getName),直接交给 PropertyNamer;
  • 归属类取自 getOwner().getJClass(),或在 invokedynamic 场景下取自 instantiatedMethodType。

兼容性:属性引用(Entity::prop)在 Kotlin 1.2 至 2.4 全部可用;getter 方法引用(JavaDO::getName) 在 Kotlin 1.2–1.4 与 1.6+ 可用。已知限制:Kotlin 1.5.x 会把 getter 方法引用优化为直接实现函数式接口的单例类, 既无 KFunction 元数据也无 SerializedLambda(字段名仅存在于 apply 方法的字节码中),因此无法解析; 此时请改用属性引用(Entity::prop)或 Kotlin 原生的 KtQueryWrapper。

  • Method Details

    • ofCapturedProperty

      public static KotlinLambdaMeta ofCapturedProperty(Object kProperty, SerializedLambda lambda)
      invokedynamic 场景下被捕获的属性引用(KProperty):字段名来自 KProperty,归属类来自 instantiatedMethodType。
    • ofCallableReference

      public static KotlinLambdaMeta ofCallableReference(Object callable)
      class-based SAM 包装类场景:包装类字段持有的 Kotlin 可调用引用(KProperty 或 KFunction),归属类来自其 owner。
    • ofGetterMethodName

      public static KotlinLambdaMeta ofGetterMethodName(String getterName, SerializedLambda lambda, ClassLoader classLoader)
      invokedynamic 场景下引用 Java getter 时编译出的合成适配方法(形如 enclosing$getName), getter 名取自方法名的末段,归属类来自 instantiatedMethodType。
    • getImplMethodName

      public String getImplMethodName()
      Description copied from interface: LambdaMeta
      获取 lambda 表达式实现方法的名称
      Specified by:
      getImplMethodName in interface LambdaMeta
      Returns:
      lambda 表达式对应的实现方法名称
    • getInstantiatedClass

      public Class<?> getInstantiatedClass()
      Description copied from interface: LambdaMeta
      实例化该方法的类
      Specified by:
      getInstantiatedClass in interface LambdaMeta
      Returns:
      返回对应的类名称