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;
7   
8   import static mockit.internal.expectations.state.ExecutingTest.isInstanceMethodWithStandardBehavior;
9   
10  import edu.umd.cs.findbugs.annotations.NonNull;
11  import edu.umd.cs.findbugs.annotations.Nullable;
12  
13  import java.util.Map;
14  
15  import mockit.internal.expectations.invocation.ExpectedInvocation;
16  
17  public final class RecordPhase extends TestOnlyPhase {
18      RecordPhase(@NonNull PhasedExecutionState executionState) {
19          super(executionState);
20      }
21  
22      void addResult(@Nullable Object result) {
23          if (currentExpectation != null) {
24              currentExpectation.addResult(result);
25          }
26      }
27  
28      public void addSequenceOfReturnValues(@NonNull Object[] values) {
29          if (currentExpectation != null) {
30              currentExpectation.addSequenceOfReturnValues(values);
31          }
32      }
33  
34      @Nullable
35      @Override
36      Object handleInvocation(@Nullable Object mock, int mockAccess, @NonNull String mockClassDesc,
37              @NonNull String mockNameAndDesc, @Nullable String genericSignature, boolean withRealImpl,
38              @NonNull Object[] args) {
39          mock = configureMatchingOnMockInstanceIfSpecified(mock);
40  
41          ExpectedInvocation invocation = new ExpectedInvocation(mock, mockAccess, mockClassDesc, mockNameAndDesc,
42                  matchInstance, genericSignature, args);
43  
44          boolean nonStrictInvocation = false;
45  
46          if (!matchInstance && invocation.isConstructor()) {
47              Map<Object, Object> replacementMap = getReplacementMap();
48              replacementMap.put(mock, mock);
49          } else {
50              nonStrictInvocation = isInstanceMethodWithStandardBehavior(mock, mockNameAndDesc);
51          }
52  
53          currentExpectation = new Expectation(this, invocation, nonStrictInvocation);
54  
55          if (argMatchers != null) {
56              invocation.arguments.setMatchers(argMatchers);
57              argMatchers = null;
58          }
59  
60          executionState.addExpectation(currentExpectation);
61  
62          return invocation.getDefaultValueForReturnType();
63      }
64  
65      @Nullable
66      private Object configureMatchingOnMockInstanceIfSpecified(@Nullable Object mock) {
67          matchInstance = false;
68  
69          if (mock == null) {
70              return null;
71          }
72  
73          Map<Object, Object> replacementMap = getReplacementMap();
74          Object replacementInstance = replacementMap.get(mock);
75          matchInstance = mock == replacementInstance || isEnumElement(mock);
76          return mock;
77      }
78  
79      @Override
80      void handleInvocationCountConstraint(int minInvocations, int maxInvocations) {
81          if (currentExpectation != null) {
82              currentExpectation.constraints.setLimits(minInvocations, maxInvocations);
83          }
84      }
85  }