@Override public boolean test(T t) throws Exception { return clazz.isInstance(t); } }
/** Returns true if {@code annotations} contains an instance of {@code cls}. */ static boolean isAnnotationPresent(Annotation[] annotations, Class<? extends Annotation> cls) { for (Annotation annotation : annotations) { if (cls.isInstance(annotation)) { return true; } } return false; }
private synchronized Heavy createAndCacheHeavy() { class HeavyFactory implements Supplier<Heavy> { private final Heavy heavyInstance = new Heavy(); @Override public Heavy get() { return heavyInstance; } } if (!HeavyFactory.class.isInstance(heavy)) { heavy = new HeavyFactory(); } return heavy.get(); } }
@Override @SuppressWarnings("unchecked") public <T> T unwrap(Class<T> iface) throws SQLException { if (iface.isInstance(this)) { return (T) this; } throw new SQLException("DataSource of type [" + getClass().getName() + "] cannot be unwrapped as [" + iface.getName() + "]"); }
private static boolean isProxyOfSameInterfaces(Object arg, Class<?> proxyClass) { return proxyClass.isInstance(arg) // Equal proxy instances should mostly be instance of proxyClass // Under some edge cases (such as the proxy of JDK types serialized and then deserialized) // the proxy type may not be the same. // We first check isProxyClass() so that the common case of comparing with non-proxy objects // is efficient. || (Proxy.isProxyClass(arg.getClass()) && Arrays.equals(arg.getClass().getInterfaces(), proxyClass.getInterfaces())); } }
@Override public void registerResolvableDependency(Class<?> dependencyType, @Nullable Object autowiredValue) { Assert.notNull(dependencyType, "Dependency type must not be null"); if (autowiredValue != null) { if (!(autowiredValue instanceof ObjectFactory || dependencyType.isInstance(autowiredValue))) { throw new IllegalArgumentException("Value [" + autowiredValue + "] does not implement specified dependency type [" + dependencyType.getName() + "]"); } this.resolvableDependencies.put(dependencyType, autowiredValue); } }
@Override public <A extends Annotation> @Nullable A getAnnotation(Class<A> annotationType) { checkNotNull(annotationType); for (Annotation annotation : annotations) { if (annotationType.isInstance(annotation)) { return annotationType.cast(annotation); } } return null; }
@Nullable public static Object resolvePushBuilder(HttpServletRequest request, Class<?> paramType) { PushBuilder pushBuilder = request.newPushBuilder(); if (pushBuilder != null && !paramType.isInstance(pushBuilder)) { throw new IllegalStateException( "Current push builder is not of type [" + paramType.getName() + "]: " + pushBuilder); } return pushBuilder; } }
@Override @SuppressWarnings("unchecked") public <T> T getBean(String name, @Nullable Class<T> requiredType) throws BeansException { Object bean = getBean(name); if (requiredType != null && !requiredType.isInstance(bean)) { throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass()); } return (T) bean; }
public CrashDummy assertError(Class<? extends Throwable> clazz) { if (!clazz.isInstance(error)) { throw new AssertionError("Different error: " + error); } return this; }
public CrashDummy assertError(Class<? extends Throwable> clazz) { if (!clazz.isInstance(error)) { throw new AssertionError("Different error: " + error); } return this; }
@Override @SuppressWarnings("unchecked") public <T> T getNativeRequest(@Nullable Class<T> requiredType) { if (requiredType != null) { Object request = getExternalContext().getRequest(); if (requiredType.isInstance(request)) { return (T) request; } } return null; }
public static void assertError(List<Throwable> list, int index, Class<? extends Throwable> clazz) { Throwable ex = list.get(index); if (!clazz.isInstance(ex)) { AssertionError err = new AssertionError(clazz + " expected but got " + list.get(index)); err.initCause(list.get(index)); throw err; } }
public static void assertError(List<Throwable> list, int index, Class<? extends Throwable> clazz, String message) { Throwable ex = list.get(index); if (!clazz.isInstance(ex)) { AssertionError err = new AssertionError("Type " + clazz + " expected but got " + ex); err.initCause(ex); throw err; } if (!ObjectHelper.equals(message, ex.getMessage())) { AssertionError err = new AssertionError("Message " + message + " expected but got " + ex.getMessage()); err.initCause(ex); throw err; } }
@Override public boolean equals(@Nullable Object obj) { // In general getClass().isInstance() is bad for equals. // But here we fully control the subclasses to ensure symmetry. if (getClass().isInstance(obj)) { Wrapper that = (Wrapper) obj; return wrapped.equals(that.wrapped); } return false; }
public static void assertError(TestSubscriber<?> ts, int index, Class<? extends Throwable> clazz) { Throwable ex = ts.errors().get(0); if (ex instanceof CompositeException) { CompositeException ce = (CompositeException) ex; List<Throwable> cel = ce.getExceptions(); assertTrue(cel.get(index).toString(), clazz.isInstance(cel.get(index))); } else { fail(ex.toString() + ": not a CompositeException"); } }
public static void assertError(TestObserver<?> to, int index, Class<? extends Throwable> clazz, String message) { Throwable ex = to.errors().get(0); if (ex instanceof CompositeException) { CompositeException ce = (CompositeException) ex; List<Throwable> cel = ce.getExceptions(); assertTrue(cel.get(index).toString(), clazz.isInstance(cel.get(index))); assertEquals(message, cel.get(index).getMessage()); } else { fail(ex.toString() + ": not a CompositeException"); } }
public static void assertError(TestSubscriber<?> ts, int index, Class<? extends Throwable> clazz, String message) { Throwable ex = ts.errors().get(0); if (ex instanceof CompositeException) { CompositeException ce = (CompositeException) ex; List<Throwable> cel = ce.getExceptions(); assertTrue(cel.get(index).toString(), clazz.isInstance(cel.get(index))); assertEquals(message, cel.get(index).getMessage()); } else { fail(ex.toString() + ": not a CompositeException"); } }
public final void run() { try { realRun(); threadShouldThrow(exceptionClass.getSimpleName()); } catch (Throwable t) { if (!exceptionClass.isInstance(t)) threadUnexpectedException(t); } } }
public final void run() { try { realRun(); threadShouldThrow(exceptionClass.getSimpleName()); } catch (Throwable t) { if (!exceptionClass.isInstance(t)) threadUnexpectedException(t); } } }