3

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.

Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192
Winter Young
  • 801
  • 1
  • 7
  • 14

2 Answers2

2

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.

Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192
  • Thank you. I use byte-buddy in a maven plugin. So I won't suffer from the dependency issue. – Winter Young Sep 14 '14 at 10:22
  • Partly for using in a Maven plugin, I offer some cool new features in version 0.3 which is released some time this month. It will allow you to redefine methods of existing classes using the existing API. In general, you want to avoid the byte code API and use the high-level API. I am curious what your use case is that you need the byte code level? – Rafael Winterhalter Sep 15 '14 at 10:43
  • as illustrated in the code in the question, to say briefly, my process is the reverse process of @AllArgument. – Winter Young Sep 16 '14 at 05:25
1

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

geoand
  • 60,071
  • 24
  • 172
  • 190