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 classTypeCachingBytecodeGenerator.MockitoMockKeyprivate static classTypeCachingBytecodeGenerator.TypeCachingLock
-
Field Summary
Fields Modifier and Type Field Description private BytecodeGeneratorbytecodeGeneratorprivate static intCACHE_LOCK_MASKThe mask to use to mask out theTypeCachingBytecodeGenerator.MockitoMockKey.hashCode()to find thecacheLocks.private static intCACHE_LOCK_SIZEThe size of thecacheLocks.private TypeCachingBytecodeGenerator.TypeCachingLock[]cacheLocksThis array containsTypeCachingBytecodeGenerator.TypeCachingLockinstances, which are used as java monitor locks forTypeCache.findOrInsert(ClassLoader, Object, Callable, Object).private java.util.concurrent.locks.ReadWriteLocklockprivate 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 voidclearAllCaches()private TypeCachingBytecodeGenerator.TypeCachingLockgetCacheLockForKey(TypeCachingBytecodeGenerator.MockitoMockKey key)Returns aTypeCachingBytecodeGenerator.TypeCachingLock, which locks theTypeCache.findOrInsert(ClassLoader, Object, Callable, Object).<T> java.lang.Class<T>mockClass(MockFeatures<T> params)voidmockClassConstruction(java.lang.Class<?> type)voidmockClassStatic(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.TypeCachingLockinstances, 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_LOCKfor 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:
mockClassin 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.TypeCachingLockto use to lock theTypeCache
-
mockClassStatic
public void mockClassStatic(java.lang.Class<?> type)
- Specified by:
mockClassStaticin interfaceBytecodeGenerator
-
mockClassConstruction
public void mockClassConstruction(java.lang.Class<?> type)
- Specified by:
mockClassConstructionin interfaceBytecodeGenerator
-
clearAllCaches
public void clearAllCaches()
- Specified by:
clearAllCachesin interfaceBytecodeGenerator
-
-