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
}
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static interfaceA loader is responsible for loading a generated class file in the current VM.Nested classes/interfaces inherited from class Enum
Enum.EnumDesc<E>Nested classes/interfaces inherited from interface ByteCodeAppender
ByteCodeAppender.Compound, ByteCodeAppender.Simple, ByteCodeAppender.Size -
Enum Constant Summary
Enum ConstantsEnum ConstantDescriptionImplements thejava.lang.invoke.LambdaMetafactory#altMetafactorymethod.Implements thejava.lang.invoke.LambdaMetafactory#metafactorymethod. -
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 voidonDispatch(org.objectweb.asm.MethodVisitor methodVisitor) Invoked upon dispatch.Returns the enum constant of this type with the specified name.values()Returns an array containing the constants of this enum type, in the order they are declared.
-
Enum Constant Details
-
REGULAR
Implements thejava.lang.invoke.LambdaMetafactory#metafactorymethod. -
ALTERNATIVE
Implements thejava.lang.invoke.LambdaMetafactory#altMetafactorymethod.
-
-
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
public static AgentBuilder.LambdaInstrumentationStrategy.LambdaMetafactoryFactory valueOf(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:
IllegalArgumentException- if this enum type has no constant with the specified nameNullPointerException- if the argument is null
-
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:
applyin 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.
-