Class LambdaFactory


  • @Enhance
    public class LambdaFactory
    extends Object
    This class serves as a dispatcher for creating lambda expression objects when Byte Buddy is configured to instrument the java.lang.invoke.LambdaMetafactory. For this purpose, this class is injected into the class path to serve as a VM-global singleton and for becoming reachable from the JVM's meta factory. This class keeps a reference to all registered transformers which need to be explicitly deregistered in order to avoid a memory leak.
    • Field Detail

      • CLASS_FILE_TRANSFORMERS

        public static final Map<ClassFileTransformer,​LambdaFactory> CLASS_FILE_TRANSFORMERS
        A mapping of all registered class file transformers and their lambda factories, linked in their application order. This field must not be accessed directly but only by reading this class from the system class loader.
    • Constructor Detail

      • LambdaFactory

        public LambdaFactory​(Object target,
                             Method dispatcher)
        Creates a new lambda factory.
        Parameters:
        target - The target instance that is a factory for creating lambdas.
        dispatcher - The dispatcher method to invoke for creating a new lambda instance.
    • Method Detail

      • register

        public static boolean register​(ClassFileTransformer classFileTransformer,
                                       Object classFileFactory)
        Registers a class file transformer together with a factory for creating a lambda expression. It is possible to call this method independently of the class loader's context as the supplied injector makes sure that the manipulated collection is the one that is held by the system class loader.
        Parameters:
        classFileTransformer - The class file transformer to register.
        classFileFactory - The lambda class file factory to use. This factory must define a visible instance method with the signature byte[] make(Object, String, Object, Object, Object, Object, boolean, List, List, Collection. The arguments provided are the invokedynamic call site's lookup object, the lambda method's name, the factory method's type, the lambda method's type, the target method's handle, the specialized method type of the lambda expression, a boolean to indicate serializability, a list of marker interfaces, a list of additional bridges and a collection of class file transformers to apply.
        Returns:
        true if this is the first registered transformer. This indicates that the LambdaMetafactory must be instrumented to delegate to this alternative factory.
      • release

        public static boolean release​(ClassFileTransformer classFileTransformer)
        Releases a class file transformer.
        Parameters:
        classFileTransformer - The class file transformer to release.
        Returns:
        true if the removed transformer was the last class file transformer registered. This indicates that the LambdaMetafactory must be instrumented to no longer delegate to this alternative factory.
      • make

        public static byte[] make​(Object caller,
                                  String invokedName,
                                  Object invokedType,
                                  Object samMethodType,
                                  Object implMethod,
                                  Object instantiatedMethodType,
                                  boolean serializable,
                                  List<Class<?>> markerInterfaces,
                                  List<?> additionalBridges)
        Dispatches the creation of a new class representing a class file.
        Parameters:
        caller - A lookup context representing the creating class of this lambda expression.
        invokedName - The name of the lambda expression's represented method.
        invokedType - The type of the lambda expression's factory method.
        samMethodType - The type of the lambda expression's represented method.
        implMethod - A handle representing the target of the lambda expression's method.
        instantiatedMethodType - A specialization of the type of the lambda expression's represented method.
        serializable - true if the lambda expression should be serializable.
        markerInterfaces - A list of interfaces for the lambda expression to represent.
        additionalBridges - A list of additional bridge methods to be implemented by the lambda expression.
        Returns:
        A binary representation of the transformed class file.