com.fitbur.mockito.bytebuddy.dynamic.scaffold.inline.RebaseImplementationTarget Maven / Gradle / Ivy
package com.fitbur.mockito.bytebuddy.dynamic.scaffold.inline;
import com.fitbur.mockito.bytebuddy.description.method.MethodDescription;
import com.fitbur.mockito.bytebuddy.description.type.TypeDescription;
import com.fitbur.mockito.bytebuddy.dynamic.scaffold.MethodGraph;
import com.fitbur.mockito.bytebuddy.implementation.Implementation;
import com.fitbur.mockito.bytebuddy.implementation.bytecode.StackManipulation;
import com.fitbur.mockito.bytebuddy.implementation.bytecode.member.MethodInvocation;
import com.fitbur.mockito.bytebuddy.jar.asm.MethodVisitor;
import java.util.Map;
/**
* An implementation target for redefining a given type while preserving the original methods within the
* instrumented type.
*
* Super method calls are merely emulated by this {@link Implementation.Target} in order
* to preserve Java's super call semantics a user would expect when invoking a {@code super}-prefixed method. This
* means that original methods are either moved to renamed {@code private} methods which are never dispatched
* virtually or they are invoked directly via the {@code INVOKESPECIAL} invocation to explicitly forbid a virtual
* dispatch.
*/
public class RebaseImplementationTarget extends Implementation.Target.AbstractBase {
/**
* A mapping of the instrumented type's declared methods by each method's token.
*/
private final Map rebaseableMethods;
/**
* Creates a rebase implementation target.
*
* @param instrumentedType The instrumented type.
* @param methodGraph A method graph of the instrumented type.
* @param rebaseableMethods A mapping of the instrumented type's declared methods by each method's token.
*/
protected RebaseImplementationTarget(TypeDescription instrumentedType,
MethodGraph.Linked methodGraph,
Map rebaseableMethods) {
super(instrumentedType, methodGraph);
this.rebaseableMethods = rebaseableMethods;
}
/**
* Creates a new rebase implementation target.
*
* @param instrumentedType The instrumented type.
* @param methodGraph A method graph of the instrumented type.
* @param methodRebaseResolver A method rebase resolver to be used when calling a rebased method.
* @return An implementation target for the given input.
*/
protected static Implementation.Target of(TypeDescription instrumentedType, MethodGraph.Linked methodGraph, MethodRebaseResolver methodRebaseResolver) {
return new RebaseImplementationTarget(instrumentedType, methodGraph, methodRebaseResolver.asTokenMap());
}
@Override
public Implementation.SpecialMethodInvocation invokeSuper(MethodDescription.SignatureToken token) {
MethodRebaseResolver.Resolution resolution = rebaseableMethods.get(token);
return resolution == null
? invokeSuper(methodGraph.getSuperClassGraph().locate(token))
: invokeSuper(resolution);
}
/**
* Creates a special method invocation for the given node.
*
* @param node The node for which a special method invocation is to be created.
* @return A special method invocation for the provided node.
*/
private Implementation.SpecialMethodInvocation invokeSuper(MethodGraph.Node node) {
return node.getSort().isResolved()
? Implementation.SpecialMethodInvocation.Simple.of(node.getRepresentative(), instrumentedType.getSuperClass().asErasure())
: Implementation.SpecialMethodInvocation.Illegal.INSTANCE;
}
/**
* Creates a special method invocation for the given rebase resolution.
*
* @param resolution The resolution for which a special method invocation is to be created.
* @return A special method invocation for the provided resolution.
*/
private Implementation.SpecialMethodInvocation invokeSuper(MethodRebaseResolver.Resolution resolution) {
return resolution.isRebased()
? RebasedMethodInvocation.of(resolution.getResolvedMethod(), instrumentedType, resolution.getAdditionalArguments())
: Implementation.SpecialMethodInvocation.Simple.of(resolution.getResolvedMethod(), instrumentedType);
}
@Override
public TypeDescription getOriginType() {
return instrumentedType;
}
@Override
public boolean equals(Object other) {
return this == other || !(other == null || getClass() != other.getClass())
&& super.equals(other)
&& rebaseableMethods.equals(((RebaseImplementationTarget) other).rebaseableMethods);
}
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + rebaseableMethods.hashCode();
return result;
}
@Override
public String toString() {
return "RebaseImplementationTarget{" +
", instrumentedType=" + instrumentedType +
", methodGraph=" + methodGraph +
", rebaseableMethods=" + rebaseableMethods +
'}';
}
/**
* A {@link Implementation.SpecialMethodInvocation} which invokes a rebased method
* as given by a {@link MethodRebaseResolver}.
*/
protected static class RebasedMethodInvocation extends Implementation.SpecialMethodInvocation.AbstractBase {
/**
* The method to invoke via a special method invocation.
*/
private final MethodDescription methodDescription;
/**
* The instrumented type on which the method should be invoked on.
*/
private final TypeDescription instrumentedType;
/**
* The stack manipulation to execute in order to invoke the rebased method.
*/
private final StackManipulation stackManipulation;
/**
* Creates a new rebased method invocation.
*
* @param methodDescription The method to invoke via a special method invocation.
* @param instrumentedType The instrumented type on which the method should be invoked on.
* @param stackManipulation The stack manipulation to execute in order to invoke the rebased method.
*/
protected RebasedMethodInvocation(MethodDescription methodDescription, TypeDescription instrumentedType, StackManipulation stackManipulation) {
this.methodDescription = methodDescription;
this.instrumentedType = instrumentedType;
this.stackManipulation = stackManipulation;
}
/**
* Creates a special method invocation for the given method.
*
* @param resolvedMethod The rebased method to be invoked.
* @param instrumentedType The instrumented type on which the method is to be invoked if it is non-static.
* @param additionalArguments Any additional arguments that are to be provided to the rebased method.
* @return A special method invocation of the rebased method.
*/
protected static Implementation.SpecialMethodInvocation of(MethodDescription resolvedMethod,
TypeDescription instrumentedType,
StackManipulation additionalArguments) {
StackManipulation stackManipulation = resolvedMethod.isStatic()
? MethodInvocation.invoke(resolvedMethod)
: MethodInvocation.invoke(resolvedMethod).special(instrumentedType);
return stackManipulation.isValid()
? new RebasedMethodInvocation(resolvedMethod, instrumentedType, new Compound(additionalArguments, stackManipulation))
: Illegal.INSTANCE;
}
@Override
public MethodDescription getMethodDescription() {
return methodDescription;
}
@Override
public TypeDescription getTypeDescription() {
return instrumentedType;
}
@Override
public Size apply(MethodVisitor methodVisitor, Implementation.Context implementationContext) {
return stackManipulation.apply(methodVisitor, implementationContext);
}
@Override
public String toString() {
return "RebaseImplementationTarget.RebasedMethodInvocation{" +
"instrumentedType=" + instrumentedType +
", methodDescription=" + methodDescription +
", stackManipulation=" + stackManipulation +
'}';
}
}
/**
* A factory for creating a {@link RebaseImplementationTarget}.
*/
public static class Factory implements Implementation.Target.Factory {
/**
* The method rebase resolver to use.
*/
private final MethodRebaseResolver methodRebaseResolver;
/**
* Creates a new factory for a rebase implementation target.
*
* @param methodRebaseResolver The method rebase resolver to use.
*/
public Factory(MethodRebaseResolver methodRebaseResolver) {
this.methodRebaseResolver = methodRebaseResolver;
}
@Override
public Implementation.Target make(TypeDescription instrumentedType, MethodGraph.Linked methodGraph) {
return RebaseImplementationTarget.of(instrumentedType, methodGraph, methodRebaseResolver);
}
@Override
public boolean equals(Object other) {
return this == other || !(other == null || getClass() != other.getClass())
&& methodRebaseResolver.equals(((Factory) other).methodRebaseResolver);
}
@Override
public int hashCode() {
return methodRebaseResolver.hashCode();
}
@Override
public String toString() {
return "RebaseImplementationTarget.Factory{" +
"methodRebaseResolver=" + methodRebaseResolver +
'}';
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy