Home | History | Annotate | Download | only in answers
      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 org.mockito.internal.util.Primitives;
      8 import org.mockito.invocation.Invocation;
      9 
     10 import java.lang.reflect.Method;
     11 
     12 /**
     13  * by Szczepan Faber, created at: 3/31/12
     14  */
     15 public class MethodInfo {
     16 
     17     private Method method;
     18 
     19     public MethodInfo(Invocation theInvocation) {
     20         this.method = theInvocation.getMethod();
     21     }
     22 
     23     public boolean isValidException(Throwable throwable) {
     24         Class<?>[] exceptions = method.getExceptionTypes();
     25         Class<?> throwableClass = throwable.getClass();
     26         for (Class<?> exception : exceptions) {
     27             if (exception.isAssignableFrom(throwableClass)) {
     28                 return true;
     29             }
     30         }
     31 
     32         return false;
     33     }
     34 
     35     public boolean isValidReturnType(Class clazz) {
     36         if (method.getReturnType().isPrimitive() || clazz.isPrimitive()) {
     37             return Primitives.primitiveTypeOf(clazz) == Primitives.primitiveTypeOf(method.getReturnType());
     38         } else {
     39             return method.getReturnType().isAssignableFrom(clazz);
     40         }
     41     }
     42 
     43     public boolean isVoid() {
     44         return this.method.getReturnType() == Void.TYPE;
     45     }
     46 
     47     public String printMethodReturnType() {
     48         return method.getReturnType().getSimpleName();
     49     }
     50 
     51     public String getMethodName() {
     52         return method.getName();
     53     }
     54 
     55     public boolean returnsPrimitive() {
     56         return method.getReturnType().isPrimitive();
     57     }
     58 
     59     public Method getMethod() {
     60         return method;
     61     }
     62 
     63     public boolean isDeclaredOnInterface() {
     64         return method.getDeclaringClass().isInterface();
     65     }
     66 }
     67