文章目录
Kotlin 的 Lambda 表达式的编译后
源码
我们演示一个简单的 Demo,Lambda 表达式实现 Predicate<Integer>
,判断整数是否是偶数的:
class LambdaTest {
companion object {
// 相当于 static 成员
private fun Int.toBoolean(predicate: (Int) -> Boolean) = predicate(this)
private fun testStaticLambda() {
val x = 1;
val y = 2;
val b = 1.toBoolean {
println("$x and $y") // 引用局部变量 x 和 y
it % 2 == 0
}
println(b)
}
}
private var x = 1;
private fun testNonStaticLambda() {
val y = 2;
val b = 1.toBoolean {
println("$x and $y") // 引用实例变量 x 和局部变量 y
it % 2 == 0
}
println(b)
}
}
编译后
=> 解读 class 文件得到:
// LambdaTest.class
public final class LambdaTest {
// companion object 被编译成静态 final 变量,并生成匿名类
public static final LambdaTest$Companion Companion = new LambdaTest$Companion(null);
private int x;
private final void testNonStaticLambda() {
int y = 2;
boolean b = LambdaTest$Companion.access$toBoolean(
new LambdaTest$Companion(),
1, // lambda 表达式编译成 Fuction1 接口的匿名类,注意这里传入了 this!!!
(Function1) new LambdaTest$testNonStaticLambda$b$1(this, y));
System.out.println(b);
}
/* x 的 setter 和 getter */
public LambdaTest() {
this.x = 1; }
public static final int access$getX$p(LambdaTest $this) {
/**/}
public static final void access$setX$p(LambdaTest $this, int n) {
/**/}
}
伴随对象生成的匿名类 LambdaTest$Companion.class,生成 static 方法。
// LambdaTest$Companion.class
public final class LambdaTest$Companion {
private final boolean toBoolean(int $this$intToBoolean, Function1<? super Integer, Boolean> predicate) {
return predicate.invoke($this$intToBoolean); // AutoBox
}
private final void testStaticLambda() {
int x = 1;
int y = 2;
// lambda 表达式编译为 Function1 接口的匿名类
boolean b = toBoolean(1, (Function1) new LambdaTest$Companion$testStaticLambda$b$1(x, y));
System.out.println(b);
}
public static final boolean access$toBoolean(LambdaTest$Companion $this, int $this$access_u24toBoolean, Function1 predicate) {
return $this.toBoolean($this$access_u24toBoolean, (Function1<? super Integer, Boolean>)predicate);
}
private LambdaTest$Companion() {
}
public LambdaTest$