tags:

views:

162

answers:

1

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?

A: 

You have to ensure that the version number of the class is at least 49 (see the visitLdcInsn on this ASM Javadoc page).

gpothier
Excellent. Adding ".bytecode 49.0" fixed the problem. Thanks!
jazzdev