Home | History | Annotate | Download | only in matchers
      1 /*
      2  * Copyright (c) 2007 Mockito contributors
      3  * This program is made available under the terms of the MIT License.
      4  */
      5 
      6 package org.mockito.internal.matchers;
      7 
      8 import org.mockito.ArgumentMatcher;
      9 import org.mockito.internal.util.Primitives;
     10 
     11 import java.io.Serializable;
     12 
     13 
     14 public class InstanceOf implements ArgumentMatcher<Object>, Serializable {
     15 
     16     private final Class<?> clazz;
     17     private String description;
     18 
     19     public InstanceOf(Class<?> clazz) {
     20         this(clazz, "isA(" + clazz.getCanonicalName() + ")");
     21     }
     22 
     23     public InstanceOf(Class<?> clazz, String describedAs) {
     24         this.clazz = clazz;
     25         this.description = describedAs;
     26     }
     27 
     28     public boolean matches(Object actual) {
     29         return (actual != null) &&
     30                 (Primitives.isAssignableFromWrapper(actual.getClass(), clazz)
     31                         || clazz.isAssignableFrom(actual.getClass()));
     32     }
     33 
     34     public String toString() {
     35         return description;
     36     }
     37 
     38     public static class VarArgAware extends InstanceOf implements VarargMatcher {
     39 
     40         public VarArgAware(Class<?> clazz) {
     41             super(clazz);
     42         }
     43 
     44         public VarArgAware(Class<?> clazz, String describedAs) {
     45             super(clazz, describedAs);
     46         }
     47     }
     48 
     49 
     50 }
     51