Class InlineBytecodeGenerator
- java.lang.Object
-
- org.mockito.internal.creation.bytebuddy.InlineBytecodeGenerator
-
- All Implemented Interfaces:
java.lang.instrument.ClassFileTransformer
,BytecodeGenerator
@SuppressSignatureCheck public class InlineBytecodeGenerator extends java.lang.Object implements BytecodeGenerator, java.lang.instrument.ClassFileTransformer
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
InlineBytecodeGenerator.ParameterWritingVisitorWrapper
-
Field Summary
Fields Modifier and Type Field Description private net.bytebuddy.ByteBuddy
byteBuddy
private java.lang.reflect.Method
canRead
(package private) static java.util.Set<java.lang.Class<?>>
EXCLUDES
private WeakConcurrentSet<java.lang.Class<?>>
flatMocked
private java.lang.reflect.Method
getModule
private java.lang.instrument.Instrumentation
instrumentation
private java.lang.Throwable
lastException
private WeakConcurrentSet<java.lang.Class<?>>
mocked
private net.bytebuddy.asm.AsmVisitorWrapper
mockTransformer
private static java.lang.String
PRELOAD
private java.lang.reflect.Method
redefineModule
private BytecodeGenerator
subclassEngine
-
Constructor Summary
Constructors Constructor Description InlineBytecodeGenerator(java.lang.instrument.Instrumentation instrumentation, WeakConcurrentMap<java.lang.Object,MockMethodInterceptor> mocks, DetachedThreadLocal<java.util.Map<java.lang.Class<?>,MockMethodInterceptor>> mockedStatics, java.util.function.Predicate<java.lang.Class<?>> isMockConstruction, ConstructionCallback onConstruction)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
addInterfaces(java.util.Set<java.lang.Class<?>> types, java.lang.Class<?>[] interfaces)
private void
assureCanReadMockito(java.util.Set<java.lang.Class<?>> types)
private static void
assureInitialization(java.lang.Class<?> type)
private <T> void
checkSupportedCombination(boolean subclassingRequired, MockFeatures<T> features)
void
clearAllCaches()
<T> java.lang.Class<? extends T>
mockClass(MockFeatures<T> features)
void
mockClassConstruction(java.lang.Class<?> type)
void
mockClassStatic(java.lang.Class<?> type)
private static void
preload()
Mockito allows to mock about any type, including such types that we are relying on ourselves.byte[]
transform(java.lang.ClassLoader loader, java.lang.String className, java.lang.Class<?> classBeingRedefined, java.security.ProtectionDomain protectionDomain, byte[] classfileBuffer)
private <T> void
triggerRetransformation(java.util.Set<java.lang.Class<?>> types, boolean flat)
-
-
-
Field Detail
-
PRELOAD
private static final java.lang.String PRELOAD
- See Also:
- Constant Field Values
-
EXCLUDES
static final java.util.Set<java.lang.Class<?>> EXCLUDES
-
instrumentation
private final java.lang.instrument.Instrumentation instrumentation
-
byteBuddy
private final net.bytebuddy.ByteBuddy byteBuddy
-
mocked
private final WeakConcurrentSet<java.lang.Class<?>> mocked
-
flatMocked
private final WeakConcurrentSet<java.lang.Class<?>> flatMocked
-
subclassEngine
private final BytecodeGenerator subclassEngine
-
mockTransformer
private final net.bytebuddy.asm.AsmVisitorWrapper mockTransformer
-
getModule
private final java.lang.reflect.Method getModule
-
canRead
private final java.lang.reflect.Method canRead
-
redefineModule
private final java.lang.reflect.Method redefineModule
-
lastException
private volatile java.lang.Throwable lastException
-
-
Constructor Detail
-
InlineBytecodeGenerator
public InlineBytecodeGenerator(java.lang.instrument.Instrumentation instrumentation, WeakConcurrentMap<java.lang.Object,MockMethodInterceptor> mocks, DetachedThreadLocal<java.util.Map<java.lang.Class<?>,MockMethodInterceptor>> mockedStatics, java.util.function.Predicate<java.lang.Class<?>> isMockConstruction, ConstructionCallback onConstruction)
-
-
Method Detail
-
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
public <T> java.lang.Class<? extends T> mockClass(MockFeatures<T> features)
- Specified by:
mockClass
in interfaceBytecodeGenerator
-
mockClassStatic
public void mockClassStatic(java.lang.Class<?> type)
- Specified by:
mockClassStatic
in interfaceBytecodeGenerator
-
mockClassConstruction
public void mockClassConstruction(java.lang.Class<?> type)
- Specified by:
mockClassConstruction
in interfaceBytecodeGenerator
-
assureInitialization
private static void assureInitialization(java.lang.Class<?> type)
-
triggerRetransformation
private <T> void triggerRetransformation(java.util.Set<java.lang.Class<?>> types, boolean flat)
-
assureCanReadMockito
private void assureCanReadMockito(java.util.Set<java.lang.Class<?>> types)
-
checkSupportedCombination
private <T> void checkSupportedCombination(boolean subclassingRequired, MockFeatures<T> features)
-
addInterfaces
private void addInterfaces(java.util.Set<java.lang.Class<?>> types, java.lang.Class<?>[] interfaces)
-
transform
public byte[] transform(java.lang.ClassLoader loader, java.lang.String className, java.lang.Class<?> classBeingRedefined, java.security.ProtectionDomain protectionDomain, byte[] classfileBuffer)
- Specified by:
transform
in interfacejava.lang.instrument.ClassFileTransformer
-
clearAllCaches
public void clearAllCaches()
- Specified by:
clearAllCaches
in interfaceBytecodeGenerator
-
-