• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2007 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 package org.mockito.internal.stubbing.answers;
6 
7 import java.lang.reflect.Method;
8 import java.lang.reflect.Modifier;
9 import org.mockito.internal.invocation.AbstractAwareMethod;
10 import org.mockito.internal.util.Primitives;
11 import org.mockito.invocation.InvocationOnMock;
12 
13 public class InvocationInfo implements AbstractAwareMethod {
14 
15     private final Method method;
16 
InvocationInfo(InvocationOnMock theInvocation)17     public InvocationInfo(InvocationOnMock theInvocation) {
18         this.method = theInvocation.getMethod();
19     }
20 
isValidException(Throwable throwable)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 
isValidReturnType(Class<?> clazz)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      */
isVoid()45     public boolean isVoid() {
46         Class<?> returnType = this.method.getReturnType();
47         return returnType == Void.TYPE|| returnType == Void.class;
48     }
49 
printMethodReturnType()50     public String printMethodReturnType() {
51         return method.getReturnType().getSimpleName();
52     }
53 
getMethodName()54     public String getMethodName() {
55         return method.getName();
56     }
57 
returnsPrimitive()58     public boolean returnsPrimitive() {
59         return method.getReturnType().isPrimitive();
60     }
61 
getMethod()62     public Method getMethod() {
63         return method;
64     }
65 
isDeclaredOnInterface()66     public boolean isDeclaredOnInterface() {
67         return method.getDeclaringClass().isInterface();
68     }
69 
70     @Override
isAbstract()71     public boolean isAbstract() {
72         return (method.getModifiers() & Modifier.ABSTRACT) != 0;
73     }
74 }
75