Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClassFormatError error while invoking method added using Java ASM

I am using Java ASM to add a method to compiled class. During run time I am getting
below error, when the newly added method is invoked.

    ClassFormatError: Field "valueEquals" in class test/asm/Item has illegal signature "(Ljava/lang/Object;)Z"  

Below is the method which I expecting to add

    public boolean valueEquals(Object obj){  
        return ItemHelper.valueEquals(obj);  
    }  

Below is the asm code for this.

    String methodName = "valueEquals";  
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, methodName, "(Ljava/lang/Object;)Z", null, null);  
    mv.visitCode();  
    mv.visitVarInsn(ALOAD, 1);  
    mv.visitFieldInsn(INVOKESTATIC, "test/asm/ItemHelper", methodName, "(Ljava/lang/Object;)Z");  
    mv.visitInsn(IRETURN);  
    mv.visitMaxs(2, 1);  
    mv.visitEnd();  

Please can some one help me in identifying what I am doing wrong here. Your help is very much appreciated.

like image 625
Rob Wilkinson Avatar asked Jul 20 '26 17:07

Rob Wilkinson


1 Answers

You need to use visitMethodInsn instead of visitFieldInsn, since you are calling a method, not accessing a field.

like image 56
Viruzzo Avatar answered Jul 23 '26 06:07

Viruzzo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!