Refine search
Validate.notNull(cls, "cls is null"); Validate.notNull(superType, "superType is null"); final Class<?> superClass = getRawType(superType); if (!isAssignable(cls, superClass)) { return null; return getTypeArguments(superType, superClass, null); final Type midType = getClosestParentType(cls, superClass); return determineTypeArguments((Class<?>) midType, superType); final Class<?> midClass = getRawType(midParameterizedType); final Map<TypeVariable<?>, Type> typeVarAssigns = determineTypeArguments(midClass, superType); mapTypeVariablesToArguments(cls, midParameterizedType, typeVarAssigns);
Validate.notNull(method); final Set<Method> result = new LinkedHashSet<>(); result.add(method); final Map<TypeVariable<?>, Type> typeArguments = TypeUtils.getTypeArguments(declaringClass, m.getDeclaringClass()); for (int i = 0; i < parameterTypes.length; i++) { final Type childType = TypeUtils.unrollVariables(typeArguments, method.getGenericParameterTypes()[i]); final Type parentType = TypeUtils.unrollVariables(typeArguments, m.getGenericParameterTypes()[i]); if (!TypeUtils.equals(childType, parentType)) { continue hierarchyTraversal;
/** * The default constructor. */ protected TypeLiteral() { this.value = Validate.notNull(TypeUtils.getTypeArguments(getClass(), TypeLiteral.class).get(T), "%s does not assign type parameter %s", getClass(), TypeUtils.toLongString(T)); this.toString = String.format("%s<%s>", TypeLiteral.class.getSimpleName(), TypeUtils.toString(value)); }
/** * <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); }
/** * Learn whether {@code t} equals {@code w}. * @param w LHS * @param t RHS * @return boolean * @since 3.2 */ private static boolean equals(final WildcardType w, final Type t) { if (t instanceof WildcardType) { final WildcardType other = (WildcardType) t; return equals(getImplicitLowerBounds(w), getImplicitLowerBounds(other)) && equals(getImplicitUpperBounds(w), getImplicitUpperBounds(other)); } return false; }
/** * 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} * @since 3.2 */ 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(isAssignable(owner, raw.getEnclosingClass()), "%s is invalid owner type for parameterized %s", owner, raw); useOwner = owner; } Validate.noNullElements(typeArguments, "null type argument at index %s"); 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); }
/** * <p>Determines whether or not specified types satisfy the bounds of their * mapped type variables. When a type parameter extends another (such as * {@code <T, S extends T>}), uses another as a type parameter (such as * {@code <T, S extends Comparable>>}), or otherwise depends on * another type variable to be specified, the dependencies must be included * in {@code typeVarAssigns}.</p> * * @param typeVarAssigns specifies the potential types to be assigned to the * type variables, not {@code null}. * @return whether or not the types can be assigned to their respective type * variables. */ public static boolean typesSatisfyVariables(final Map<TypeVariable<?>, Type> typeVarAssigns) { Validate.notNull(typeVarAssigns, "typeVarAssigns is null"); // all types must be assignable to all the bounds of their mapped // type variable. for (final Map.Entry<TypeVariable<?>, Type> entry : typeVarAssigns.entrySet()) { final TypeVariable<?> typeVar = entry.getKey(); final Type type = entry.getValue(); for (final Type bound : getImplicitBounds(typeVar)) { if (!isAssignable(type, substituteTypeVariables(bound, typeVarAssigns), typeVarAssigns)) { return false; } } } return true; }
/** * Format a {@link TypeVariable} including its {@link GenericDeclaration}. * * @param var the type variable to create a String representation for, not {@code null} * @return String * @since 3.2 */ public static String toLongString(final TypeVariable<?> var) { Validate.notNull(var, "var is null"); final StringBuilder buf = new StringBuilder(); final GenericDeclaration d = var.getGenericDeclaration(); if (d instanceof Class<?>) { Class<?> c = (Class<?>) d; while (true) { if (c.getEnclosingClass() == null) { buf.insert(0, c.getName()); break; } buf.insert(0, c.getSimpleName()).insert(0, '.'); c = c.getEnclosingClass(); } } else if (d instanceof Type) {// not possible as of now buf.append(toString((Type) d)); } else { buf.append(d); } return buf.append(':').append(typeVariableToString(var)).toString(); }
Validate.notNull(bounds, "null value specified for bounds array"); if (type1 != type2 && isAssignable(type2, type1, null)) { subtypeFound = true; break;
/** * Helper method to establish the formal parameters for a parameterized type. * @param mappings map containing the assignments * @param variables expected map keys * @return array of map values corresponding to specified keys */ private static Type[] extractTypeArgumentsFrom(final Map<TypeVariable<?>, Type> mappings, final TypeVariable<?>[] variables) { final Type[] result = new Type[variables.length]; int index = 0; for (final TypeVariable<?> var : variables) { Validate.isTrue(mappings.containsKey(var), "missing argument mapping for %s", toString(var)); result[index++] = mappings.get(var); } return result; }
/** * Append {@code types} to {@code buf} with separator {@code sep}. * @param buf destination * @param sep separator * @param types to append * @return {@code buf} * @since 3.2 */ private static <T> StringBuilder appendAllTo(final StringBuilder buf, final String sep, final T... types) { Validate.notEmpty(Validate.noNullElements(types)); if (types.length > 0) { buf.append(toString(types[0])); for (int i = 1; i < types.length; i++) { buf.append(sep).append(toString(types[i])); } } return buf; }
typeArguments = Collections.emptyMap(); if (containsTypeVariables(type)) { if (type instanceof TypeVariable<?>) { return unrollVariables(typeArguments, typeArguments.get(type)); } else { parameterizedTypeArguments = new HashMap<>(typeArguments); parameterizedTypeArguments.putAll(getTypeArguments(p)); final Type unrolled = unrollVariables(parameterizedTypeArguments, args[i]); if (unrolled != null) { args[i] = unrolled; return parameterizeWithOwner(p.getOwnerType(), (Class<?>) p.getRawType(), args); return wildcardType().withUpperBounds(unrollBounds(typeArguments, wild.getUpperBounds())) .withLowerBounds(unrollBounds(typeArguments, wild.getLowerBounds())).build();
final Map<TypeVariable<?>, Type> subtypeVarAssigns) { if (type instanceof Class<?>) { return getTypeArguments((Class<?>) type, toClass, subtypeVarAssigns); return getTypeArguments((ParameterizedType) type, toClass, subtypeVarAssigns); return getTypeArguments(((GenericArrayType) type).getGenericComponentType(), toClass .isArray() ? toClass.getComponentType() : toClass, subtypeVarAssigns); for (final Type bound : getImplicitUpperBounds((WildcardType) type)) { if (isAssignable(bound, toClass)) { return getTypeArguments(bound, toClass, subtypeVarAssigns); for (final Type bound : getImplicitBounds((TypeVariable<?>) type)) { if (isAssignable(bound, toClass)) { return getTypeArguments(bound, toClass, subtypeVarAssigns);
final Type[] toUpperBounds = getImplicitUpperBounds(toWildcardType); final Type[] toLowerBounds = getImplicitLowerBounds(toWildcardType); final Type[] upperBounds = getImplicitUpperBounds(wildcardType); final Type[] lowerBounds = getImplicitLowerBounds(wildcardType); toBound = substituteTypeVariables(toBound, typeVarAssigns); if (!isAssignable(bound, toBound, typeVarAssigns)) { return false; toBound = substituteTypeVariables(toBound, typeVarAssigns); if (!isAssignable(toBound, bound, typeVarAssigns)) { return false; if (!isAssignable(type, substituteTypeVariables(toBound, typeVarAssigns), typeVarAssigns)) { return false; if (!isAssignable(substituteTypeVariables(toBound, typeVarAssigns), type, typeVarAssigns)) { return false;
final Class<?> toClass = getRawType(toParameterizedType); final Map<TypeVariable<?>, Type> fromTypeVarAssigns = getTypeArguments(type, toClass, null); final Map<TypeVariable<?>, Type> toTypeVarAssigns = getTypeArguments(toParameterizedType, toClass, typeVarAssigns); final Type toTypeArg = unrollVariableAssignments(var, toTypeVarAssigns); final Type fromTypeArg = unrollVariableAssignments(var, fromTypeVarAssigns); && !(toTypeArg instanceof WildcardType && isAssignable(fromTypeArg, toTypeArg, typeVarAssigns))) { return false;
final ParameterizedType parameterizedType, final Class<?> toClass, final Map<TypeVariable<?>, Type> subtypeVarAssigns) { final Class<?> cls = getRawType(parameterizedType); if (!isAssignable(cls, toClass)) { return null; typeVarAssigns = getTypeArguments(parameterizedOwnerType, getRawType(parameterizedOwnerType), subtypeVarAssigns); } else { return getTypeArguments(getClosestParentType(cls, toClass), toClass, typeVarAssigns);
final Map<TypeVariable<?>, Type> subtypeVarAssigns) { if (!isAssignable(cls, toClass)) { return null; return getTypeArguments(getClosestParentType(cls, toClass), toClass, typeVarAssigns);
midClass = getRawType((ParameterizedType) midType); } else if (midType instanceof Class<?>) { midClass = (Class<?>) midType; if (isAssignable(midClass, superClass) && isAssignable(genericInterface, (Type) midClass)) { genericInterface = midType;
&& isAssignable(cls.getComponentType(), toComponentType, typeVarAssigns); return isAssignable(((GenericArrayType) type).getGenericComponentType(), toComponentType, typeVarAssigns); for (final Type bound : getImplicitUpperBounds((WildcardType) type)) { if (isAssignable(bound, toGenericArrayType)) { return true; for (final Type bound : getImplicitBounds((TypeVariable<?>) type)) { if (isAssignable(bound, toGenericArrayType)) { return true;
/** * <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 ClassUtils#isAssignable(Class, 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); }