Search code examples
byte-buddy

Is type assignability too strict in JavaConstant.Dynamic.ofInvocation()?


I've read Rafael's article and am now doing awful, terrible things with JavaConstant.Dynamic. Mostly I'm getting a feel for how it works.

As part of these horrid experiments, I am turning an array of non-constant things into an array of JavaConstants. Then I'm invoking JavaConstant.Dynamic.ofInvocation(SOME_METHOD_THAT_ACCEPTS_A_VARARGS_OF_THINGS, javaConstantsArray).

So, for example, something like this:

static final JavaConstant toJavaConstant(final Glorp[] glorps) {
  final JavaConstant[] javaConstants = new JavaConstant[glorps.length];
  for (int i = 0; i < javaConstants.length; i++) {
    javaConstants[i] = toJavaConstant(glorps[i]); // another version of this method that works on scalars
  }
  return JavaConstant.Dynamic.ofInvocation(SOME_METHOD_THAT_ACCEPTS_A_VARARGS_OF_THINGS, javaConstants);
}

ByteBuddy is telling me in the ofInvocation call that one of the JavaConstants in the varargs array I've passed it is not assignable to the parameter type of the SOME_METHOD_THAT_ACCEPTS_A_VARARGS_OF_THINGS. I can understand this, because strictly speaking a variable arity method accepts an array as its last parameter, and a JavaConstant is not an array. But given that the SOME_METHOD_THAT_ACCEPTS_A_VARARGS_OF_THINGS is ultimately resolved via the MethodHandle machinery with all of its argument adaptation and spreading tricks, I wonder: is this proactive assignability check "too much"? Should ByteBuddy take into account the varargs nature of the bootstrap method? Is there some other way to create an array or a list of an arbitrary number of scalar constants as a constant itself?


Solution

  • Yes, this was a bug and it will be fixed in Byte Buddy 1.10.18. Thanks for the patch!