Enum AgentBuilder.LambdaInstrumentationStrategy.LambdaMetafactoryFactory

java.lang.Object
java.lang.Enum<AgentBuilder.LambdaInstrumentationStrategy.LambdaMetafactoryFactory>
net.bytebuddy.agent.builder.AgentBuilder.LambdaInstrumentationStrategy.LambdaMetafactoryFactory
All Implemented Interfaces:
Serializable, Comparable<AgentBuilder.LambdaInstrumentationStrategy.LambdaMetafactoryFactory>, java.lang.constant.Constable, ByteCodeAppender
Enclosing class:
AgentBuilder.LambdaInstrumentationStrategy

protected static enum AgentBuilder.LambdaInstrumentationStrategy.LambdaMetafactoryFactory extends Enum<AgentBuilder.LambdaInstrumentationStrategy.LambdaMetafactoryFactory> implements ByteCodeAppender
A factory for rewriting the JDK's java.lang.invoke.LambdaMetafactory methods for use with Byte Buddy. The code that is created by this factory is roughly equivalent to the following:
 public static CallSite metafactory(MethodHandles.Lookup caller,
                                    String interfaceMethodName,
                                    MethodType factoryType,
                                    MethodType interfaceMethodType,
                                    MethodHandle implementation,
                                    MethodType dynamicMethodType) throws Exception {
   boolean serializable = false;
  List<Class<?>> markerInterfaces = Collections.emptyList();
  List<MethodType> additionalBridges = Collections.emptyList();
   byte[] binaryRepresentation = (byte[]) ClassLoader.getSystemClassLoader().loadClass("net.bytebuddy.agent.builder.LambdaFactory").getDeclaredMethod("make",
     Object.class,
     String.class,
     Object.class,
     Object.class,
     Object.class,
     Object.class,
     boolean.class,
     List.class,
     List.class).invoke(null,
       caller,
       interfaceMethodName,
       factoryType,
       interfaceMethodType,
       implementation,
       dynamicMethodType,
       serializable,
       markerInterfaces,
       additionalBridges);
  Class<?> lambdaClass = ... // loading code
   return factoryType.parameterCount() == 0
     ? new ConstantCallSite(MethodHandles.constant(factoryType.returnType(), lambdaClass.getDeclaredConstructors()[0].newInstance()))
     : new ConstantCallSite(Lookup.IMPL_LOOKUP.findStatic(lambdaClass, "get$Lambda", factoryType));
 }
 
The code's preamble is adjusted for the alternative metafactory to ressemble the following:
 public static CallSite altMetafactory(MethodHandles.Lookup caller,
                                       String interfaceMethodName,
                                       MethodType factoryType,
                                       Object... argument) throws Exception {
   int flags = (Integer) argument[3];
   int index = 4;
  Class<?>[] markerInterface;
   if ((flags& 2) != 0) {
     int count = (Integer) argument[index++];
     markerInterface = newClass<?>[count];
     System.arraycopy(argument, index, markerInterface, 0, count);
     index += count;
   } else {
     markerInterface = newClass<?>[0];
   }
   MethodType[] additionalBridge;
   if ((flags& 2) != 0) {
     int count = (Integer) argument[index++];
     additionalBridge = new MethodType[count];
     System.arraycopy(argument, index, additionalBridge, 0, count);
   } else {
     additionalBridge = new MethodType[0];
   }
   MethodType interfaceMethodType = (MethodType) argument[0];
   MethodHandle implementation = (MethodHandle) argument[1];
   MethodType dynamicMethodType = (MethodType) argument[2];
   boolean serializable = (flags& 1) != 0;
  List<Class<?>> markerInterfaces = Arrays.asList(markerInterface);
  List<MethodType> additionalBridges = Arrays.asList(additionalBridge);
   // ... reminder of method as before
 }
 
  • Enum Constant Details

  • Field Details

  • Constructor Details

    • LambdaMetafactoryFactory

      private LambdaMetafactoryFactory(int stackSize, int localVariableLength)
      Creates a new factory.
      Parameters:
      stackSize - The required stack size for this factory.
      localVariableLength - The required local variable length for this factory.
  • Method Details

    • values

      Returns an array containing the constants of this enum type, in the order they are declared.
      Returns:
      an array containing the constants of this enum type, in the order they are declared
    • valueOf

      Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)
      Parameters:
      name - the name of the enum constant to be returned.
      Returns:
      the enum constant with the specified name
      Throws:
      IllegalArgumentException - if this enum type has no constant with the specified name
      NullPointerException - if the argument is null
    • resolve

      Resolves the loader for the current VM.
      Returns:
      An appropriate loader.
    • apply

      public ByteCodeAppender.Size apply(org.objectweb.asm.MethodVisitor methodVisitor, Implementation.Context implementationContext, MethodDescription instrumentedMethod)
      Applies this byte code appender to a type creation process.
      Specified by:
      apply in interface ByteCodeAppender
      Parameters:
      methodVisitor - The method visitor to which the byte code appender writes its code to.
      implementationContext - The implementation context of the current type creation process.
      instrumentedMethod - The method that is the target of the instrumentation.
      Returns:
      The required size for the applied byte code to run.
    • onDispatch

      protected abstract void onDispatch(org.objectweb.asm.MethodVisitor methodVisitor)
      Invoked upon dispatch. As a result, all values that are presented to Byte Buddy's code generator must be arranged on the stack.
      Parameters:
      methodVisitor - The method visitor to use.