1

I'm trying to understand how Java's ObjectWeb ASM library (framework?) works by a combination of reading the documentation and looking at example code. I'm slowly getting the idea behind it, but I've found a bit of example code that I'm just not understanding, so I hope someone here can explain it to me.

The code takes mv, a MethodVisitor obtained from a ClassWriter's visitMethod method, and starts manipulating the method like this:

    mv.visitCode();
    String methodSignature = "(L" + worldClass + ";IIIL" + blockClass + ";)V";

    Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitLineNumber(81, l0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitVarInsn(ILOAD, 2);
    mv.visitVarInsn(ILOAD, 3);
    mv.visitVarInsn(ILOAD, 4);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESTATIC, "com/olafski/fastleafdecay/FldHandler", "handleLeafDecay", methodSignature);

There's more to it, of course, but this is the part I don't understand. As you can see from the method signature, it takes 5 arguments: a World class, three integers, and a Block class (and yes, those variables in the signature string do hold the proper FQNs for those classes). Yet before the new method invocation is visited, only four parameters are pushed onto the stack (plus the reference to this). So where is it getting the fifth parameter from?

Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
IceMetalPunk
  • 5,476
  • 3
  • 19
  • 26

1 Answers1

1

There are five parameters:

Because handleLeafDecay is a static method (INVOKESTATIC operand is used to call it), the reference to this is not pushed on stack before method call. However, the reference to this is used as the fifth parameter.

So the generated bytecode would be equivalent to this code:

public void generatedMethod(World wolrd, int i1, int i2, int i3, Block block) {
    FldHandler.handleLeafDecay(wolrd, i1, i2, i3, this);
    // ...
}

That means this method is inside Block class or it's derivatives. Also the block parameter is unused.

Adamantium
  • 118
  • 7