Search in sources :

Example 61 with WildcardType

use of java.lang.reflect.WildcardType in project spring-data-commons by spring-projects.

the class TypeDiscoverer method createInfo.

/**
 * Creates {@link TypeInformation} for the given {@link Type}.
 *
 * @param fieldType must not be {@literal null}.
 * @return
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
protected TypeInformation<?> createInfo(Type fieldType) {
    Assert.notNull(fieldType, "Field type must not be null!");
    if (fieldType.equals(this.type)) {
        return this;
    }
    if (fieldType instanceof Class) {
        return ClassTypeInformation.from((Class<?>) fieldType);
    }
    if (fieldType instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) fieldType;
        return new ParameterizedTypeInformation(parameterizedType, this);
    }
    if (fieldType instanceof TypeVariable) {
        TypeVariable<?> variable = (TypeVariable<?>) fieldType;
        return new TypeVariableTypeInformation(variable, this);
    }
    if (fieldType instanceof GenericArrayType) {
        return new GenericArrayTypeInformation((GenericArrayType) fieldType, this);
    }
    if (fieldType instanceof WildcardType) {
        WildcardType wildcardType = (WildcardType) fieldType;
        Type[] bounds = wildcardType.getLowerBounds();
        if (bounds.length > 0) {
            return createInfo(bounds[0]);
        }
        bounds = wildcardType.getUpperBounds();
        if (bounds.length > 0) {
            return createInfo(bounds[0]);
        }
    }
    throw new IllegalArgumentException();
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 62 with WildcardType

use of java.lang.reflect.WildcardType in project cdap by caskdata.

the class TypeToken method getGenericSuperclass.

/**
 * Returns the generic superclass of this type or {@code null} if the type represents
 * {@link Object} or an interface. This method is similar but different from {@link
 * Class#getGenericSuperclass}. For example, {@code
 * new TypeToken<StringArrayList>() {}.getGenericSuperclass()} will return {@code
 * new TypeToken<ArrayList<String>>() {}}; while {@code
 * StringArrayList.class.getGenericSuperclass()} will return {@code ArrayList<E>}, where {@code E}
 * is the type variable declared by class {@code ArrayList}.
 *
 * <p>If this type is a type variable or wildcard, its first upper bound is examined and returned
 * if the bound is a class or extends from a class. This means that the returned type could be a
 * type variable too.
 */
@Nullable
final TypeToken<? super T> getGenericSuperclass() {
    if (runtimeType instanceof TypeVariable) {
        // First bound is always the super class, if one exists.
        return boundAsSuperclass(((TypeVariable<?>) runtimeType).getBounds()[0]);
    }
    if (runtimeType instanceof WildcardType) {
        // wildcard has one and only one upper bound.
        return boundAsSuperclass(((WildcardType) runtimeType).getUpperBounds()[0]);
    }
    Type superclass = getRawType().getGenericSuperclass();
    if (superclass == null) {
        return null;
    }
    // super class of T
    @SuppressWarnings("unchecked") TypeToken<? super T> superToken = (TypeToken<? super T>) resolveSupertype(superclass);
    return superToken;
}
Also used : WildcardType(java.lang.reflect.WildcardType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) Nullable(javax.annotation.Nullable)

Example 63 with WildcardType

use of java.lang.reflect.WildcardType in project cdap by caskdata.

the class Types method newArrayType.

/**
 * Returns the array type of {@code componentType}.
 */
static Type newArrayType(Type componentType) {
    if (componentType instanceof WildcardType) {
        WildcardType wildcard = (WildcardType) componentType;
        Type[] lowerBounds = wildcard.getLowerBounds();
        checkArgument(lowerBounds.length <= 1, "Wildcard cannot have more than one lower bounds.");
        if (lowerBounds.length == 1) {
            return supertypeOf(newArrayType(lowerBounds[0]));
        } else {
            Type[] upperBounds = wildcard.getUpperBounds();
            checkArgument(upperBounds.length == 1, "Wildcard should have only one upper bound.");
            return subtypeOf(newArrayType(upperBounds[0]));
        }
    }
    return JavaVersion.CURRENT.newArrayType(componentType);
}
Also used : WildcardType(java.lang.reflect.WildcardType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type)

Example 64 with WildcardType

use of java.lang.reflect.WildcardType in project commons-lang by apache.

the class TypeUtils method isAssignable.

/**
 * <p>Checks if the subject type may be implicitly cast to the target
 * wildcard type following the Java generics rules.</p>
 *
 * @param type the subject type to be assigned to the target type
 * @param toWildcardType the target wildcard type
 * @param typeVarAssigns a map with type variables
 * @return {@code true} if {@code type} is assignable to
 * {@code toWildcardType}.
 */
private static boolean isAssignable(final Type type, final WildcardType toWildcardType, final Map<TypeVariable<?>, Type> typeVarAssigns) {
    if (type == null) {
        return true;
    }
    // would have cause the previous to return true
    if (toWildcardType == null) {
        return false;
    }
    // all types are assignable to themselves
    if (toWildcardType.equals(type)) {
        return true;
    }
    final Type[] toUpperBounds = getImplicitUpperBounds(toWildcardType);
    final Type[] toLowerBounds = getImplicitLowerBounds(toWildcardType);
    if (type instanceof WildcardType) {
        final WildcardType wildcardType = (WildcardType) type;
        final Type[] upperBounds = getImplicitUpperBounds(wildcardType);
        final Type[] lowerBounds = getImplicitLowerBounds(wildcardType);
        for (Type toBound : toUpperBounds) {
            // if there are assignments for unresolved type variables,
            // now's the time to substitute them.
            toBound = substituteTypeVariables(toBound, typeVarAssigns);
            // upper bound of the target type
            for (final Type bound : upperBounds) {
                if (!isAssignable(bound, toBound, typeVarAssigns)) {
                    return false;
                }
            }
        }
        for (Type toBound : toLowerBounds) {
            // if there are assignments for unresolved type variables,
            // now's the time to substitute them.
            toBound = substituteTypeVariables(toBound, typeVarAssigns);
            // lower bound of the subject type
            for (final Type bound : lowerBounds) {
                if (!isAssignable(toBound, bound, typeVarAssigns)) {
                    return false;
                }
            }
        }
        return true;
    }
    for (final Type toBound : toUpperBounds) {
        // now's the time to substitute them.
        if (!isAssignable(type, substituteTypeVariables(toBound, typeVarAssigns), typeVarAssigns)) {
            return false;
        }
    }
    for (final Type toBound : toLowerBounds) {
        // now's the time to substitute them.
        if (!isAssignable(substituteTypeVariables(toBound, typeVarAssigns), type, typeVarAssigns)) {
            return false;
        }
    }
    return true;
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType)

Example 65 with WildcardType

use of java.lang.reflect.WildcardType in project commons-lang by apache.

the class TypeUtils method isAssignable.

/**
 * <p>Checks if the subject type may be implicitly cast to the target
 * parameterized type following the Java generics rules.</p>
 *
 * @param type the subject type to be assigned to the target type
 * @param toParameterizedType the target parameterized type
 * @param typeVarAssigns a map with type variables
 * @return {@code true} if {@code type} is assignable to {@code toType}.
 */
private static boolean isAssignable(final Type type, final ParameterizedType toParameterizedType, final Map<TypeVariable<?>, Type> typeVarAssigns) {
    if (type == null) {
        return true;
    }
    // would have cause the previous to return true
    if (toParameterizedType == null) {
        return false;
    }
    // all types are assignable to themselves
    if (toParameterizedType.equals(type)) {
        return true;
    }
    // get the target type's raw type
    final Class<?> toClass = getRawType(toParameterizedType);
    // get the subject type's type arguments including owner type arguments
    // and supertype arguments up to and including the target class.
    final Map<TypeVariable<?>, Type> fromTypeVarAssigns = getTypeArguments(type, toClass, null);
    // null means the two types are not compatible
    if (fromTypeVarAssigns == null) {
        return false;
    }
    // to parameterized types.
    if (fromTypeVarAssigns.isEmpty()) {
        return true;
    }
    // get the target type's type arguments including owner type arguments
    final Map<TypeVariable<?>, Type> toTypeVarAssigns = getTypeArguments(toParameterizedType, toClass, typeVarAssigns);
    // now to check each type argument
    for (final TypeVariable<?> var : toTypeVarAssigns.keySet()) {
        final Type toTypeArg = unrollVariableAssignments(var, toTypeVarAssigns);
        final Type fromTypeArg = unrollVariableAssignments(var, fromTypeVarAssigns);
        if (toTypeArg == null && fromTypeArg instanceof Class) {
            continue;
        }
        // parameters of the target type.
        if (fromTypeArg != null && !toTypeArg.equals(fromTypeArg) && !(toTypeArg instanceof WildcardType && isAssignable(fromTypeArg, toTypeArg, typeVarAssigns))) {
            return false;
        }
    }
    return true;
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) TypeVariable(java.lang.reflect.TypeVariable)

Aggregations

WildcardType (java.lang.reflect.WildcardType)236 ParameterizedType (java.lang.reflect.ParameterizedType)208 Type (java.lang.reflect.Type)180 GenericArrayType (java.lang.reflect.GenericArrayType)160 TypeVariable (java.lang.reflect.TypeVariable)134 Test (org.junit.Test)24 GenericClass (org.evosuite.utils.generic.GenericClass)14 Method (java.lang.reflect.Method)10 WildcardTypeImpl (org.evosuite.utils.generic.WildcardTypeImpl)10 ArrayList (java.util.ArrayList)9 Map (java.util.Map)8 Test (org.junit.jupiter.api.Test)8 CaptureType (com.googlecode.gentyref.CaptureType)7 JSONException (com.alibaba.fastjson.JSONException)5 JSONType (com.alibaba.fastjson.annotation.JSONType)5 CatalogType (org.spongepowered.api.CatalogType)4 GenericDeclaration (java.lang.reflect.GenericDeclaration)3 HashSet (java.util.HashSet)3 List (java.util.List)3 MediaType (javax.ws.rs.core.MediaType)3