Search code examples
javajava-bytecode-asm

Java asm method error


I'm trying to add a method with an Inetadress in the parameter which will invoke a static method in another class with the same parameters, however my code will throw a verifyerror.

    MethodNode mv = new MethodNode(ACC_PUBLIC, "startPrivate", "(Ljava/net/InetAddress;)V", null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESTATIC, "I/Z", "I", "(Ljava/net/InetAddress;)V");
    mv.visitInsn(RETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
    into.methods.add(mv);

error:

Exception in thread "main" java.lang.VerifyError: (class: client, method: startPrivate signature: (Ljava/net/InetAddress;)V) Incompatible argument to function
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2483)
at java.lang.Class.getConstructor0(Class.java:2793)
at java.lang.Class.newInstance(Class.java:345)
at vanquish.Loader.initializeClient(Loader.java:33)
at vanquish.Loader.main(Loader.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

Thanks for helping working now:

    MethodNode mv = new MethodNode(ACC_PUBLIC, "startPrivate", "(Ljava/net/InetAddress;)V", null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKESTATIC, "I/Z", "I", "(Ljava/net/InetAddress;)V");
    mv.visitInsn(RETURN);
    mv.visitMaxs(1, 2);
    mv.visitEnd();
    into.methods.add(mv);

Solution

  • Since you didn’t specify ACC_STATIC you create an instance method. Therefore you have this inside variable 0, so your max. number of local variable is wrong and passing the contents of variable 0 to the static target method is wrong too. Didn’t the VerifierError give you a specific message?

    And is the target method really called I and declared in a class I.Z?