字符串数组上的Java ByteCode [putstatic中的错误类型]

时间:2016-01-14 19:42:43

标签: java bytecode java-bytecode-asm jasmin

我试图为类相当于以下的类编写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来运行字节码,并为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)

有人知道问题在哪里以及如何解决?

1 个答案:

答案 0 :(得分:1)

anewarray指令中有错误。而不是

anewarray Ljava/lang/String;

你需要写

anewarray java/lang/String