1/*
2 * Copyright (c) 2007 Mockito contributors
3 * This program is made available under the terms of the MIT License.
4 */
5package org.mockito.internal.stubbing.answers;
6
7import java.lang.reflect.Method;
8import java.lang.reflect.Modifier;
9import org.mockito.internal.invocation.AbstractAwareMethod;
10import org.mockito.internal.util.Primitives;
11import org.mockito.invocation.InvocationOnMock;
12
13public class InvocationInfo implements AbstractAwareMethod {
14
15    private final Method method;
16
17    public InvocationInfo(InvocationOnMock theInvocation) {
18        this.method = theInvocation.getMethod();
19    }
20
21    public boolean isValidException(Throwable throwable) {
22        Class<?>[] exceptions = method.getExceptionTypes();
23        Class<?> throwableClass = throwable.getClass();
24        for (Class<?> exception : exceptions) {
25            if (exception.isAssignableFrom(throwableClass)) {
26                return true;
27            }
28        }
29
30        return false;
31    }
32
33    public boolean isValidReturnType(Class<?> clazz) {
34        if (method.getReturnType().isPrimitive() || clazz.isPrimitive()) {
35            return Primitives.primitiveTypeOf(clazz) == Primitives.primitiveTypeOf(method.getReturnType());
36        } else {
37            return method.getReturnType().isAssignableFrom(clazz);
38        }
39    }
40
41    /**
42     * Returns {@code true} is the return type is {@link Void} or represents the pseudo-type to the keyword {@code void}.
43     * E.g:  {@code void foo()} or {@code Void bar()}
44     */
45    public boolean isVoid() {
46        Class<?> returnType = this.method.getReturnType();
47        return returnType == Void.TYPE|| returnType == Void.class;
48    }
49
50    public String printMethodReturnType() {
51        return method.getReturnType().getSimpleName();
52    }
53
54    public String getMethodName() {
55        return method.getName();
56    }
57
58    public boolean returnsPrimitive() {
59        return method.getReturnType().isPrimitive();
60    }
61
62    public Method getMethod() {
63        return method;
64    }
65
66    public boolean isDeclaredOnInterface() {
67        return method.getDeclaringClass().isInterface();
68    }
69
70    @Override
71    public boolean isAbstract() {
72        return (method.getModifiers() & Modifier.ABSTRACT) != 0;
73    }
74}
75