org.testifyproject.bytebuddy.implementation.StubMethod Maven / Gradle / Ivy
The newest version!
package org.testifyproject.bytebuddy.implementation;
import org.testifyproject.bytebuddy.description.method.MethodDescription;
import org.testifyproject.bytebuddy.dynamic.scaffold.InstrumentedType;
import org.testifyproject.bytebuddy.implementation.bytecode.ByteCodeAppender;
import org.testifyproject.bytebuddy.implementation.bytecode.StackManipulation;
import org.testifyproject.bytebuddy.implementation.bytecode.constant.DefaultValue;
import org.testifyproject.bytebuddy.implementation.bytecode.member.MethodReturn;
import org.testifyproject.bytebuddy.jar.asm.MethodVisitor;
/**
* This implementation creates a method stub which does nothing but returning the default value of the return
* type of the method. These default values are:
*
* - The value {@code 0} for all numeric type.
* - The null character for the {@code char} type.
* - {@code false} for the {@code boolean} type.
* - Nothing for {@code void} types.
* - A {@code null} reference for any reference types. Note that this includes primitive wrapper types.
*
*/
public enum StubMethod implements Implementation, ByteCodeAppender {
/**
* The singleton instance.
*/
INSTANCE;
@Override
public InstrumentedType prepare(InstrumentedType instrumentedType) {
return instrumentedType;
}
@Override
public ByteCodeAppender appender(Target implementationTarget) {
return this;
}
@Override
public Size apply(MethodVisitor methodVisitor,
Context implementationContext,
MethodDescription instrumentedMethod) {
StackManipulation.Size stackSize = new StackManipulation.Compound(
DefaultValue.of(instrumentedMethod.getReturnType()),
MethodReturn.of(instrumentedMethod.getReturnType())
).apply(methodVisitor, implementationContext);
return new Size(stackSize.getMaximalSize(), instrumentedMethod.getStackSize());
}
}