Java 字符串数组上的字节码 [putstatic 中的错误类型]
Java ByteCode on String Array [Bad Type in putstatic]
我正在尝试为 class 编写等同于以下内容的 JVM 字节码:
public class foo {
static String[] crr;
public static void printString(String str) {
System.out.println(str);
}
public static void main(String[] args) {
crr = new String[10];
crr[0] = "Hello";
foo.printString(crr[0]);
}
}
我写的字节码:
.class public foo
.super java/lang/Object
.field public static crr [Ljava/lang/String;
.method public <init>()V
aload_0
invokenonvirtual java/lang/Object/<init>()V
return
.end method
.method public static printString(Ljava/lang/String;)V
.limit locals 1
.limit stack 2
getstatic java/lang/System/out Ljava/io/PrintStream;
aload_0
invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V
return
.end method
.method public static main([Ljava/lang/String;)V
.limit stack 20
.limit locals 10
bipush 9
anewarray Ljava/lang/String;
putstatic foo.crr [Ljava/lang/String;
getstatic foo.crr [Ljava/lang/String;
ldc 0
ldc "Hello"
aastore
getstatic foo.crr [Ljava/lang/String;
ldc 0
aaload
invokestatic foo.printString(Ljava/lang/String;)V
return
.end method
我使用 Jasmine 运行 字节码并得到 Bad Type Error for getfield/putfield,但无法弄清楚原因。
Exception in thread "main" java.lang.VerifyError: (class: foo, method: main signature: ([Ljava/lang/String;)V) Bad type in putfield/putstatic
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2625)
at java.lang.Class.getMethod0(Class.java:2866)
at java.lang.Class.getMethod(Class.java:1676)
at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
有人知道问题出在哪里以及如何解决吗?
您的 anewarray
指令有误。而不是
anewarray Ljava/lang/String;
你需要写
anewarray java/lang/String
我正在尝试为 class 编写等同于以下内容的 JVM 字节码:
public class foo {
static String[] crr;
public static void printString(String str) {
System.out.println(str);
}
public static void main(String[] args) {
crr = new String[10];
crr[0] = "Hello";
foo.printString(crr[0]);
}
}
我写的字节码:
.class public foo
.super java/lang/Object
.field public static crr [Ljava/lang/String;
.method public <init>()V
aload_0
invokenonvirtual java/lang/Object/<init>()V
return
.end method
.method public static printString(Ljava/lang/String;)V
.limit locals 1
.limit stack 2
getstatic java/lang/System/out Ljava/io/PrintStream;
aload_0
invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V
return
.end method
.method public static main([Ljava/lang/String;)V
.limit stack 20
.limit locals 10
bipush 9
anewarray Ljava/lang/String;
putstatic foo.crr [Ljava/lang/String;
getstatic foo.crr [Ljava/lang/String;
ldc 0
ldc "Hello"
aastore
getstatic foo.crr [Ljava/lang/String;
ldc 0
aaload
invokestatic foo.printString(Ljava/lang/String;)V
return
.end method
我使用 Jasmine 运行 字节码并得到 Bad Type Error for getfield/putfield,但无法弄清楚原因。
Exception in thread "main" java.lang.VerifyError: (class: foo, method: main signature: ([Ljava/lang/String;)V) Bad type in putfield/putstatic
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2625)
at java.lang.Class.getMethod0(Class.java:2866)
at java.lang.Class.getMethod(Class.java:1676)
at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
有人知道问题出在哪里以及如何解决吗?
您的 anewarray
指令有误。而不是
anewarray Ljava/lang/String;
你需要写
anewarray java/lang/String