本文整理了Java中java.lang.Class.isInstance()
方法的一些代码示例,展示了Class.isInstance()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Class.isInstance()
方法的具体详情如下:
包路径:java.lang.Class
类名称:Class
方法名:isInstance
[英]Tests whether the given object can be cast to the class represented by this Class. This is the runtime version of the instanceof operator.
[中]测试给定对象是否可以强制转换到此类表示的类。这是instanceof运算符的运行时版本。
代码示例来源:origin: ReactiveX/RxJava
@Override
public boolean test(T t) throws Exception {
return clazz.isInstance(t);
}
}
代码示例来源:origin: square/retrofit
/** 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;
}
代码示例来源:origin: iluwatar/java-design-patterns
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();
}
}
代码示例来源:origin: spring-projects/spring-framework
@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() + "]");
}
代码示例来源:origin: google/guava
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()));
}
}
代码示例来源:origin: spring-projects/spring-framework
@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);
}
}
代码示例来源:origin: google/guava
@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;
}
代码示例来源:origin: spring-projects/spring-framework
@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;
}
}
代码示例来源:origin: spring-projects/spring-framework
@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;
}
代码示例来源:origin: ReactiveX/RxJava
public CrashDummy assertError(Class<? extends Throwable> clazz) {
if (!clazz.isInstance(error)) {
throw new AssertionError("Different error: " + error);
}
return this;
}
代码示例来源:origin: ReactiveX/RxJava
public CrashDummy assertError(Class<? extends Throwable> clazz) {
if (!clazz.isInstance(error)) {
throw new AssertionError("Different error: " + error);
}
return this;
}
代码示例来源:origin: spring-projects/spring-framework
@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;
}
代码示例来源:origin: ReactiveX/RxJava
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;
}
}
代码示例来源:origin: ReactiveX/RxJava
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;
}
}
代码示例来源:origin: google/guava
@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;
}
代码示例来源:origin: ReactiveX/RxJava
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");
}
}
代码示例来源:origin: ReactiveX/RxJava
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");
}
}
代码示例来源:origin: ReactiveX/RxJava
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");
}
}
代码示例来源:origin: google/guava
public final void run() {
try {
realRun();
threadShouldThrow(exceptionClass.getSimpleName());
} catch (Throwable t) {
if (!exceptionClass.isInstance(t)) threadUnexpectedException(t);
}
}
}
代码示例来源:origin: google/guava
public final void run() {
try {
realRun();
threadShouldThrow(exceptionClass.getSimpleName());
} catch (Throwable t) {
if (!exceptionClass.isInstance(t)) threadUnexpectedException(t);
}
}
}
内容来源于网络,如有侵权,请联系作者删除!