1 year ago

#347392

test-img

cityblack

Javassist. java.lang.VerifyError: Bad type on operand stack

public interface MethodInvoke {
    Object invoke(Object... args);
}

public class Target {
    int hello(int value) { return value + 1; }
}
// pool is ClassPool
CtClass enhance = pool.makeClass(MethodInvoke.class.getName+$A);
// this.bean is Target instance
CtMethod invoke = CtNewMethod.make("public Object invoke(Object... args) { return this.bean.hello((int)args[0]); }", enhance);
enhance.addMethod(invoke);

When i use the enhance class to new a object:

Exception in thread "main" java.lang.VerifyError: Bad type on operand stack:
Exception Details:
Location:
xx$A.invoke([Ljava/lang/Object;)Ljava/lang/Object; @7: invokevirtual Reason:

Type 'java/lang/Object' (current frame, stack[1]) is not assignable to integer
Current Frame:
bci: @7
flags: { }
locals: {'xx$A','[Ljava/lang/Object;'}
stack: {'xx$A','java/lang/Object'}
Bytecode:
0000000: 2ab4 0010 2b03 32b6 0018 b0

Params type of Hello method is int, but methodinvoke #invoke is object []. When Java type conversion is compiled into bytecode, integer should be required, that is, this bean. hello((Integer)args[0]). But when I set it like this, that throw an exception:[source error] hello (Java. Lang. integer) not found in XX Target

Am I using it wrong? or is there any other way to do this

java

javassist

0 Answers

Your Answer

Accepted video resources