Winter Young
Winter Young

Reputation: 821

Why does Byte Buddy lack a StackManipulation implementation corresponding to the opcode ASTORE?

If its absence is because byte-buddy aims at the method delegation domain, then I can provide a scenario where this feature is necessary:

private Object invokeSpi(Object spi, Object... params) {
    Reducer reducer = (Reducer) spi;
    return reducer.reduce((Integer) params[0], (Integer) params[8]);
}

The above code would generate an instruction of ASTORE for the down cast statement.

Upvotes: 3

Views: 340

Answers (2)

geoand
geoand

Reputation: 64011

I am using ByteBuddy version 1.7.3 and in that version the ALOAD, ASTORE operations along with other relevant operations can be found at in net.bytebuddy.implementation.bytecode.member.MethodVariableAccess. Check out the Javadoc here

Upvotes: 1

Rafael Winterhalter
Rafael Winterhalter

Reputation: 44032

Byte Buddy offers different Instrumentation implementations which are all composed by the mentioned StackManipulations. However, no prebuilt instrumentation requires an ASTORE instruction which is why it is not predefned. You can however easily implement your own implementation for this purpose:

class AStrore implements StackManipulation {

  private final int index; // Constructor omitted

  public boolean isValid() {
    return index >= 0;
  }

  public Size apply(MethodVisitor methodVisitor, Instrumentation.Context context) {
    methodVisitor.visitIntInsn(Opcodes.ASTORE, index);
    return new Size(-1, 0);
  }
}

Note however that you then make direct use of ASM which suffers compatbility issues. For this reason, please read the information on Byte Buddy's website of how to repackage ASM and Byte Buddy into your own namespace.

Also note that you could avoid the ASTORE instruction by directly casting the instance before the call.

Upvotes: 2

Related Questions