return getTypeArguments(type, toClass, null);
/** * <p>Retrieves all the type arguments for this parameterized type * including owner hierarchy arguments such as * {@code Outer<K,V>.Inner<T>.DeepInner<E>} . * The arguments are returned in a * {@link Map} specifying the argument type for each {@link TypeVariable}. * </p> * * @param type specifies the subject parameterized type from which to * harvest the parameters. * @return a {@code Map} of the type arguments to their respective type * variables. */ public static Map<TypeVariable<?>, Type> getTypeArguments(final ParameterizedType type) { return getTypeArguments(type, getRawType(type), null); }
/** * <p>Retrieves all the type arguments for this parameterized type * including owner hierarchy arguments such as * {@code Outer<K,V>.Inner<T>.DeepInner<E>} . * The arguments are returned in a * {@link Map} specifying the argument type for each {@link TypeVariable}. * </p> * * @param type specifies the subject parameterized type from which to * harvest the parameters. * @return a {@code Map} of the type arguments to their respective type * variables. */ public static Map<TypeVariable<?>, Type> getTypeArguments(final ParameterizedType type) { return getTypeArguments(type, getRawType(type), null); }
/** * <p>Retrieves all the type arguments for this parameterized type * including owner hierarchy arguments such as * {@code Outer<K,V>.Inner<T>.DeepInner<E>} . * The arguments are returned in a * {@link Map} specifying the argument type for each {@link TypeVariable}. * </p> * * @param type specifies the subject parameterized type from which to * harvest the parameters. * @return a {@code Map} of the type arguments to their respective type * variables. */ public static Map<TypeVariable<?>, Type> getTypeArguments(final ParameterizedType type) { return getTypeArguments(type, getRawType(type), null); }
private static Class<?> getValidatedType(Class<? extends ConstraintValidator<?, ?>> validatorType) { final Type result = TypeUtils.getTypeArguments(validatorType, ConstraintValidator.class) .get(ConstraintValidator.class.getTypeParameters()[1]); if (!isSupported(result)) { Exceptions.raise(ConstraintDefinitionException::new, "Validated type %s declared by %s %s is unsupported", result, CV, validatorType.getName()); } return TypeUtils.getRawType(result, null); }
private static Class<?> getValidatedType(Class<? extends ConstraintValidator<?, ?>> validatorType) { final Type result = TypeUtils.getTypeArguments(validatorType, ConstraintValidator.class) .get(ConstraintValidator.class.getTypeParameters()[1]); if (!isSupported(result)) { Exceptions.raise(ConstraintDefinitionException::new, "Validated type %s declared by %s %s is unsupported", result, CV, validatorType.getName()); } return TypeUtils.getRawType(result, null); }
public final <A extends Annotation> ValidatorMapping<A> getValidatorMapping(Class<A> constraintType) { final Optional<ValidatorMapping<A>> result = Optional.ofNullable(this.<A> doGetValidatorMapping(constraintType)); if (result.isPresent()) { for (Class<? extends ConstraintValidator<A, ?>> t : result.get().getValidatorTypes()) { final Type constraintParameter = TypeUtils.getTypeArguments(t, ConstraintValidator.class) .get(ConstraintValidator.class.getTypeParameters()[0]); if (!constraintType.equals(constraintParameter)) { Exceptions.raise(ConstraintDefinitionException::new, "%s %s expected first type parameter of %s, %s; source %s", ConstraintValidator.class, t, constraintType, constraintParameter, result.get().getSource()); } } return result.get(); } return null; }
public final <A extends Annotation> ValidatorMapping<A> getValidatorMapping(Class<A> constraintType) { final Optional<ValidatorMapping<A>> result = Optional.ofNullable(this.<A> doGetValidatorMapping(constraintType)); if (result.isPresent()) { for (Class<? extends ConstraintValidator<A, ?>> t : result.get().getValidatorTypes()) { final Type constraintParameter = TypeUtils.getTypeArguments(t, ConstraintValidator.class) .get(ConstraintValidator.class.getTypeParameters()[0]); if (!constraintType.equals(constraintParameter)) { Exceptions.raise(ConstraintDefinitionException::new, "%s %s expected first type parameter of %s, %s; source %s", ConstraintValidator.class, t, constraintType, constraintParameter, result.get().getSource()); } } return result.get(); } return 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); }
@Override public Type getType() { Type result = getHost().getType(); if (result instanceof TypeVariable<?>) { final Type parentType = parent.getType(); if (parentType instanceof ParameterizedType) { final Map<TypeVariable<?>, Type> typeArguments = TypeUtils.getTypeArguments((ParameterizedType) parentType); if (typeArguments.containsKey(result)) { return typeArguments.get(result); } } } return result; }
@Override public Type getType() { Type result = getHost().getType(); if (result instanceof TypeVariable<?>) { final Type parentType = parent.getType(); if (parentType instanceof ParameterizedType) { final Map<TypeVariable<?>, Type> typeArguments = TypeUtils.getTypeArguments((ParameterizedType) parentType); if (typeArguments.containsKey(result)) { return typeArguments.get(result); } } } return result; }
@Override public final Type getGenericType() { if (TypeUtils.containsTypeVariables(genericType)) { final Map<TypeVariable<?>, Type> args = TypeUtils.getTypeArguments(parent.getGenericType(), Object.class); return TypeUtils.unrollVariables(args, genericType); } return genericType; }
@Override public final Type getGenericType() { if (TypeUtils.containsTypeVariables(genericType)) { final Map<TypeVariable<?>, Type> args = TypeUtils.getTypeArguments(parent.getGenericType(), Object.class); return TypeUtils.unrollVariables(args, genericType); } return genericType; }
/** * 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; }
UnwrappingInfo inTermsOf(Class<?> containerClass) { final Class<?> keyContainer = containerElementKey.getContainerClass(); if (keyContainer.equals(containerClass)) { return this; } Validate.validState(keyContainer.isAssignableFrom(containerClass), "Cannot render %s in terms of %s", containerElementKey, containerClass); final ContainerElementKey key; if (containerElementKey.getTypeArgumentIndex() == null) { key = new ContainerElementKey(containerClass, null); } else { Integer typeArgumentIndex = null; final Map<TypeVariable<?>, Type> typeArguments = TypeUtils.getTypeArguments(containerClass, keyContainer); Type t = typeArguments .get(keyContainer.getTypeParameters()[containerElementKey.getTypeArgumentIndex().intValue()]); while (t instanceof TypeVariable<?>) { final TypeVariable<?> var = (TypeVariable<?>) t; if (containerClass.equals(var.getGenericDeclaration())) { typeArgumentIndex = Integer.valueOf(ObjectUtils.indexOf(containerClass.getTypeParameters(), var)); break; } t = typeArguments.get(t); } key = new ContainerElementKey(containerClass, typeArgumentIndex); } return new UnwrappingInfo(key, valueExtractor); }
public static Class<?> getExtractedType(ValueExtractor<?> extractor, Type target) { final ContainerElementKey key = ContainerElementKey.forValueExtractor(extractor); Type result = key.getAnnotatedType().getType(); if (result instanceof WildcardType && key.getTypeArgumentIndex() != null) { result = TypeUtils.getTypeArguments(target, key.getContainerClass()) .get(key.getContainerClass().getTypeParameters()[key.getTypeArgumentIndex().intValue()]); } Exceptions.raiseUnless(result instanceof Class<?>, ValueExtractorDefinitionException::new, "%s did not resolve to a %s relative to %s", f -> f.args(key, Class.class.getName(), target)); return (Class<?>) result; }
TypeUtils.getTypeArguments(runtimeType, containerClass);
public static Class<?> getExtractedType(ValueExtractor<?> extractor, Type target) { final ContainerElementKey key = ContainerElementKey.forValueExtractor(extractor); Type result = key.getAnnotatedType().getType(); if (result instanceof WildcardType && key.getTypeArgumentIndex() != null) { result = TypeUtils.getTypeArguments(target, key.getContainerClass()) .get(key.getContainerClass().getTypeParameters()[key.getTypeArgumentIndex().intValue()]); } Exceptions.raiseUnless(result instanceof Class<?>, ValueExtractorDefinitionException::new, "%s did not resolve to a %s relative to %s", f -> f.args(key, Class.class.getName(), target)); return (Class<?>) 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 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; }