/** * <p>Checks if the subject type may be implicitly cast to the target type * following the Java generics rules. If both types are {@link Class} * objects, the method returns the result of * {@link Class#isAssignableFrom(Class)}.</p> * * @param type the subject type to be assigned to the target type * @param toType the target type * @return {@code true} if {@code type} is assignable to {@code toType}. */ public static boolean isAssignable(final Type type, final Type toType) { return isAssignable(type, toType, null); }
/** * <p>Checks if the subject type may be implicitly cast to the target type * following the Java generics rules. If both types are {@link Class} * objects, the method returns the result of * {@link Class#isAssignableFrom(Class)}.</p> * * @param type the subject type to be assigned to the target type * @param toType the target type * @return {@code true} if {@code type} is assignable to {@code toType}. */ public static boolean isAssignable(final Type type, final Type toType) { return isAssignable(type, toType, null); }
/** * <p>Checks if the subject type may be implicitly cast to the target type * following the Java generics rules. If both types are {@link Class} * objects, the method returns the result of * {@link Class#isAssignableFrom(Class)}.</p> * * @param type the subject type to be assigned to the target type * @param toType the target type * @return {@code true} if {@code type} is assignable to {@code toType}. */ public static boolean isAssignable(final Type type, final Type toType) { return isAssignable(type, toType, null); }
public boolean isValid() { return method != null && method != NULL_METHOD && TypeUtils.isAssignable(method.getReturnType(), type); }
public boolean isValid() { return method != null && method != NULL_METHOD && TypeUtils.isAssignable(method.getReturnType(), type); }
/** * <p>Checks if the subject type may be implicitly cast to the target type * following the Java generics rules.</p> * * @param type the subject type to be assigned to the target type * @param toType the target type * @param typeVarAssigns optional map of type variable assignments * @return {@code true} if {@code type} is assignable to {@code toType}. */ private static boolean isAssignable(final Type type, final Type toType, final Map<TypeVariable<?>, Type> typeVarAssigns) { if (toType == null || toType instanceof Class<?>) { return isAssignable(type, (Class<?>) toType); } if (toType instanceof ParameterizedType) { return isAssignable(type, (ParameterizedType) toType, typeVarAssigns); } if (toType instanceof GenericArrayType) { return isAssignable(type, (GenericArrayType) toType, typeVarAssigns); } if (toType instanceof WildcardType) { return isAssignable(type, (WildcardType) toType, typeVarAssigns); } if (toType instanceof TypeVariable<?>) { return isAssignable(type, (TypeVariable<?>) toType, typeVarAssigns); } throw new IllegalStateException("found an unhandled type: " + toType); }
/** * <p>Checks if the subject type may be implicitly cast to the target type * following the Java generics rules.</p> * * @param type the subject type to be assigned to the target type * @param toType the target type * @param typeVarAssigns optional map of type variable assignments * @return {@code true} if {@code type} is assignable to {@code toType}. */ private static boolean isAssignable(final Type type, final Type toType, final Map<TypeVariable<?>, Type> typeVarAssigns) { if (toType == null || toType instanceof Class<?>) { return isAssignable(type, (Class<?>) toType); } if (toType instanceof ParameterizedType) { return isAssignable(type, (ParameterizedType) toType, typeVarAssigns); } if (toType instanceof GenericArrayType) { return isAssignable(type, (GenericArrayType) toType, typeVarAssigns); } if (toType instanceof WildcardType) { return isAssignable(type, (WildcardType) toType, typeVarAssigns); } if (toType instanceof TypeVariable<?>) { return isAssignable(type, (TypeVariable<?>) toType, typeVarAssigns); } throw new IllegalStateException("found an unhandled type: " + toType); }
/** * <p>Checks if the subject type may be implicitly cast to the target type * following the Java generics rules.</p> * * @param type the subject type to be assigned to the target type * @param toType the target type * @param typeVarAssigns optional map of type variable assignments * @return {@code true} if {@code type} is assignable to {@code toType}. */ private static boolean isAssignable(final Type type, final Type toType, final Map<TypeVariable<?>, Type> typeVarAssigns) { if (toType == null || toType instanceof Class<?>) { return isAssignable(type, (Class<?>) toType); } if (toType instanceof ParameterizedType) { return isAssignable(type, (ParameterizedType) toType, typeVarAssigns); } if (toType instanceof GenericArrayType) { return isAssignable(type, (GenericArrayType) toType, typeVarAssigns); } if (toType instanceof WildcardType) { return isAssignable(type, (WildcardType) toType, typeVarAssigns); } if (toType instanceof TypeVariable<?>) { return isAssignable(type, (TypeVariable<?>) toType, typeVarAssigns); } throw new IllegalStateException("found an unhandled type: " + toType); }
/** * <p>Checks if the given value can be assigned to the target type * following the Java generics rules.</p> * * @param value the value to be checked * @param type the target type * @return {@code true} if {@code value} is an instance of {@code type}. */ public static boolean isInstance(final Object value, final Type type) { if (type == null) { return false; } return value == null ? !(type instanceof Class<?>) || type == void.class || !((Class<?>) type).isPrimitive() : isAssignable(value.getClass(), type, null); }
/** * <p>Checks if the given value can be assigned to the target type * following the Java generics rules.</p> * * @param value the value to be checked * @param type the target type * @return {@code true} if {@code value} is an instance of {@code type}. */ public static boolean isInstance(final Object value, final Type type) { if (type == null) { return false; } return value == null ? !(type instanceof Class<?>) || type == void.class || !((Class<?>) type).isPrimitive() : isAssignable(value.getClass(), type, null); }
/** * <p>Checks if the given value can be assigned to the target type * following the Java generics rules.</p> * * @param value the value to be checked * @param type the target type * @return {@code true} if {@code value} is an instance of {@code type}. */ public static boolean isInstance(final Object value, final Type type) { if (type == null) { return false; } return value == null ? !(type instanceof Class<?>) || !((Class<?>) type).isPrimitive() : isAssignable(value.getClass(), type, null); }
private Step handleElementByType(Type type) { Type elementType; if (TypeUtils.isArrayType(type)) { elementType = TypeUtils.getArrayComponentType(type); } else if (TypeUtils.isAssignable(type, Map.class)) { elementType = Optional.ofNullable(TypeUtils.getTypeArguments(type, Map.class).get(MAP_VALUE)).orElse(MAP_VALUE); } else if (TypeUtils.isAssignable(type, Iterable.class)) { elementType = Optional.ofNullable(TypeUtils.getTypeArguments(type, Iterable.class).get(ITERABLE_ELEMENT)) .orElse(ITERABLE_ELEMENT); } else { throw Exceptions.create(IllegalArgumentException::new, "Unable to resolve element type of %s", type); } return new TypeWrapper(validatorContext, elementType); }
private Step handleElementByType(Type type) { Type elementType; if (TypeUtils.isArrayType(type)) { elementType = TypeUtils.getArrayComponentType(type); } else if (TypeUtils.isAssignable(type, Map.class)) { elementType = Optional.ofNullable(TypeUtils.getTypeArguments(type, Map.class).get(MAP_VALUE)).orElse(MAP_VALUE); } else if (TypeUtils.isAssignable(type, Iterable.class)) { elementType = Optional.ofNullable(TypeUtils.getTypeArguments(type, Iterable.class).get(ITERABLE_ELEMENT)) .orElse(ITERABLE_ELEMENT); } else { throw Exceptions.create(IllegalArgumentException::new, "Unable to resolve element type of %s", type); } return new TypeWrapper(validatorContext, elementType); }
private Class<? extends ConstraintValidator<A, ?>> findCrossParameterValidator( Set<ConstraintValidatorInfo<A>> infos) { final Set<ConstraintValidatorInfo<A>> set = infos.stream().filter(info -> info.getSupportedTargets().contains(ValidationTarget.PARAMETERS)) .collect(Collectors.toSet()); @SuppressWarnings("unchecked") final Class<A> constraintType = (Class<A>) descriptor.getAnnotation().annotationType(); final int size = set.size(); Exceptions.raiseIf(size > 1 || !isComposed() && set.isEmpty(), ConstraintDefinitionException::new, "%d cross-parameter %ss found for constraint type %s", size, CV, constraintType); final Class<? extends ConstraintValidator<A, ?>> result = set.iterator().next().getType(); if (!TypeUtils.isAssignable(Object[].class, getValidatedType(result))) { Exceptions.raise(ConstraintDefinitionException::new, "Cross-parameter %s %s does not support the validation of an object array", CV, result.getName()); } return result; }
private Class<? extends ConstraintValidator<A, ?>> findCrossParameterValidator( Set<ConstraintValidatorInfo<A>> infos) { final Set<ConstraintValidatorInfo<A>> set = infos.stream().filter(info -> info.getSupportedTargets().contains(ValidationTarget.PARAMETERS)) .collect(Collectors.toSet()); @SuppressWarnings("unchecked") final Class<A> constraintType = (Class<A>) descriptor.getAnnotation().annotationType(); final int size = set.size(); Exceptions.raiseIf(size > 1 || !isComposed() && set.isEmpty(), ConstraintDefinitionException::new, "%d cross-parameter %ss found for constraint type %s", size, CV, constraintType); final Class<? extends ConstraintValidator<A, ?>> result = set.iterator().next().getType(); if (!TypeUtils.isAssignable(Object[].class, getValidatedType(result))) { Exceptions.raise(ConstraintDefinitionException::new, "Cross-parameter %s %s does not support the validation of an object array", CV, result.getName()); } return result; }
/** * Get the Java element type of a particular container type. * * @param containerType * @return Type or <code>null</code> if <code>containerType</code> is not * some kind of {@link Map} */ public static Type getJavaElementType(Type containerType) { if (TypeUtils.isAssignable(containerType, Map.class)) { Map<TypeVariable<?>, Type> typeArguments = TypeUtils.getTypeArguments(containerType, Map.class); return ObjectUtils.defaultIfNull(TypeUtils.unrollVariables(typeArguments, MAP_TYPEVARS[1]), Object.class); } return null; }
/** * Create a parameterized type instance. * * @param owner the owning type * @param raw the raw class to create a parameterized type instance for * @param typeArguments the types used for parameterization * * @return {@link ParameterizedType} */ public static final ParameterizedType parameterizeWithOwner(final Type owner, final Class<?> raw, final Type... typeArguments) { Validate.notNull(raw, "raw class is null"); final Type useOwner; if (raw.getEnclosingClass() == null) { Validate.isTrue(owner == null, "no owner allowed for top-level %s", raw); useOwner = null; } else if (owner == null) { useOwner = raw.getEnclosingClass(); } else { Validate.isTrue(TypeUtils.isAssignable(owner, raw.getEnclosingClass()), "%s is invalid owner type for parameterized %s", owner, raw); useOwner = owner; } Validate.isTrue(raw.getTypeParameters().length == typeArguments.length, "invalid number of type parameters specified: expected %d, got %d", raw.getTypeParameters().length, typeArguments.length); return new ParameterizedTypeImpl(raw, useOwner, typeArguments); }
/** * Create a parameterized type instance. * * @param owner the owning type * @param raw the raw class to create a parameterized type instance for * @param typeArguments the types used for parameterization * * @return {@link ParameterizedType} */ public static final ParameterizedType parameterizeWithOwner(final Type owner, final Class<?> raw, final Type... typeArguments) { Validate.notNull(raw, "raw class is null"); final Type useOwner; if (raw.getEnclosingClass() == null) { Validate.isTrue(owner == null, "no owner allowed for top-level %s", raw); useOwner = null; } else if (owner == null) { useOwner = raw.getEnclosingClass(); } else { Validate.isTrue(TypeUtils.isAssignable(owner, raw.getEnclosingClass()), "%s is invalid owner type for parameterized %s", owner, raw); useOwner = owner; } Validate.isTrue(raw.getTypeParameters().length == typeArguments.length, "invalid number of type parameters specified: expected %d, got %d", raw.getTypeParameters().length, typeArguments.length); return new ParameterizedTypeImpl(raw, useOwner, typeArguments); }
/** * Create a parameterized type instance. * * @param owner the owning type * @param raw the raw class to create a parameterized type instance for * @param typeArguments the types used for parameterization * * @return {@link ParameterizedType} */ public static final ParameterizedType parameterizeWithOwner(final Type owner, final Class<?> raw, final Type... typeArguments) { Validate.notNull(raw, "raw class is null"); final Type useOwner; if (raw.getEnclosingClass() == null) { Validate.isTrue(owner == null, "no owner allowed for top-level %s", raw); useOwner = null; } else if (owner == null) { useOwner = raw.getEnclosingClass(); } else { Validate.isTrue(TypeUtils.isAssignable(owner, raw.getEnclosingClass()), "%s is invalid owner type for parameterized %s", owner, raw); useOwner = owner; } Validate.isTrue(raw.getTypeParameters().length == typeArguments.length, "invalid number of type parameters specified: expected %d, got %d", raw.getTypeParameters().length, typeArguments.length); return new ParameterizedTypeImpl(raw, useOwner, typeArguments); }
/** * Get the Java element type of a particular container type. * * @param containerType * @return Type or <code>null</code> if <code>containerType</code> is not * some type of {@link Iterable} or array */ public static Type getJavaElementType(Type containerType) { if (TypeUtils.isArrayType(containerType)) { return TypeUtils.getArrayComponentType(containerType); } if (TypeUtils.isAssignable(containerType, Iterable.class)) { Map<TypeVariable<?>, Type> typeArguments = TypeUtils.getTypeArguments(containerType, Iterable.class); Type type = TypeUtils.unrollVariables(typeArguments, ITERABLE_TYPE); return type != null ? type : Object.class; } return null; }