/** * 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, ""); }
/** * 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, ""); }
public Jackson2SmileDecoder(ObjectMapper mapper, MimeType... mimeTypes) { super(mapper, mimeTypes); Assert.isAssignable(SmileFactory.class, mapper.getFactory().getClass()); }
/** * 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); }
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"))); }
@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; } }
@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"); } }); } }
@Test public void isAssignableWithMessageSupplier() { Assert.isAssignable(Number.class, Integer.class, () -> "enigma"); }
@Test public void isAssignable() { Assert.isAssignable(Number.class, Integer.class, "enigma"); }
/** * 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()); }
@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"); }
@Test public void isAssignableWithTypeMismatchAndCustomMessage() { thrown.expect(IllegalArgumentException.class); thrown.expectMessage("Custom message: class java.lang.Integer"); Assert.isAssignable(String.class, Integer.class, "Custom message"); }
@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); }
@Test public void isAssignableWithNullSubtypeAndMessageSupplier() { thrown.expect(IllegalArgumentException.class); thrown.expectMessage("enigma: null"); Assert.isAssignable(Integer.class, null, () -> "enigma"); }
@Test public void isAssignableWithNullSubtype() { thrown.expect(IllegalArgumentException.class); thrown.expectMessage("enigma: null"); Assert.isAssignable(Integer.class, null, "enigma"); }
@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:"); }
@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"); }
@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); }
@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 "); }
@Test public void isAssignableWithTypeMismatchAndMessageSupplier() { thrown.expect(IllegalArgumentException.class); thrown.expectMessage("enigma: class java.lang.Integer"); Assert.isAssignable(String.class, Integer.class, () -> "enigma"); }