Search code examples
javajvmbytecodeverifyerrorjvm-bytecode

Java Bytecode Error : Expecting to find object/array on stack


I am compiling a simple language into JVM Bytecode and having some issues with Java object method calls. The verifier gives the error below

java.lang.VerifyError: (class: Test_1, method: main signature: ()V) Expecting to find object/array on stack

and below is the generated Java source code from my bytecodes by IntelliJ

import java.util.ArrayList;

public final class Test_1 {
    public static void main() {
        ArrayList var1 = new ArrayList();
        var1.add(19);
        int var2 = (Integer)var1.get(0);
    }
}

which is exactly what I am trying to do. Creating an ArrayList, assigning a value and reading from it. The above code looks like a valid Java code to me.

Below is my bytecode

{
  public static void main();
    descriptor: ()V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=2, locals=2, args_size=0
  0: new           #9  // class java/util/ArrayList
  3: dup
  4: invokespecial #12  // Method java/util/ArrayList."<init>":()V
  7: astore_1
  8: aload_1
  9: bipush        19
  11: invokestatic  #16  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
  14: invokevirtual #26  // Method java/util/ArrayList.add:(Ljava/lang/Object;)Z
  17: pop
  18: aload_1
  19: astore_0
  20: aload_0
  21: iconst_0
  22: invokevirtual #34  // Method java/util/ArrayList.get:(I)Z
  25: checkcast     #2   // class java/lang/Integer
  28: invokevirtual #11  // Method java/lang/Integer.intValue:()I
  31: istore_1
  32: return
}

I suspect something funny is going on along the lines 18-20, but I am not sure. The rest of the Bytecode instructions seem okay to me.

Why does the verifier complain about not finding an object on the stack?


Solution

  • The signature of ArrayList.get method at 22 is wrong.
    The correct one is (I)Ljava/lang/Object;