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