View Javadoc
1   /*
2    * MIT License
3    * Copyright (c) 2006-2025 JMockit developers
4    * See LICENSE file for full license text.
5    */
6   package mockit.internal.expectations.mocking;
7   
8   import static mockit.internal.expectations.RecordAndReplayExecution.RECORD_OR_REPLAY_LOCK;
9   import static mockit.internal.expectations.RecordAndReplayExecution.recordOrReplay;
10  
11  import edu.umd.cs.findbugs.annotations.NonNull;
12  import edu.umd.cs.findbugs.annotations.Nullable;
13  
14  import java.lang.reflect.Method;
15  
16  import mockit.internal.ClassLoadingBridge;
17  import mockit.internal.expectations.ExecutionMode;
18  import mockit.internal.state.TestRun;
19  import mockit.internal.util.ObjectMethods;
20  
21  public final class MockedBridge extends ClassLoadingBridge {
22      @NonNull
23      public static final ClassLoadingBridge MB = new MockedBridge();
24  
25      private MockedBridge() {
26          super("$MB");
27      }
28  
29      @Nullable
30      @Override
31      public Object invoke(@Nullable Object mocked, Method method, @NonNull Object[] args) throws Throwable {
32          String mockedClassDesc = (String) args[1];
33  
34          if (notToBeMocked(mocked, mockedClassDesc)) {
35              return Void.class;
36          }
37  
38          String mockName = (String) args[2];
39          String mockDesc = (String) args[3];
40          String mockNameAndDesc = mockName + mockDesc;
41          Integer executionMode = (Integer) args[5];
42          Object[] mockArgs = extractArguments(6, args);
43  
44          boolean regularExecutionWithRecordReplayLock = executionMode == ExecutionMode.Regular.ordinal()
45                  && RECORD_OR_REPLAY_LOCK.isHeldByCurrentThread();
46          Object rv;
47  
48          if (regularExecutionWithRecordReplayLock && mocked != null) {
49              rv = ObjectMethods.evaluateOverride(mocked, mockNameAndDesc, args);
50  
51              if (rv != null) {
52                  return rv;
53              }
54          }
55  
56          if (TestRun.getExecutingTest().isProceedingIntoRealImplementation() || regularExecutionWithRecordReplayLock
57                  || TestRun.isInsideNoMockingZone()) {
58              return Void.class;
59          }
60  
61          TestRun.enterNoMockingZone();
62  
63          try {
64              int mockAccess = (Integer) args[0];
65              String genericSignature = (String) args[4];
66              rv = recordOrReplay(mocked, mockAccess, mockedClassDesc, mockNameAndDesc, genericSignature, executionMode,
67                      mockArgs);
68          } finally {
69              TestRun.exitNoMockingZone();
70          }
71  
72          return rv;
73      }
74  }