Package net.bytebuddy.agent.builder
Enum AgentBuilder.LambdaInstrumentationStrategy.LambdaMetafactoryFactory
- java.lang.Object
-
- java.lang.Enum<AgentBuilder.LambdaInstrumentationStrategy.LambdaMetafactoryFactory>
-
- net.bytebuddy.agent.builder.AgentBuilder.LambdaInstrumentationStrategy.LambdaMetafactoryFactory
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Comparable<AgentBuilder.LambdaInstrumentationStrategy.LambdaMetafactoryFactory>
,ByteCodeAppender
- Enclosing class:
- AgentBuilder.LambdaInstrumentationStrategy
protected static enum AgentBuilder.LambdaInstrumentationStrategy.LambdaMetafactoryFactory extends java.lang.Enum<AgentBuilder.LambdaInstrumentationStrategy.LambdaMetafactoryFactory> implements ByteCodeAppender
A factory for rewriting the JDK'sjava.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 Classes Modifier and Type Class Description protected static interface
AgentBuilder.LambdaInstrumentationStrategy.LambdaMetafactoryFactory.Loader
A loader is responsible for loading a generated class file in the current VM.-
Nested classes/interfaces inherited from interface net.bytebuddy.implementation.bytecode.ByteCodeAppender
ByteCodeAppender.Compound, ByteCodeAppender.Simple, ByteCodeAppender.Size
-
-
Enum Constant Summary
Enum Constants Enum Constant Description ALTERNATIVE
Implements thejava.lang.invoke.LambdaMetafactory#altMetafactory
method.REGULAR
Implements thejava.lang.invoke.LambdaMetafactory#metafactory
method.
-
Field Summary
Fields Modifier and Type Field Description private static AgentBuilder.LambdaInstrumentationStrategy.LambdaMetafactoryFactory.Loader
LOADER
A loader for the generated lambda class.private int
localVariableLength
The required local variable length for this factory.private int
stackSize
The required stack size for this factory.
-
Constructor Summary
Constructors Modifier Constructor Description private
LambdaMetafactoryFactory(int stackSize, int localVariableLength)
Creates a new factory.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description ByteCodeAppender.Size
apply(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.private static AgentBuilder.LambdaInstrumentationStrategy.LambdaMetafactoryFactory.Loader
resolve()
Resolves the loader for the current VM.static AgentBuilder.LambdaInstrumentationStrategy.LambdaMetafactoryFactory
valueOf(java.lang.String name)
Returns the enum constant of this type with the specified name.static AgentBuilder.LambdaInstrumentationStrategy.LambdaMetafactoryFactory[]
values()
Returns an array containing the constants of this enum type, in the order they are declared.
-
-
-
Enum Constant Detail
-
REGULAR
public static final AgentBuilder.LambdaInstrumentationStrategy.LambdaMetafactoryFactory REGULAR
Implements thejava.lang.invoke.LambdaMetafactory#metafactory
method.
-
ALTERNATIVE
public static final AgentBuilder.LambdaInstrumentationStrategy.LambdaMetafactoryFactory ALTERNATIVE
Implements thejava.lang.invoke.LambdaMetafactory#altMetafactory
method.
-
-
Field Detail
-
LOADER
private static final AgentBuilder.LambdaInstrumentationStrategy.LambdaMetafactoryFactory.Loader LOADER
A loader for the generated lambda class.
-
stackSize
private final int stackSize
The required stack size for this factory.
-
localVariableLength
private final int 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 namejava.lang.NullPointerException
- if the argument is null
-
resolve
private static AgentBuilder.LambdaInstrumentationStrategy.LambdaMetafactoryFactory.Loader 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.
-
-