How to avoid "Illegal type in constant pool" using "ldc_w <classname>" in Jasmin?

Posted by jazzdev on Stack Overflow See other posts from Stack Overflow or by jazzdev
Published on 2010-05-06T21:52:23Z Indexed on 2010/05/06 21:58 UTC
Read the original article Hit count: 223

Filed under:
|

I'm writing a compiler that generates Jasmin code and want to invoke a method that takes a Class as a parameter.

public class CTest
{
    public static void main(String[] args)
        throws Exception
    {
        java.lang.reflect.Array.newInstance(CTest.class, 0);
    }
}

So in Jasmin, I think that should be:

.class public CTest2
.super java/lang/Object

.method public static main([Ljava/lang/String;)V
    .limit stack 2
    .limit locals 1
    ldc_w CTest2
    iconst_0
    invokestatic java/lang/reflect/Array/newInstance(Ljava/lang/Class;I)Ljava/lang/Object;
    pop
    return
.end method

When I assemble it and run it I get:

Exception in thread "main" java.lang.VerifyError: (class: CTest2, method: main signature: ([Ljava/lang/String;)V) Illegal type in constant pool

Looking at the disassembled code for both CTest.class (the Java version) and CTest2.class (the Jasmin version) with "javap -c -verbose" they both appear to set up the constant pool the same way:

const #2 = class        #16;    //  CTest
const #16 = Asciz       CTest;

  0:   ldc_w   #2; //class CTest

const #14 = Asciz       CTest2;
const #17 = class       #14;    //  CTest2

  0:   ldc_w   #17; //class CTest2

I've fixed two bugs in Jasmin already, but I can't see what it's doing wrong when putting the class in the constant pool for "ldc_w" it puts classes in the constant pool for other instructions, like "new" and "anewarray" correctly.

I've tried looking at the .class files with TraceClassVisitor in ASM, but it doesn't dump the constant pool.

Any ideas what I can try next?

© Stack Overflow or respective owner

Related posts about java

Related posts about jasmin