Package net.bytebuddy.agent.builder
Class LambdaFactory
- java.lang.Object
-
- net.bytebuddy.agent.builder.LambdaFactory
-
@Enhance public class LambdaFactory extends java.lang.Object
This class serves as a dispatcher for creating lambda expression objects when Byte Buddy is configured to instrument thejava.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 Summary
Fields Modifier and Type Field Description static java.util.Map<java.lang.instrument.ClassFileTransformer,LambdaFactory>
CLASS_FILE_TRANSFORMERS
A mapping of all registered class file transformers and their lambda factories, linked in their application order.private java.lang.reflect.Method
dispatcher
The dispatcher method to invoke for creating a new lambda instance.private static java.lang.String
FIELD_NAME
The name of the field to access.private java.lang.Object
target
The target instance that is a factory for creating lambdas.
-
Constructor Summary
Constructors Constructor Description LambdaFactory(java.lang.Object target, java.lang.reflect.Method dispatcher)
Creates a new lambda factory.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private byte[]
invoke(java.lang.Object caller, java.lang.String invokedName, java.lang.Object invokedType, java.lang.Object samMethodType, java.lang.Object implMethod, java.lang.Object instantiatedMethodType, boolean serializable, java.util.List<java.lang.Class<?>> markerInterfaces, java.util.List<?> additionalBridges, java.util.Collection<java.lang.instrument.ClassFileTransformer> classFileTransformers)
Applies this lambda meta factory.static byte[]
make(java.lang.Object caller, java.lang.String invokedName, java.lang.Object invokedType, java.lang.Object samMethodType, java.lang.Object implMethod, java.lang.Object instantiatedMethodType, boolean serializable, java.util.List<java.lang.Class<?>> markerInterfaces, java.util.List<?> additionalBridges)
Dispatches the creation of a new class representing a class file.static boolean
register(java.lang.instrument.ClassFileTransformer classFileTransformer, java.lang.Object classFileFactory)
Registers a class file transformer together with a factory for creating a lambda expression.static boolean
release(java.lang.instrument.ClassFileTransformer classFileTransformer)
Releases a class file transformer.
-
-
-
Field Detail
-
FIELD_NAME
private static final java.lang.String FIELD_NAME
The name of the field to access.- See Also:
- Constant Field Values
-
CLASS_FILE_TRANSFORMERS
public static final java.util.Map<java.lang.instrument.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.
-
target
private final java.lang.Object target
The target instance that is a factory for creating lambdas.
-
dispatcher
private final java.lang.reflect.Method dispatcher
The dispatcher method to invoke for creating a new lambda instance.
-
-
Constructor Detail
-
LambdaFactory
public LambdaFactory(java.lang.Object target, java.lang.reflect.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(java.lang.instrument.ClassFileTransformer classFileTransformer, java.lang.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 signaturebyte[] 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 theLambdaMetafactory
must be instrumented to delegate to this alternative factory.
-
release
public static boolean release(java.lang.instrument.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 theLambdaMetafactory
must be instrumented to no longer delegate to this alternative factory.
-
invoke
private byte[] invoke(java.lang.Object caller, java.lang.String invokedName, java.lang.Object invokedType, java.lang.Object samMethodType, java.lang.Object implMethod, java.lang.Object instantiatedMethodType, boolean serializable, java.util.List<java.lang.Class<?>> markerInterfaces, java.util.List<?> additionalBridges, java.util.Collection<java.lang.instrument.ClassFileTransformer> classFileTransformers)
Applies this lambda meta factory.- 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.classFileTransformers
- A collection of class file transformers to apply when creating the class.- Returns:
- A binary representation of the transformed class file.
-
make
public static byte[] make(java.lang.Object caller, java.lang.String invokedName, java.lang.Object invokedType, java.lang.Object samMethodType, java.lang.Object implMethod, java.lang.Object instantiatedMethodType, boolean serializable, java.util.List<java.lang.Class<?>> markerInterfaces, java.util.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.
-
-