Class InlineBytecodeGenerator
java.lang.Object
org.mockito.internal.creation.bytebuddy.InlineBytecodeGenerator
- All Implemented Interfaces:
ClassFileTransformer
,BytecodeGenerator
@SuppressSignatureCheck
public class InlineBytecodeGenerator
extends Object
implements BytecodeGenerator, ClassFileTransformer
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static class
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final net.bytebuddy.ByteBuddy
private final Method
private final WeakConcurrentSet
<Class<?>> private final Method
private final Instrumentation
private Throwable
private final WeakConcurrentSet
<Class<?>> private final net.bytebuddy.asm.AsmVisitorWrapper
private static final String
private final Method
private final BytecodeGenerator
-
Constructor Summary
ConstructorsConstructorDescriptionInlineBytecodeGenerator
(Instrumentation instrumentation, WeakConcurrentMap<Object, MockMethodInterceptor> mocks, DetachedThreadLocal<Map<Class<?>, MockMethodInterceptor>> mockedStatics, Predicate<Class<?>> isMockConstruction, ConstructionCallback onConstruction) -
Method Summary
Modifier and TypeMethodDescriptionprivate void
addInterfaces
(Set<Class<?>> types, Class<?>[] interfaces) private void
assureCanReadMockito
(Set<Class<?>> types) private static void
assureInitialization
(Class<?> type) private <T> void
checkSupportedCombination
(boolean subclassingRequired, MockFeatures<T> features) void
<T> Class
<? extends T> mockClass
(MockFeatures<T> features) void
mockClassConstruction
(Class<?> type) void
mockClassStatic
(Class<?> type) private static void
preload()
Mockito allows to mock about any type, including such types that we are relying on ourselves.byte[]
transform
(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) private <T> void
triggerRetransformation
(Set<Class<?>> types, boolean flat)
-
Field Details
-
PRELOAD
- See Also:
-
EXCLUDES
-
instrumentation
-
byteBuddy
private final net.bytebuddy.ByteBuddy byteBuddy -
mocked
-
flatMocked
-
subclassEngine
-
mockTransformer
private final net.bytebuddy.asm.AsmVisitorWrapper mockTransformer -
getModule
-
canRead
-
redefineModule
-
lastException
-
-
Constructor Details
-
InlineBytecodeGenerator
public InlineBytecodeGenerator(Instrumentation instrumentation, WeakConcurrentMap<Object, MockMethodInterceptor> mocks, DetachedThreadLocal<Map<Class<?>, MockMethodInterceptor>> mockedStatics, Predicate<Class<?>> isMockConstruction, ConstructionCallback onConstruction)
-
-
Method Details
-
preload
private static void preload()Mockito allows to mock about any type, including such types that we are relying on ourselves. This can cause a circularity: In order to check if an instance is a mock we need to look up if this instance is registered in themocked
set. But to look up this instance, we need to create key instances that rely on weak reference properties. Loading the later classes will happen before the key instances are completed what will cause Mockito to check if those key instances are themselves mocks what causes a loop which results in a circularity error. This is not normally a problem as we explicitly check if the instance that we investigate is one of our instance of which we hold a reference by reference equality what does not cause any code execution. But it seems like the load order plays a role here with unloaded types being loaded before we even get to check the mock instance property. To avoid this, we are making sure that crucuial JVM types are loaded before we create the first inline mock. Unfortunately, these types dependant on a JVM's implementation and we can only maintain types that we know of from well-known JVM implementations such as HotSpot and extend this list once we learn of further problematic types for future Java versions. To allow users to whitelist their own types, we do not also offer a property that allows running problematic tests before a new Mockito version can be released and that allows us to ask users to easily validate that whitelisting actually solves a problem as circularities could also be caused by other problems. -
mockClass
- Specified by:
mockClass
in interfaceBytecodeGenerator
-
mockClassStatic
- Specified by:
mockClassStatic
in interfaceBytecodeGenerator
-
mockClassConstruction
- Specified by:
mockClassConstruction
in interfaceBytecodeGenerator
-
assureInitialization
-
triggerRetransformation
-
assureCanReadMockito
-
checkSupportedCombination
-
addInterfaces
-
transform
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) - Specified by:
transform
in interfaceClassFileTransformer
-
clearAllCaches
public void clearAllCaches()- Specified by:
clearAllCaches
in interfaceBytecodeGenerator
-