ClassFormatError error while invoking method added using Java ASM
-
29-10-2019 - |
Question
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.
Solution
You need to use visitMethodInsn
instead of visitFieldInsn
, since you are calling a method, not accessing a field.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow