From 64e246a22d1b6dfe58e37aae1ca69789c9e1949b Mon Sep 17 00:00:00 2001 From: Rafael Winterhalter Date: Sun, 30 Oct 2022 17:29:16 +0100 Subject: [PATCH] Avoids clearing stale weak entries from critical code segments. This reduces the propability of checking for mocked from from the mock-checking code what can lead to infinitive loops. --- .../creation/bytebuddy/InlineBytecodeGenerator.java | 7 +++++-- .../bytebuddy/InlineDelegateByteBuddyMockMaker.java | 10 +++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/mockito/internal/creation/bytebuddy/InlineBytecodeGenerator.java b/src/main/java/org/mockito/internal/creation/bytebuddy/InlineBytecodeGenerator.java index 72ab81adbd..ed12d2ffe2 100644 --- a/src/main/java/org/mockito/internal/creation/bytebuddy/InlineBytecodeGenerator.java +++ b/src/main/java/org/mockito/internal/creation/bytebuddy/InlineBytecodeGenerator.java @@ -94,8 +94,8 @@ public InlineBytecodeGenerator( .with(Implementation.Context.Disabled.Factory.INSTANCE) .with(MethodGraph.Compiler.ForDeclaredMethods.INSTANCE) .ignore(isSynthetic().and(not(isConstructor())).or(isDefaultFinalizer())); - mocked = new WeakConcurrentSet<>(WeakConcurrentSet.Cleaner.INLINE); - flatMocked = new WeakConcurrentSet<>(WeakConcurrentSet.Cleaner.INLINE); + mocked = new WeakConcurrentSet<>(WeakConcurrentSet.Cleaner.MANUAL); + flatMocked = new WeakConcurrentSet<>(WeakConcurrentSet.Cleaner.MANUAL); String identifier = RandomString.make(); subclassEngine = new TypeCachingBytecodeGenerator( @@ -299,6 +299,9 @@ private void triggerRetransformation(Set> types, boolean flat) { lastException = null; } } + + mocked.expungeStaleEntries(); + flatMocked.expungeStaleEntries(); } private void assureCanReadMockito(Set> types) { diff --git a/src/main/java/org/mockito/internal/creation/bytebuddy/InlineDelegateByteBuddyMockMaker.java b/src/main/java/org/mockito/internal/creation/bytebuddy/InlineDelegateByteBuddyMockMaker.java index 1a19a92732..05512b8409 100644 --- a/src/main/java/org/mockito/internal/creation/bytebuddy/InlineDelegateByteBuddyMockMaker.java +++ b/src/main/java/org/mockito/internal/creation/bytebuddy/InlineDelegateByteBuddyMockMaker.java @@ -192,13 +192,13 @@ class InlineDelegateByteBuddyMockMaker private final BytecodeGenerator bytecodeGenerator; private final WeakConcurrentMap mocks = - new WeakConcurrentMap.WithInlinedExpunction(); + new WeakConcurrentMap<>(false); private final DetachedThreadLocal, MockMethodInterceptor>> mockedStatics = - new DetachedThreadLocal<>(DetachedThreadLocal.Cleaner.INLINE); + new DetachedThreadLocal<>(DetachedThreadLocal.Cleaner.MANUAL); private final DetachedThreadLocal, BiConsumer>> - mockedConstruction = new DetachedThreadLocal<>(DetachedThreadLocal.Cleaner.INLINE); + mockedConstruction = new DetachedThreadLocal<>(DetachedThreadLocal.Cleaner.MANUAL); private final ThreadLocal mockitoConstruction = ThreadLocal.withInitial(() -> false); @@ -382,6 +382,7 @@ private T doCreateMock( if (instance instanceof MockAccess) { ((MockAccess) instance).setMockitoInterceptor(mockMethodInterceptor); } + mocks.expungeStaleEntries(); return instance; } catch (InstantiationException e) { throw new MockitoException( @@ -496,6 +497,7 @@ public void resetMock(Object mock, MockHandler newHandler, MockCreationSettings if (mock instanceof MockAccess) { ((MockAccess) mock).setMockitoInterceptor(mockMethodInterceptor); } + mocks.expungeStaleEntries(); } } @@ -570,6 +572,7 @@ public StaticMockControl createStaticMock( interceptors = new WeakHashMap<>(); mockedStatics.set(interceptors); } + mockedStatics.getBackingMap().expungeStaleEntries(); return new InlineStaticMockControl<>(type, interceptors, settings, handler); } @@ -598,6 +601,7 @@ public ConstructionMockControl createConstructionMock( interceptors = new WeakHashMap<>(); mockedConstruction.set(interceptors); } + mockedConstruction.getBackingMap().expungeStaleEntries(); return new InlineConstructionMockControl<>( type, settingsFactory, handlerFactory, mockInitializer, interceptors);