org.springframework.util.Assert.isAssignable()方法的使用及代码示例

x33g5p2x  于2022-01-15 转载在 其他  
字(8.3k)|赞(0)|评价(0)|浏览(240)

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

Assert.isAssignable介绍

[英]Assert that superType.isAssignableFrom(subType) is true.

Assert.isAssignable(Number.class, myClass);

[中]断言superType.isAssignableFrom(subType)true

Assert.isAssignable(Number.class, myClass);

代码示例

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

/**
 * Assert that {@code superType.isAssignableFrom(subType)} is {@code true}.
 * <pre class="code">Assert.isAssignable(Number.class, myClass);</pre>
 * @param superType the super type to check
 * @param subType the sub type to check
 * @throws IllegalArgumentException if the classes are not assignable
 */
public static void isAssignable(Class<?> superType, Class<?> subType) {
  isAssignable(superType, subType, "");
}

代码示例来源:origin: org.springframework/spring-core

/**
 * Assert that {@code superType.isAssignableFrom(subType)} is {@code true}.
 * <pre class="code">Assert.isAssignable(Number.class, myClass);</pre>
 * @param superType the super type to check
 * @param subType the sub type to check
 * @throws IllegalArgumentException if the classes are not assignable
 */
public static void isAssignable(Class<?> superType, Class<?> subType) {
  isAssignable(superType, subType, "");
}

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

public Jackson2SmileDecoder(ObjectMapper mapper, MimeType... mimeTypes) {
  super(mapper, mimeTypes);
  Assert.isAssignable(SmileFactory.class, mapper.getFactory().getClass());
}

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

/**
 * Instantiate a class using its no-arg constructor and return the new instance
 * as the specified assignable type.
 * <p>Useful in cases where the type of the class to instantiate (clazz) is not
 * available, but the type desired (assignableTo) is known.
 * <p>Note that this method tries to set the constructor accessible if given a
 * non-accessible (that is, non-public) constructor.
 * @param clazz class to instantiate
 * @param assignableTo type that clazz must be assignableTo
 * @return the new instance
 * @throws BeanInstantiationException if the bean cannot be instantiated
 * @see Constructor#newInstance
 */
@SuppressWarnings("unchecked")
public static <T> T instantiateClass(Class<?> clazz, Class<T> assignableTo) throws BeanInstantiationException {
  Assert.isAssignable(assignableTo, clazz);
  return (T) instantiateClass(clazz);
}

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

public Jackson2SmileEncoder(ObjectMapper mapper, MimeType... mimeTypes) {
  super(mapper, mimeTypes);
  Assert.isAssignable(SmileFactory.class, mapper.getFactory().getClass());
  setStreamingMediaTypes(Collections.singletonList(new MediaType("application", "stream+x-jackson-smile")));
}

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

@Nullable
private Type getHttpEntityType(MethodParameter parameter) {
  Assert.isAssignable(HttpEntity.class, parameter.getParameterType());
  Type parameterType = parameter.getGenericParameterType();
  if (parameterType instanceof ParameterizedType) {
    ParameterizedType type = (ParameterizedType) parameterType;
    if (type.getActualTypeArguments().length != 1) {
      throw new IllegalArgumentException("Expected single generic parameter on '" +
          parameter.getParameterName() + "' in method " + parameter.getMethod());
    }
    return type.getActualTypeArguments()[0];
  }
  else if (parameterType instanceof Class) {
    return Object.class;
  }
  else {
    return null;
  }
}

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

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
  if (this.scopes != null) {
    this.scopes.forEach((scopeKey, value) -> {
      if (value instanceof Scope) {
        beanFactory.registerScope(scopeKey, (Scope) value);
      }
      else if (value instanceof Class) {
        Class<?> scopeClass = (Class<?>) value;
        Assert.isAssignable(Scope.class, scopeClass, "Invalid scope class");
        beanFactory.registerScope(scopeKey, (Scope) BeanUtils.instantiateClass(scopeClass));
      }
      else if (value instanceof String) {
        Class<?> scopeClass = ClassUtils.resolveClassName((String) value, this.beanClassLoader);
        Assert.isAssignable(Scope.class, scopeClass, "Invalid scope class");
        beanFactory.registerScope(scopeKey, (Scope) BeanUtils.instantiateClass(scopeClass));
      }
      else {
        throw new IllegalArgumentException("Mapped value [" + value + "] for scope key [" +
            scopeKey + "] is not an instance of required type [" + Scope.class.getName() +
            "] or a corresponding Class or String value indicating a Scope implementation");
      }
    });
  }
}

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

@Test
public void isAssignableWithMessageSupplier() {
  Assert.isAssignable(Number.class, Integer.class, () -> "enigma");
}

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

@Test
public void isAssignable() {
  Assert.isAssignable(Number.class, Integer.class, "enigma");
}

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

/**
 * Cast this {@link TypeDescriptor} to a superclass or implemented interface
 * preserving annotations and nested type context.
 * @param superType the super type to cast to (can be {@code null})
 * @return a new TypeDescriptor for the up-cast type
 * @throws IllegalArgumentException if this type is not assignable to the super-type
 * @since 3.2
 */
@Nullable
public TypeDescriptor upcast(@Nullable Class<?> superType) {
  if (superType == null) {
    return null;
  }
  Assert.isAssignable(superType, getType());
  return new TypeDescriptor(getResolvableType().as(superType), superType, getAnnotations());
}

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

@Test
public void isAssignableWithNullSupertype() {
  thrown.expect(IllegalArgumentException.class);
  thrown.expectMessage("Super type to check against must not be null");
  Assert.isAssignable(null, Integer.class, "enigma");
}

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

@Test
public void isAssignableWithTypeMismatchAndCustomMessage() {
  thrown.expect(IllegalArgumentException.class);
  thrown.expectMessage("Custom message: class java.lang.Integer");
  Assert.isAssignable(String.class, Integer.class, "Custom message");
}

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

@Test
public void isAssignableWithTypeMismatchAndNullMessage() {
  thrown.expect(IllegalArgumentException.class);
  thrown.expectMessage("class java.lang.Integer is not assignable to class java.lang.String");
  Assert.isAssignable(String.class, Integer.class, (String) null);
}

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

@Test
public void isAssignableWithNullSubtypeAndMessageSupplier() {
  thrown.expect(IllegalArgumentException.class);
  thrown.expectMessage("enigma: null");
  Assert.isAssignable(Integer.class, null, () -> "enigma");
}

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

@Test
public void isAssignableWithNullSubtype() {
  thrown.expect(IllegalArgumentException.class);
  thrown.expectMessage("enigma: null");
  Assert.isAssignable(Integer.class, null, "enigma");
}

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

@Test
public void isAssignableWithTypeMismatchAndCustomMessageWithSeparator() {
  thrown.expect(IllegalArgumentException.class);
  thrown.expectMessage("Custom message: class java.lang.Integer is not assignable to class java.lang.String");
  Assert.isAssignable(String.class, Integer.class, "Custom message:");
}

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

@Test
public void isAssignableWithNullSupertypeAndMessageSupplier() {
  thrown.expect(IllegalArgumentException.class);
  thrown.expectMessage("Super type to check against must not be null");
  Assert.isAssignable(null, Integer.class, () -> "enigma");
}

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

@Test
public void isAssignableWithTypeMismatchAndNullMessageSupplier() {
  thrown.expect(IllegalArgumentException.class);
  thrown.expectMessage("class java.lang.Integer is not assignable to class java.lang.String");
  Assert.isAssignable(String.class, Integer.class, (Supplier<String>) null);
}

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

@Test
public void isAssignableWithTypeMismatchAndCustomMessageWithSpace() {
  thrown.expect(IllegalArgumentException.class);
  thrown.expectMessage("Custom message for class java.lang.Integer");
  Assert.isAssignable(String.class, Integer.class, "Custom message for ");
}

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

@Test
public void isAssignableWithTypeMismatchAndMessageSupplier() {
  thrown.expect(IllegalArgumentException.class);
  thrown.expectMessage("enigma: class java.lang.Integer");
  Assert.isAssignable(String.class, Integer.class, () -> "enigma");
}

相关文章