Package net.bytebuddy.agent.builder
Enum Class 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>
,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:
The code's preamble is adjusted for the alternative metafactory to ressemble 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)); }
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 }
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static interface
A loader is responsible for loading a generated class file in the current VM.Nested classes/interfaces inherited from class java.lang.Enum
Enum.EnumDesc<E extends Enum<E>>
Nested classes/interfaces inherited from interface net.bytebuddy.implementation.bytecode.ByteCodeAppender
ByteCodeAppender.Compound, ByteCodeAppender.Simple, ByteCodeAppender.Size
-
Enum Constant Summary
Enum ConstantsEnum ConstantDescriptionImplements thejava.lang.invoke.LambdaMetafactory#altMetafactory
method.Implements thejava.lang.invoke.LambdaMetafactory#metafactory
method. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final AgentBuilder.LambdaInstrumentationStrategy.LambdaMetafactoryFactory.Loader
A loader for the generated lambda class.private final int
The required local variable length for this factory.private final int
The required stack size for this factory. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
LambdaMetafactoryFactory
(int stackSize, int localVariableLength) Creates a new factory. -
Method Summary
Modifier and TypeMethodDescriptionapply
(org.objectweb.asm.MethodVisitor methodVisitor, Implementation.Context implementationContext, MethodDescription instrumentedMethod) Applies this byte code appender to a type creation process.protected abstract void
onDispatch
(org.objectweb.asm.MethodVisitor methodVisitor) Invoked upon dispatch.resolve()
Resolves the loader for the current VM.Returns the enum constant of this class with the specified name.values()
Returns an array containing the constants of this enum class, in the order they are declared.
-
Enum Constant Details
-
REGULAR
Implements thejava.lang.invoke.LambdaMetafactory#metafactory
method. -
ALTERNATIVE
Implements thejava.lang.invoke.LambdaMetafactory#altMetafactory
method.
-
-
Field Details
-
LOADER
private static final AgentBuilder.LambdaInstrumentationStrategy.LambdaMetafactoryFactory.Loader LOADERA loader for the generated lambda class. -
stackSize
private final int stackSizeThe required stack size for this factory. -
localVariableLength
private final int localVariableLengthThe required local variable length for this factory.
-
-
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 class, in the order they are declared.- Returns:
- an array containing the constants of this enum class, in the order they are declared
-
valueOf
public static AgentBuilder.LambdaInstrumentationStrategy.LambdaMetafactoryFactory valueOf(String name) Returns the enum constant of this class with the specified name. The string must match exactly an identifier used to declare an enum constant in this class. (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 class has no constant with the specified nameNullPointerException
- 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 interfaceByteCodeAppender
- 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.
-