Class TypeCachingBytecodeGenerator
- java.lang.Object
-
- java.lang.ref.ReferenceQueue<java.lang.ClassLoader>
-
- org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator
-
- All Implemented Interfaces:
BytecodeGenerator
class TypeCachingBytecodeGenerator extends java.lang.ref.ReferenceQueue<java.lang.ClassLoader> implements BytecodeGenerator
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
TypeCachingBytecodeGenerator.MockitoMockKey
private static class
TypeCachingBytecodeGenerator.TypeCachingLock
-
Field Summary
Fields Modifier and Type Field Description private BytecodeGenerator
bytecodeGenerator
private static int
CACHE_LOCK_MASK
The mask to use to mask out theTypeCachingBytecodeGenerator.MockitoMockKey.hashCode()
to find thecacheLocks
.private static int
CACHE_LOCK_SIZE
The size of thecacheLocks
.private TypeCachingBytecodeGenerator.TypeCachingLock[]
cacheLocks
This array containsTypeCachingBytecodeGenerator.TypeCachingLock
instances, which are used as java monitor locks forTypeCache.findOrInsert(ClassLoader, Object, Callable, Object)
.private java.util.concurrent.locks.ReadWriteLock
lock
private net.bytebuddy.TypeCache<TypeCachingBytecodeGenerator.MockitoMockKey>
typeCache
-
Constructor Summary
Constructors Constructor Description TypeCachingBytecodeGenerator(BytecodeGenerator bytecodeGenerator, boolean weak)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clearAllCaches()
private TypeCachingBytecodeGenerator.TypeCachingLock
getCacheLockForKey(TypeCachingBytecodeGenerator.MockitoMockKey key)
Returns aTypeCachingBytecodeGenerator.TypeCachingLock
, which locks theTypeCache.findOrInsert(ClassLoader, Object, Callable, Object)
.<T> java.lang.Class<T>
mockClass(MockFeatures<T> params)
void
mockClassConstruction(java.lang.Class<?> type)
void
mockClassStatic(java.lang.Class<?> type)
-
-
-
Field Detail
-
CACHE_LOCK_SIZE
private static final int CACHE_LOCK_SIZE
The size of thecacheLocks
.Caution: This must match the
CACHE_LOCK_MASK
.- See Also:
- Constant Field Values
-
CACHE_LOCK_MASK
private static final int CACHE_LOCK_MASK
The mask to use to mask out theTypeCachingBytecodeGenerator.MockitoMockKey.hashCode()
to find thecacheLocks
.Caution: this must match the bits of the
CACHE_LOCK_SIZE
.- See Also:
- Constant Field Values
-
bytecodeGenerator
private final BytecodeGenerator bytecodeGenerator
-
typeCache
private final net.bytebuddy.TypeCache<TypeCachingBytecodeGenerator.MockitoMockKey> typeCache
-
lock
private final java.util.concurrent.locks.ReadWriteLock lock
-
cacheLocks
private final TypeCachingBytecodeGenerator.TypeCachingLock[] cacheLocks
This array containsTypeCachingBytecodeGenerator.TypeCachingLock
instances, which are used as java monitor locks forTypeCache.findOrInsert(ClassLoader, Object, Callable, Object)
. The locks spread the lock to acquire over multiple locks instead of using a single lockBOOTSTRAP_LOCK
for allTypeCachingBytecodeGenerator.MockitoMockKey
.Note: We can't simply use the mockedType class lock as a lock, because the
TypeCachingBytecodeGenerator.MockitoMockKey
, will be the same for different mockTypes + interfaces.#3035: Excessive locking in TypeCachingBytecodeGenerator#BOOTSTRAP_LOCK
-
-
Constructor Detail
-
TypeCachingBytecodeGenerator
public TypeCachingBytecodeGenerator(BytecodeGenerator bytecodeGenerator, boolean weak)
-
-
Method Detail
-
mockClass
public <T> java.lang.Class<T> mockClass(MockFeatures<T> params)
- Specified by:
mockClass
in interfaceBytecodeGenerator
-
getCacheLockForKey
private TypeCachingBytecodeGenerator.TypeCachingLock getCacheLockForKey(TypeCachingBytecodeGenerator.MockitoMockKey key)
Returns aTypeCachingBytecodeGenerator.TypeCachingLock
, which locks theTypeCache.findOrInsert(ClassLoader, Object, Callable, Object)
.- Parameters:
key
- the key to lock- Returns:
- the
TypeCachingBytecodeGenerator.TypeCachingLock
to use to lock theTypeCache
-
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
-
clearAllCaches
public void clearAllCaches()
- Specified by:
clearAllCaches
in interfaceBytecodeGenerator
-
-