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.invocation;
7   
8   import edu.umd.cs.findbugs.annotations.NonNull;
9   import edu.umd.cs.findbugs.annotations.Nullable;
10  
11  import java.util.Iterator;
12  
13  class InvocationResult {
14      InvocationResult next;
15  
16      @Nullable
17      Object produceResult(@NonNull Object[] args) throws Throwable {
18          return null;
19      }
20  
21      @Nullable
22      Object produceResult(@Nullable Object invokedObject, @NonNull ExpectedInvocation invocation,
23              @NonNull InvocationConstraints constraints, @NonNull Object[] args) throws Throwable {
24          return produceResult(args);
25      }
26  
27      static final class ReturnValueResult extends InvocationResult {
28          @Nullable
29          private final Object returnValue;
30  
31          ReturnValueResult(@Nullable Object returnValue) {
32              this.returnValue = returnValue;
33          }
34  
35          @Nullable
36          @Override
37          Object produceResult(@NonNull Object[] args) {
38              return returnValue;
39          }
40      }
41  
42      static final class ThrowableResult extends InvocationResult {
43          @NonNull
44          private final Throwable throwable;
45  
46          ThrowableResult(@NonNull Throwable throwable) {
47              this.throwable = throwable;
48          }
49  
50          @NonNull
51          @Override
52          Object produceResult(@NonNull Object[] args) throws Throwable {
53              throwable.fillInStackTrace();
54              throw throwable;
55          }
56      }
57  
58      static final class DeferredResults extends InvocationResult {
59          @NonNull
60          private final Iterator<?> values;
61  
62          DeferredResults(@NonNull Iterator<?> values) {
63              this.values = values;
64          }
65  
66          @Nullable
67          @Override
68          Object produceResult(@NonNull Object[] args) throws Throwable {
69              Object nextValue = values.hasNext() ? values.next() : null;
70  
71              if (nextValue instanceof Throwable) {
72                  Throwable t = (Throwable) nextValue;
73                  t.fillInStackTrace();
74                  throw t;
75              }
76  
77              return nextValue;
78          }
79      }
80  }