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