java.lang.Class.isAssignableFrom()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(7.4k)|赞(0)|评价(0)|浏览(295)

本文整理了Java中java.lang.Class.isAssignableFrom()方法的一些代码示例,展示了Class.isAssignableFrom()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Class.isAssignableFrom()方法的具体详情如下:
包路径:java.lang.Class
类名称:Class
方法名:isAssignableFrom

Class.isAssignableFrom介绍

[英]Tests whether the given class type can be converted to the class represented by this Class. Conversion may be done via an identity conversion or a widening reference conversion (if either the receiver or the argument represent primitive types, only the identity conversion applies).
[中]测试给定的类类型是否可以转换为此类表示的类。转换可以通过标识转换或加宽引用转换完成(如果接收方或参数表示基元类型,则仅应用标识转换)。

代码示例

代码示例来源:origin: spring-projects/spring-framework

private boolean isSupportedType(@Nullable Class<?> clazz) {
  return (clazz != null && ((HttpEntity.class.isAssignableFrom(clazz) &&
      !RequestEntity.class.isAssignableFrom(clazz)) ||
      HttpHeaders.class.isAssignableFrom(clazz)));
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public boolean matches(Method method, Class<?> targetClass) {
  if (TransactionalProxy.class.isAssignableFrom(targetClass) ||
      PlatformTransactionManager.class.isAssignableFrom(targetClass) ||
      PersistenceExceptionTranslator.class.isAssignableFrom(targetClass)) {
    return false;
  }
  TransactionAttributeSource tas = getTransactionAttributeSource();
  return (tas == null || tas.getTransactionAttribute(method, targetClass) != null);
}

代码示例来源:origin: square/retrofit

@Override public @Nullable Converter<?, RequestBody> requestBodyConverter(Type type,
   Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
  if (!(type instanceof Class<?>)) {
   return null;
  }
  if (!MessageLite.class.isAssignableFrom((Class<?>) type)) {
   return null;
  }
  return new ProtoRequestBodyConverter<>();
 }
}

代码示例来源:origin: square/retrofit

@Override public @Nullable Converter<?, RequestBody> requestBodyConverter(Type type,
  Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
 if (RequestBody.class.isAssignableFrom(Utils.getRawType(type))) {
  return RequestBodyConverter.INSTANCE;
 }
 return null;
}

代码示例来源:origin: google/guava

private boolean someRawTypeIsSubclassOf(Class<?> superclass) {
 for (Class<?> rawType : getRawTypes()) {
  if (superclass.isAssignableFrom(rawType)) {
   return true;
  }
 }
 return false;
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public boolean isTypeMatch(String name, @Nullable Class<?> typeToMatch) throws NoSuchBeanDefinitionException {
  Class<?> type = getType(name);
  return (typeToMatch == null || (type != null && typeToMatch.isAssignableFrom(type)));
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Is this type a {@link Collection} type?
 */
public boolean isCollection() {
  return Collection.class.isAssignableFrom(getType());
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
  Class<?> sourceClass = sourceType.getObjectType();
  if (String.class == sourceClass) {
    // no conversion required
    return false;
  }
  return (CharSequence.class.isAssignableFrom(sourceClass) ||
      StringWriter.class.isAssignableFrom(sourceClass) ||
      ObjectToObjectConverter.hasConversionMethodOrConstructor(sourceClass, String.class));
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public boolean isTypeMatch(String name, @Nullable Class<?> typeToMatch) throws NoSuchBeanDefinitionException {
  Class<?> type = getType(name);
  return (typeToMatch == null || (type != null && typeToMatch.isAssignableFrom(type)));
}

代码示例来源:origin: square/retrofit

/**
 * Returns the generic form of {@code supertype}. For example, if this is {@code
 * ArrayList<String>}, this returns {@code Iterable<String>} given the input {@code
 * Iterable.class}.
 *
 * @param supertype a superclass of, or interface implemented by, this.
 */
static Type getSupertype(Type context, Class<?> contextRawType, Class<?> supertype) {
 if (!supertype.isAssignableFrom(contextRawType)) throw new IllegalArgumentException();
 return resolve(context, contextRawType,
   getGenericSupertype(context, contextRawType, supertype));
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public boolean supportsParameter(MethodParameter parameter) {
  return checkAnnotatedParamNoReactiveWrapper(parameter, MatrixVariable.class,
      (ann, type) -> !Map.class.isAssignableFrom(type) || StringUtils.hasText(ann.name()));
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public boolean canDecode(ResolvableType elementType, @Nullable MimeType mimeType) {
  return (Resource.class.isAssignableFrom(elementType.toClass()) &&
      super.canDecode(elementType, mimeType));
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public boolean canEncode(ResolvableType elementType, @Nullable MimeType mimeType) {
  return super.canEncode(elementType, mimeType)
      && ResourceRegion.class.isAssignableFrom(elementType.toClass());
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public boolean matches(Method method, Class<?> targetClass) {
  if (CacheManager.class.isAssignableFrom(targetClass)) {
    return false;
  }
  CacheOperationSource cas = getCacheOperationSource();
  return (cas != null && !CollectionUtils.isEmpty(cas.getCacheOperations(method, targetClass)));
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public Object resolveArgumentValue(
    MethodParameter methodParameter, BindingContext context, ServerWebExchange exchange) {
  boolean isMultiValueMap = MultiValueMap.class.isAssignableFrom(methodParameter.getParameterType());
  HttpHeaders headers = exchange.getRequest().getHeaders();
  return (isMultiValueMap ? headers : headers.toSingleValueMap());
}

代码示例来源:origin: spring-projects/spring-framework

private boolean hasErrorsArgument(MethodParameter parameter) {
  int i = parameter.getParameterIndex();
  Class<?>[] paramTypes = parameter.getExecutable().getParameterTypes();
  return (paramTypes.length > i + 1 && Errors.class.isAssignableFrom(paramTypes[i + 1]));
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public boolean canDecode(ResolvableType elementType, @Nullable MimeType mimeType) {
  JavaType javaType = getObjectMapper().getTypeFactory().constructType(elementType.getType());
  // Skip String: CharSequenceDecoder + "*/*" comes after
  return (!CharSequence.class.isAssignableFrom(elementType.toClass()) &&
      getObjectMapper().canDeserialize(javaType) && supportsMimeType(mimeType));
}

代码示例来源:origin: spring-projects/spring-framework

@Override
  protected void assertions(MethodInvocation invocation) {
    assertSame(this, invocation.getThis());
    assertTrue("Invocation should be on ITestBean: " + invocation.getMethod(),
      ITestBean.class.isAssignableFrom(invocation.getMethod().getDeclaringClass()));
  }
}

代码示例来源:origin: spring-projects/spring-framework

@SuppressWarnings("unchecked")
private <T> T resolveValue(ServerWebExchange exchange, ResolvableType type) {
  MethodParameter param = this.testMethod.arg(type);
  Mono<Object> result = this.resolver.resolveArgument(param, new BindingContext(), exchange);
  Object value = result.block(Duration.ofSeconds(5));
  assertNotNull(value);
  assertTrue("Unexpected return value type: " + value.getClass(),
      param.getParameterType().isAssignableFrom(value.getClass()));
  return (T) value;
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void createXmlMapper() {
  Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.json().indentOutput(true);
  ObjectMapper jsonObjectMapper = builder.build();
  ObjectMapper xmlObjectMapper = builder.createXmlMapper(true).build();
  assertTrue(jsonObjectMapper.isEnabled(SerializationFeature.INDENT_OUTPUT));
  assertTrue(xmlObjectMapper.isEnabled(SerializationFeature.INDENT_OUTPUT));
  assertTrue(xmlObjectMapper.getClass().isAssignableFrom(XmlMapper.class));
}

相关文章

Class类方法