/
InvocationInfo.java
127 lines (105 loc) · 4.2 KB
/
InvocationInfo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.stubbing.answers;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import org.mockito.internal.invocation.AbstractAwareMethod;
import org.mockito.internal.util.MockUtil;
import org.mockito.internal.util.Primitives;
import org.mockito.internal.util.reflection.GenericMetadataSupport;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.mock.MockCreationSettings;
public class InvocationInfo implements AbstractAwareMethod {
private final Method method;
private final InvocationOnMock invocation;
public InvocationInfo(InvocationOnMock theInvocation) {
this.method = theInvocation.getMethod();
this.invocation = theInvocation;
}
public boolean isValidException(final Throwable throwable) {
if (isValidException(method, throwable)) {
return true;
}
return isValidExceptionForParents(method.getDeclaringClass(), throwable);
}
private boolean isValidExceptionForParents(final Class<?> parent, final Throwable throwable) {
final List<Class<?>> ancestors = new ArrayList<>(Arrays.asList(parent.getInterfaces()));
if (parent.getSuperclass() != null) {
ancestors.add(parent.getSuperclass());
}
final boolean validException =
ancestors.stream()
.anyMatch(ancestor -> isValidExceptionForClass(ancestor, throwable));
if (validException) {
return true;
}
return ancestors.stream()
.anyMatch(ancestor -> isValidExceptionForParents(ancestor, throwable));
}
private boolean isValidExceptionForClass(final Class<?> parent, final Throwable throwable) {
try {
final Method parentMethod =
parent.getMethod(this.method.getName(), this.method.getParameterTypes());
return isValidException(parentMethod, throwable);
} catch (NoSuchMethodException e) {
// ignore interfaces that doesn't have such a method
return false;
}
}
private boolean isValidException(final Method method, final Throwable throwable) {
final Class<?>[] exceptions = method.getExceptionTypes();
final Class<?> throwableClass = throwable.getClass();
for (final Class<?> exception : exceptions) {
if (exception.isAssignableFrom(throwableClass)) {
return true;
}
}
return false;
}
public boolean isValidReturnType(Class<?> clazz) {
if (method.getReturnType().isPrimitive() || clazz.isPrimitive()) {
return Primitives.primitiveTypeOf(clazz)
== Primitives.primitiveTypeOf(method.getReturnType());
} else {
return method.getReturnType().isAssignableFrom(clazz);
}
}
/**
* Returns {@code true} is the return type is {@link Void} or represents the pseudo-type to the keyword {@code void}.
* E.g: {@code void foo()} or {@code Void bar()}
*/
public boolean isVoid() {
final MockCreationSettings mockSettings =
MockUtil.getMockHandler(invocation.getMock()).getMockSettings();
Class<?> returnType =
GenericMetadataSupport.inferFrom(mockSettings.getTypeToMock())
.resolveGenericReturnType(this.method)
.rawType();
return returnType == Void.TYPE || returnType == Void.class;
}
public String printMethodReturnType() {
return method.getReturnType().getSimpleName();
}
public String getMethodName() {
return method.getName();
}
public boolean returnsPrimitive() {
return method.getReturnType().isPrimitive();
}
public Method getMethod() {
return method;
}
public boolean isDeclaredOnInterface() {
return method.getDeclaringClass().isInterface();
}
@Override
public boolean isAbstract() {
return (method.getModifiers() & Modifier.ABSTRACT) != 0;
}
}