gpt4 book ai didi

kotlin - 为什么 Kotlin 反编译器生成 null.INSTANCE

转载 作者:行者123 更新时间:2023-12-05 09:14:52 25 4
gpt4 key购买 nike

我试图通过检查它在 Java 端的外观来了解 Kotlin 的一些功能。

所以作为一个实验,我尝试了这个:

val printKotlin = fun () {
print("Hello Kotlin")
}

所以上面代码片段的输出是:

public final class FunAsVariableKt {
private static final Function0 printKotlin;

public static final Function0 getPrintKotlin() {
return printKotlin;
}

static {
printKotlin = (Function0)null.INSTANCE;
}
}

如何理解上面反编译代码的静态 block ?为什么会生成此无效代码?

最佳答案

使用 Kotlin 字节码检查器查看作为 JVM 字节码生成的内容,而不是尝试将字节码反编译为 Java,这可能不适用于由不同语言编译器生成的代码,因为它可能不遵循预期的模式。字节码反编译器并不总是生成工作代码。

JVM bytecode != Java language specific

Kotlin generated JVM bytecode != Java generated JVM bytecode

您看到的 NULL 实例是反编译器的错误解释,您可以看到字节码中所有对 INSTANCE 的引用都正确设置了值。

这是实际输出:

// ================Stackoverflow_53384931Kt.class =================
// class version 52.0 (52)
// access flags 0x31
public final class Stackoverflow_53384931Kt {


// access flags 0x1A
// signature Lkotlin/jvm/functions/Function0<Lkotlin/Unit;>;
// declaration: kotlin.jvm.functions.Function0<kotlin.Unit>
private final static Lkotlin/jvm/functions/Function0; printKotlin
@Lorg/jetbrains/annotations/NotNull;() // invisible

// access flags 0x19
// signature ()Lkotlin/jvm/functions/Function0<Lkotlin/Unit;>;
// declaration: kotlin.jvm.functions.Function0<kotlin.Unit> getPrintKotlin()
public final static getPrintKotlin()Lkotlin/jvm/functions/Function0;
@Lorg/jetbrains/annotations/NotNull;() // invisible
L0
LINENUMBER 3 L0
GETSTATIC Stackoverflow_53384931Kt.printKotlin : Lkotlin/jvm/functions/Function0;
ARETURN
L1
MAXSTACK = 1
MAXLOCALS = 0

// access flags 0x8
static <clinit>()V
L0
LINENUMBER 3 L0
GETSTATIC Stackoverflow_53384931Kt$printKotlin$1.INSTANCE : LStackoverflow_53384931Kt$printKotlin$1;
CHECKCAST kotlin/jvm/functions/Function0
PUTSTATIC Stackoverflow_53384931Kt.printKotlin : Lkotlin/jvm/functions/Function0;
RETURN
MAXSTACK = 1
MAXLOCALS = 0

@Lkotlin/Metadata; ...
// access flags 0x18
final static INNERCLASS Stackoverflow_53384931Kt$printKotlin$1 null null
// compiled from: stackoverflow-53384931.kt
}


// ================Stackoverflow_53384931Kt$printKotlin$1.class =================
// class version 52.0 (52)
// access flags 0x30
// signature Lkotlin/jvm/internal/Lambda;Lkotlin/jvm/functions/Function0<Lkotlin/Unit;>;
// declaration: Stackoverflow_53384931Kt$printKotlin$1 extends kotlin.jvm.internal.Lambda implements kotlin.jvm.functions.Function0<kotlin.Unit>
final class Stackoverflow_53384931Kt$printKotlin$1 extends kotlin/jvm/internal/Lambda implements kotlin/jvm/functions/Function0 {


// access flags 0x1041
public synthetic bridge invoke()Ljava/lang/Object;
ALOAD 0
INVOKEVIRTUAL Stackoverflow_53384931Kt$printKotlin$1.invoke ()V
GETSTATIC kotlin/Unit.INSTANCE : Lkotlin/Unit;
ARETURN
MAXSTACK = 1
MAXLOCALS = 1

// access flags 0x11
public final invoke()V
L0
LINENUMBER 4 L0
LDC "Hello Kotlin"
ASTORE 1
L1
GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
ALOAD 1
INVOKEVIRTUAL java/io/PrintStream.print (Ljava/lang/Object;)V
L2
L3
LINENUMBER 5 L3
RETURN
L4
LOCALVARIABLE this LStackoverflow_53384931Kt$printKotlin$1; L0 L4 0
MAXSTACK = 2
MAXLOCALS = 2

// access flags 0x0
<init>()V
ALOAD 0
ICONST_0
INVOKESPECIAL kotlin/jvm/internal/Lambda.<init> (I)V
RETURN
MAXSTACK = 2
MAXLOCALS = 1

// access flags 0x19
public final static LStackoverflow_53384931Kt$printKotlin$1; INSTANCE

// access flags 0x8
static <clinit>()V
NEW Stackoverflow_53384931Kt$printKotlin$1
DUP
INVOKESPECIAL Stackoverflow_53384931Kt$printKotlin$1.<init> ()V
PUTSTATIC Stackoverflow_53384931Kt$printKotlin$1.INSTANCE : LStackoverflow_53384931Kt$printKotlin$1;
RETURN
MAXSTACK = 2
MAXLOCALS = 0

@Lkotlin/Metadata; ...
OUTERCLASS Stackoverflow_53384931Kt null
// access flags 0x18
final static INNERCLASS Stackoverflow_53384931Kt$printKotlin$1 null null
// compiled from: stackoverflow-53384931.kt
// debug info: SMAP
...

}

关于kotlin - 为什么 Kotlin 反编译器生成 null.INSTANCE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53384931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com