com.fitbur.bytebuddy.implementation.InvocationHandlerAdapter Maven / Gradle / Ivy
The newest version!
package com.fitbur.bytebuddy.implementation;
import com.fitbur.bytebuddy.description.field.FieldDescription;
import com.fitbur.bytebuddy.description.method.MethodDescription;
import com.fitbur.bytebuddy.description.type.TypeDescription;
import com.fitbur.bytebuddy.description.type.TypeList;
import com.fitbur.bytebuddy.dynamic.scaffold.InstrumentedType;
import com.fitbur.bytebuddy.implementation.bytecode.ByteCodeAppender;
import com.fitbur.bytebuddy.implementation.bytecode.StackManipulation;
import com.fitbur.bytebuddy.implementation.bytecode.assign.Assigner;
import com.fitbur.bytebuddy.implementation.bytecode.collection.ArrayFactory;
import com.fitbur.bytebuddy.implementation.bytecode.constant.MethodConstant;
import com.fitbur.bytebuddy.implementation.bytecode.member.FieldAccess;
import com.fitbur.bytebuddy.implementation.bytecode.member.MethodInvocation;
import com.fitbur.bytebuddy.implementation.bytecode.member.MethodReturn;
import com.fitbur.bytebuddy.implementation.bytecode.member.MethodVariableAccess;
import com.fitbur.bytebuddy.jar.asm.MethodVisitor;
import com.fitbur.bytebuddy.jar.asm.Opcodes;
import java.lang.reflect.InvocationHandler;
import java.util.ArrayList;
import java.util.List;
import static com.fitbur.bytebuddy.matcher.ElementMatchers.named;
/**
* An adapter for adapting an {@link java.lang.reflect.InvocationHandler}. The adapter allows the invocation handler
* to also intercept method calls to non-interface methods.
*/
public abstract class InvocationHandlerAdapter implements Implementation {
/**
* Indicates that a value should not be cached.
*/
private static final boolean NO_CACHING = false;
/**
* Indicates that a {@link java.lang.reflect.Method} should be cached.
*/
protected static final boolean CACHING = true;
/**
* The prefix for field that are created for storing the instrumented value.
*/
private static final String PREFIX = "invocationHandler";
/**
* A type description of the {@link InvocationHandler}.
*/
private static final TypeDescription.Generic INVOCATION_HANDLER_TYPE = new TypeDescription.Generic.OfNonGenericType.ForLoadedType(InvocationHandler.class);
/**
* The name of the field for storing an invocation handler.
*/
protected final String fieldName;
/**
* The assigner that is used for assigning the return invocation handler's return value to the
* intercepted method's return value.
*/
protected final Assigner assigner;
/**
* Determines if the {@link java.lang.reflect.Method} instances that are handed to the intercepted methods are
* cached in {@code static} fields.
*/
protected final boolean cacheMethods;
/**
* Creates a new invocation handler for a given field.
*
* @param fieldName The name of the field.
* @param cacheMethods Determines if the {@link java.lang.reflect.Method} instances that are handed to the
* intercepted methods are cached in {@code static} fields.
* @param assigner The assigner to apply when defining this implementation.
*/
protected InvocationHandlerAdapter(String fieldName, boolean cacheMethods, Assigner assigner) {
this.fieldName = fieldName;
this.cacheMethods = cacheMethods;
this.assigner = assigner;
}
/**
* Creates an implementation for any instance of an {@link java.lang.reflect.InvocationHandler} that delegates
* all method interceptions to the given instance which will be stored in a {@code static} field.
*
* @param invocationHandler The invocation handler to which all method calls are delegated.
* @return An implementation that delegates all method interceptions to the given invocation handler.
*/
public static InvocationHandlerAdapter of(InvocationHandler invocationHandler) {
return of(invocationHandler, String.format("%s$%d", PREFIX, Math.abs(invocationHandler.hashCode() % Integer.MAX_VALUE)));
}
/**
* Creates an implementation for any instance of an {@link java.lang.reflect.InvocationHandler} that delegates
* all method interceptions to the given instance which will be stored in a {@code static} field.
*
* @param invocationHandler The invocation handler to which all method calls are delegated.
* @param fieldName The name of the field.
* @return An implementation that delegates all method interceptions to the given invocation handler.
*/
public static InvocationHandlerAdapter of(InvocationHandler invocationHandler, String fieldName) {
return new ForStaticDelegation(fieldName, NO_CACHING, Assigner.DEFAULT, invocationHandler);
}
/**
* Creates an implementation for any {@link java.lang.reflect.InvocationHandler} that delegates
* all method interceptions to a {@code public} instance field with the given name. This field has to be
* set before any invocations are intercepted. Otherwise, a {@link java.lang.NullPointerException} will be
* thrown.
*
* @param fieldName The name of the field.
* @return An implementation that delegates all method interceptions to an instance field of the given name.
*/
public static InvocationHandlerAdapter toInstanceField(String fieldName) {
return new ForInstanceDelegation(fieldName, NO_CACHING, Assigner.DEFAULT);
}
/**
* Returns a list of stack manipulations that loads all arguments of an instrumented method.
*
* @param instrumentedMethod The method that is instrumented.
* @return A list of stack manipulation that loads all arguments of an instrumented method.
*/
private List argumentValuesOf(MethodDescription instrumentedMethod) {
TypeList.Generic parameterTypes = instrumentedMethod.getParameters().asTypeList();
List instruction = new ArrayList(parameterTypes.size());
int currentIndex = 1;
for (TypeDescription.Generic parameterType : parameterTypes) {
instruction.add(new StackManipulation.Compound(
MethodVariableAccess.of(parameterType).loadOffset(currentIndex),
assigner.assign(parameterType, TypeDescription.Generic.OBJECT, Assigner.Typing.STATIC)));
currentIndex += parameterType.getStackSize().getSize();
}
return instruction;
}
/**
* By default, any {@link java.lang.reflect.Method} instance that is handed over to an
* {@link java.lang.reflect.InvocationHandler} is created on each invocation of the method.
* {@link java.lang.reflect.Method} look-ups are normally cached by its defining {@link java.lang.Class} what
* makes a repeated look-up of a method little expensive. However, because {@link java.lang.reflect.Method}
* instances are mutable by their {@link java.lang.reflect.AccessibleObject} contact, any looked-up instance
* needs to be copied by its defining {@link java.lang.Class} before exposing it. This can cause performance
* deficits when a method is for example called repeatedly in a loop. By enabling the method cache, this
* performance penalty can be avoided by caching a single {@link java.lang.reflect.Method} instance for
* any intercepted method as a {@code static} field in the instrumented type.
*
* @return A similar invocation handler adapter which caches any {@link java.lang.reflect.Method} instance
* in form of a {@code static} field.
*/
public abstract AssignerConfigurable withMethodCache();
/**
* Applies an implementation that delegates to a invocation handler.
*
* @param methodVisitor The method visitor for writing the byte code to.
* @param implementationContext The implementation context for the current implementation.
* @param instrumentedMethod The method that is instrumented.
* @param instrumentedType The type that is instrumented.
* @param preparingManipulation A stack manipulation that applies any preparation to the operand stack.
* @return The size of the applied assignment.
*/
protected ByteCodeAppender.Size apply(MethodVisitor methodVisitor,
Context implementationContext,
MethodDescription instrumentedMethod,
TypeDescription instrumentedType,
StackManipulation preparingManipulation) {
StackManipulation.Size stackSize = new StackManipulation.Compound(
preparingManipulation,
FieldAccess.forField(instrumentedType.getDeclaredFields()
.filter((named(fieldName))).getOnly()).getter(),
MethodVariableAccess.REFERENCE.loadOffset(0),
cacheMethods
? MethodConstant.forMethod(instrumentedMethod.asDefined()).cached()
: MethodConstant.forMethod(instrumentedMethod.asDefined()),
ArrayFactory.forType(TypeDescription.Generic.OBJECT).withValues(argumentValuesOf(instrumentedMethod)),
MethodInvocation.invoke(INVOCATION_HANDLER_TYPE.getDeclaredMethods().getOnly()),
assigner.assign(TypeDescription.Generic.OBJECT, instrumentedMethod.getReturnType(), Assigner.Typing.DYNAMIC),
MethodReturn.returning(instrumentedMethod.getReturnType().asErasure())
).apply(methodVisitor, implementationContext);
return new ByteCodeAppender.Size(stackSize.getMaximalSize(), instrumentedMethod.getStackSize());
}
@Override
public boolean equals(Object other) {
if (this == other) return true;
if (other == null || getClass() != other.getClass()) return false;
InvocationHandlerAdapter that = (InvocationHandlerAdapter) other;
return cacheMethods == that.cacheMethods
&& assigner.equals(that.assigner)
&& fieldName.equals(that.fieldName);
}
@Override
public int hashCode() {
int result = fieldName.hashCode();
result = 31 * result + assigner.hashCode();
result = 31 * result + (cacheMethods ? 1 : 0);
return result;
}
/**
* Allows for the configuration of an {@link com.fitbur.bytebuddy.implementation.bytecode.assign.Assigner}
* of an {@link com.fitbur.bytebuddy.implementation.InvocationHandlerAdapter}.
*/
protected interface AssignerConfigurable extends Implementation {
/**
* Configures an assigner to use with this invocation handler adapter.
*
* @param assigner The assigner to apply when defining this implementation.
* @return This instrumentation with the given {@code assigner} configured.
*/
Implementation withAssigner(Assigner assigner);
}
/**
* An implementation of an {@link com.fitbur.bytebuddy.implementation.InvocationHandlerAdapter} that delegates method
* invocations to an adapter that is stored in a static field.
*/
protected static class ForStaticDelegation extends InvocationHandlerAdapter implements AssignerConfigurable {
/**
* The invocation handler to which method interceptions are to be delegated.
*/
protected final InvocationHandler invocationHandler;
/**
* Creates a new invocation handler adapter for delegating invocations to an invocation handler that is stored
* in a static field.
*
* @param fieldName The name of the field.
* @param cacheMethods Determines if the {@link java.lang.reflect.Method} instances that are handed to the
* intercepted methods are cached in {@code static} fields.
* @param assigner The assigner to apply when defining this implementation.
* @param invocationHandler The invocation handler to which all method calls are delegated.
*/
protected ForStaticDelegation(String fieldName, boolean cacheMethods, Assigner assigner, InvocationHandler invocationHandler) {
super(fieldName, cacheMethods, assigner);
this.invocationHandler = invocationHandler;
}
@Override
public AssignerConfigurable withMethodCache() {
return new ForStaticDelegation(fieldName, CACHING, assigner, invocationHandler);
}
@Override
public Implementation withAssigner(Assigner assigner) {
return new ForStaticDelegation(fieldName, cacheMethods, assigner, invocationHandler);
}
@Override
public InstrumentedType prepare(InstrumentedType instrumentedType) {
return instrumentedType
.withField(new FieldDescription.Token(fieldName, Opcodes.ACC_SYNTHETIC | Opcodes.ACC_STATIC | Opcodes.ACC_PUBLIC, INVOCATION_HANDLER_TYPE))
.withInitializer(new LoadedTypeInitializer.ForStaticField(fieldName, invocationHandler));
}
@Override
public ByteCodeAppender appender(Target implementationTarget) {
return new Appender(implementationTarget.getInstrumentedType());
}
@Override
public boolean equals(Object other) {
return this == other || !(other == null || getClass() != other.getClass())
&& super.equals(other)
&& invocationHandler.equals(((ForStaticDelegation) other).invocationHandler);
}
@Override
public int hashCode() {
return 31 * super.hashCode() + invocationHandler.hashCode();
}
@Override
public String toString() {
return "InvocationHandlerAdapter.ForStaticDelegation{" +
"fieldName=" + fieldName +
", cacheMethods=" + cacheMethods +
", invocationHandler=" + invocationHandler +
'}';
}
/**
* An appender for implementing the {@link com.fitbur.bytebuddy.implementation.InvocationHandlerAdapter.ForStaticDelegation}.
*/
protected class Appender implements ByteCodeAppender {
/**
* The instrumented type for which the methods are being intercepted.
*/
private final TypeDescription instrumentedType;
/**
* Creates a new appender.
*
* @param instrumentedType The type that is instrumented.
*/
protected Appender(TypeDescription instrumentedType) {
this.instrumentedType = instrumentedType;
}
@Override
public Size apply(MethodVisitor methodVisitor, Context implementationContext, MethodDescription instrumentedMethod) {
return ForStaticDelegation.this.apply(methodVisitor,
implementationContext,
instrumentedMethod,
instrumentedType,
StackManipulation.Trivial.INSTANCE);
}
/**
* Returns the outer class.
*
* @return The outer class of this instance.
*/
private InvocationHandlerAdapter getInvocationHandlerAdapter() {
return ForStaticDelegation.this;
}
@Override
public boolean equals(Object other) {
return this == other || !(other == null || getClass() != other.getClass())
&& instrumentedType.equals(((Appender) other).instrumentedType)
&& ForStaticDelegation.this.equals(((Appender) other).getInvocationHandlerAdapter());
}
@Override
public int hashCode() {
return 31 * ForStaticDelegation.this.hashCode() + instrumentedType.hashCode();
}
@Override
public String toString() {
return "InvocationHandlerAdapter.ForStaticDelegation.Appender{" +
"invocationHandlerAdapter=" + ForStaticDelegation.this +
"instrumentedType=" + instrumentedType +
'}';
}
}
}
/**
* An implementation of an {@link com.fitbur.bytebuddy.implementation.InvocationHandlerAdapter} that delegates method
* invocations to an adapter that is stored in an instance field.
*/
protected static class ForInstanceDelegation extends InvocationHandlerAdapter implements AssignerConfigurable {
/**
* Creates a new invocation handler adapter for delegating invocations to an invocation handler that is stored
* in an instance field.
*
* @param fieldName The name of the field.
* @param cacheMethods Determines if the {@link java.lang.reflect.Method} instances that are handed to the
* intercepted methods are cached in {@code static} fields.
* @param assigner The assigner to apply when defining this implementation.
*/
protected ForInstanceDelegation(String fieldName, boolean cacheMethods, Assigner assigner) {
super(fieldName, cacheMethods, assigner);
}
@Override
public AssignerConfigurable withMethodCache() {
return new ForInstanceDelegation(fieldName, CACHING, assigner);
}
@Override
public Implementation withAssigner(Assigner assigner) {
return new ForInstanceDelegation(fieldName, cacheMethods, assigner);
}
@Override
public InstrumentedType prepare(InstrumentedType instrumentedType) {
return instrumentedType.withField(new FieldDescription.Token(fieldName, Opcodes.ACC_SYNTHETIC | Opcodes.ACC_PUBLIC, INVOCATION_HANDLER_TYPE));
}
@Override
public ByteCodeAppender appender(Target implementationTarget) {
return new Appender(implementationTarget.getInstrumentedType());
}
@Override
public String toString() {
return "InvocationHandlerAdapter.ForInstanceDelegation{" +
"fieldName=" + fieldName +
"cacheMethods=" + cacheMethods +
'}';
}
/**
* An appender for implementing the {@link com.fitbur.bytebuddy.implementation.InvocationHandlerAdapter.ForInstanceDelegation}.
*/
protected class Appender implements ByteCodeAppender {
/**
* The type that is subject of the instrumentation.
*/
private final TypeDescription instrumentedType;
/**
* Creates a new appender.
*
* @param instrumentedType The type that is instrumented.
*/
protected Appender(TypeDescription instrumentedType) {
this.instrumentedType = instrumentedType;
}
@Override
public Size apply(MethodVisitor methodVisitor, Context implementationContext, MethodDescription instrumentedMethod) {
return ForInstanceDelegation.this.apply(methodVisitor,
implementationContext,
instrumentedMethod,
instrumentedType,
MethodVariableAccess.of(instrumentedType).loadOffset(0));
}
@Override
public boolean equals(Object other) {
return this == other || !(other == null || getClass() != other.getClass())
&& instrumentedType.equals(((Appender) other).instrumentedType)
&& ForInstanceDelegation.this.equals(((Appender) other).getInvocationHandlerAdapter());
}
/**
* Returns the outer class.
*
* @return The outer class.
*/
private InvocationHandlerAdapter getInvocationHandlerAdapter() {
return ForInstanceDelegation.this;
}
@Override
public int hashCode() {
return 31 * ForInstanceDelegation.this.hashCode() + instrumentedType.hashCode();
}
@Override
public String toString() {
return "InvocationHandlerAdapter.ForInstanceDelegation.Appender{" +
"invocationHandlerAdapter=" + ForInstanceDelegation.this +
"instrumentedType=" + instrumentedType +
'}';
}
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy