Search in sources :

Example 96 with WildcardType

use of java.lang.reflect.WildcardType in project guava by hceylan.

the class TypeToken method isAssignableBySubtypeBound.

private static boolean isAssignableBySubtypeBound(Type from, WildcardType to) {
    Type toSubtypeBound = subtypeBound(to);
    if (toSubtypeBound == null) {
        return true;
    }
    Type fromSubtypeBound = subtypeBound(from);
    if (fromSubtypeBound == null) {
        return false;
    }
    return isAssignable(toSubtypeBound, fromSubtypeBound);
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type)

Example 97 with WildcardType

use of java.lang.reflect.WildcardType in project guava by hceylan.

the class TypeToken method getGenericInterfaces.

/**
 * Returns the generic interfaces that this type directly {@code implements}. This method is
 * similar but different from {@link Class#getGenericInterfaces()}. For example, {@code
 * new TypeToken<List<String>>() {}.getGenericInterfaces()} will return a list that contains
 * {@code new TypeToken<Iterable<String>>() {}}; while {@code List.class.getGenericInterfaces()}
 * will return an array that contains {@code Iterable<T>}, where the {@code T} is the type
 * variable declared by interface {@code Iterable}.
 *
 * <p>If this type is a type variable or wildcard, its upper bounds are examined and those that
 * are either an interface or upper-bounded only by interfaces are returned. This means that the
 * returned types could include type variables too.
 */
final ImmutableList<TypeToken<? super T>> getGenericInterfaces() {
    if (runtimeType instanceof TypeVariable) {
        return boundsAsInterfaces(((TypeVariable<?>) runtimeType).getBounds());
    }
    if (runtimeType instanceof WildcardType) {
        return boundsAsInterfaces(((WildcardType) runtimeType).getUpperBounds());
    }
    ImmutableList.Builder<TypeToken<? super T>> builder = ImmutableList.builder();
    for (Type interfaceType : getRawType().getGenericInterfaces()) {
        // interface of T
        @SuppressWarnings("unchecked") TypeToken<? super T> resolvedInterface = (TypeToken<? super T>) resolveSupertype(interfaceType);
        builder.add(resolvedInterface);
    }
    return builder.build();
}
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) ImmutableList(com.google.common.collect.ImmutableList)

Example 98 with WildcardType

use of java.lang.reflect.WildcardType in project guava by hceylan.

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 99 with WildcardType

use of java.lang.reflect.WildcardType in project tomee by apache.

the class InjectionUtils method getActualType.

public static Class<?> getActualType(Type genericType, int pos) {
    if (genericType == null) {
        return null;
    }
    if (genericType == Object.class) {
        return (Class<?>) genericType;
    }
    if (!ParameterizedType.class.isAssignableFrom(genericType.getClass())) {
        if (genericType instanceof TypeVariable) {
            genericType = getType(((TypeVariable<?>) genericType).getBounds(), pos);
        } else if (genericType instanceof WildcardType) {
            WildcardType wildcardType = (WildcardType) genericType;
            Type[] bounds = wildcardType.getLowerBounds();
            if (bounds.length == 0) {
                bounds = wildcardType.getUpperBounds();
            }
            genericType = getType(bounds, pos);
        } else if (genericType instanceof GenericArrayType) {
            genericType = ((GenericArrayType) genericType).getGenericComponentType();
        }
        final Class<?> cls;
        if (!(genericType instanceof ParameterizedType)) {
            cls = (Class<?>) genericType;
        } else {
            cls = (Class<?>) ((ParameterizedType) genericType).getRawType();
        }
        return cls.isArray() ? cls.getComponentType() : cls;
    }
    ParameterizedType paramType = (ParameterizedType) genericType;
    Type t = getType(paramType.getActualTypeArguments(), pos);
    return t instanceof Class ? (Class<?>) t : getActualType(t, 0);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterType(org.apache.cxf.jaxrs.model.ParameterType) MediaType(javax.ws.rs.core.MediaType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 100 with WildcardType

use of java.lang.reflect.WildcardType in project tomee by apache.

the class GenericArgumentComparator method asClass.

private Class<?> asClass(final Type aType) {
    if (aType instanceof Class) {
        return (Class<?>) aType;
    }
    if (aType instanceof ParameterizedType) {
        final ParameterizedType parameterizedType = (ParameterizedType) aType;
        return asClass(parameterizedType.getRawType());
    }
    if (aType instanceof TypeVariable) {
        final TypeVariable typeVariable = (TypeVariable) aType;
        final Type[] bounds = typeVariable.getBounds();
        if (bounds == null || bounds.length == 0) {
            return Object.class;
        } else {
            return asClass(bounds[0]);
        }
    }
    if (aType instanceof WildcardType) {
        // todo
        return Object.class;
    }
    return Object.class;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) 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