Enum AgentBuilder.LambdaInstrumentationStrategy.LambdaMetafactoryFactory

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Comparable<AgentBuilder.LambdaInstrumentationStrategy.LambdaMetafactoryFactory>, AsmVisitorWrapper.ForDeclaredMethods.MethodVisitorWrapper
    Enclosing class:
    AgentBuilder.LambdaInstrumentationStrategy

    protected static enum AgentBuilder.LambdaInstrumentationStrategy.LambdaMetafactoryFactory
    extends java.lang.Enum<AgentBuilder.LambdaInstrumentationStrategy.LambdaMetafactoryFactory>
    implements AsmVisitorWrapper.ForDeclaredMethods.MethodVisitorWrapper
    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
     }
     
    • Constructor Detail

      • 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 Detail

      • values

        public static AgentBuilder.LambdaInstrumentationStrategy.LambdaMetafactoryFactory[] values()
        Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:
        for (AgentBuilder.LambdaInstrumentationStrategy.LambdaMetafactoryFactory c : AgentBuilder.LambdaInstrumentationStrategy.LambdaMetafactoryFactory.values())
            System.out.println(c);
        
        Returns:
        an array containing the constants of this enum type, in the order they are declared
      • valueOf

        public static AgentBuilder.LambdaInstrumentationStrategy.LambdaMetafactoryFactory valueOf​(java.lang.String name)
        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:
        java.lang.IllegalArgumentException - if this enum type has no constant with the specified name
        java.lang.NullPointerException - if the argument is null
      • wrap

        public org.objectweb.asm.MethodVisitor wrap​(TypeDescription typeDescription,
                                                    MethodDescription methodDescription,
                                                    org.objectweb.asm.MethodVisitor methodVisitor,
                                                    Implementation.Context implementationContext,
                                                    TypePool typePool,
                                                    int writerFlags,
                                                    int readerFlags)
        Wraps a method visitor.
        Specified by:
        wrap in interface AsmVisitorWrapper.ForDeclaredMethods.MethodVisitorWrapper
        Parameters:
        typeDescription - The instrumented type.
        methodDescription - The method that is currently being defined.
        methodVisitor - The original field visitor that defines the given method.
        implementationContext - The implementation context to use.
        typePool - The type pool to use.
        writerFlags - The ASM ClassWriter reader flags to consider.
        readerFlags - The ASM ClassReader reader flags to consider.
        Returns:
        The wrapped method visitor.
      • 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.