Added AbstractExtendedMockito.getClearInlineMethodsAtTheEnd().

Test: atest ModulesUtilsTestingTests:ExtendedMockitoRuleTest
Bug: 315196012

Change-Id: I8583fc74d1350511e00222164a42a2be0f4c4266
diff --git a/java/com/android/modules/utils/testing/AbstractExtendedMockitoRule.java b/java/com/android/modules/utils/testing/AbstractExtendedMockitoRule.java
index 594551c..1bb8364 100644
--- a/java/com/android/modules/utils/testing/AbstractExtendedMockitoRule.java
+++ b/java/com/android/modules/utils/testing/AbstractExtendedMockitoRule.java
@@ -133,8 +133,16 @@
         return Collections.unmodifiableSet(staticClasses);
     }
 
-
-
+    /**
+     * Gets whether the rule should clear the inline mocks after the given test.
+     *
+     * <p>By default, it returns {@code} (unless the rule was built with
+     * {@link AbstractBuilder#dontClearInlineMocks()}, but subclasses can override to change the
+     * behavior (for example, to decide based on custom annotations).
+     */
+    protected boolean getClearInlineMethodsAtTheEnd(Description description) {
+        return mClearInlineMocks;
+    }
 
     @Override
     public Statement apply(Statement base, Description description) {
@@ -247,12 +255,13 @@
                 }
             }
         } finally {
-            clearInlineMocks();
+            clearInlineMocks(description);
         }
     }
 
-    private void clearInlineMocks() {
-        if (!mClearInlineMocks) {
+    private void clearInlineMocks(Description description) {
+        boolean clearIt = getClearInlineMethodsAtTheEnd(description);
+        if (!clearIt) {
             Log.d(TAG, "NOT calling clearInlineMocks() as set on builder");
             return;
         }
diff --git a/javatests/com/android/modules/utils/testing/ExtendedMockitoRuleTest.java b/javatests/com/android/modules/utils/testing/ExtendedMockitoRuleTest.java
index c671e78..a8b0621 100644
--- a/javatests/com/android/modules/utils/testing/ExtendedMockitoRuleTest.java
+++ b/javatests/com/android/modules/utils/testing/ExtendedMockitoRuleTest.java
@@ -639,6 +639,16 @@
         assertWithMessage("mockito framework cleared").that(mockitoFramework.called).isTrue();
     }
 
+    @Test
+    public void testGetClearInlineMethodsAtTheEnd() throws Throwable {
+        assertWithMessage("getClearInlineMethodsAtTheEnd() by default")
+                .that(mBuilder.build().getClearInlineMethodsAtTheEnd(mDescription)).isTrue();
+        assertWithMessage("getClearInlineMethodsAtTheEnd() when built with dontClearInlineMocks()")
+                .that(mBuilder.dontClearInlineMocks().build()
+                        .getClearInlineMethodsAtTheEnd(mDescription))
+                .isFalse();
+    }
+
     private void applyRuleOnTestThatDoesntUseExpectation(@Nullable Strictness strictness)
             throws Throwable {
         Log.d(TAG, "applyRuleOnTestThatDoesntUseExpectation(): strictness= " + strictness);