Search in sources :

Example 81 with WildcardType

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

the class TypeTokenTest method testAssignableWildcardBoundedByIntArrayToArrayClass.

public void testAssignableWildcardBoundedByIntArrayToArrayClass() {
    Type wildcardType = Types.subtypeOf(int[].class);
    assertTrue(TypeToken.of(int[].class).isSupertypeOf(wildcardType));
    assertTrue(TypeToken.of(Object.class).isSupertypeOf(wildcardType));
    assertFalse(TypeToken.of(wildcardType).isSupertypeOf(wildcardType));
    assertFalse(TypeToken.of(Object[].class).isSupertypeOf(wildcardType));
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type)

Example 82 with WildcardType

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

the class TypeTokenTest method testAssignableWildcardBoundedByArrayToArrayClass.

public void testAssignableWildcardBoundedByArrayToArrayClass() {
    Type wildcardType = Types.subtypeOf(Object[].class);
    assertTrue(TypeToken.of(Object[].class).isSupertypeOf(wildcardType));
    assertTrue(TypeToken.of(Object.class).isSupertypeOf(wildcardType));
    assertFalse(TypeToken.of(wildcardType).isSupertypeOf(wildcardType));
    assertFalse(TypeToken.of(int[].class).isSupertypeOf(wildcardType));
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type)

Example 83 with WildcardType

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

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

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

the class TypeToken method canonicalizeWildcardType.

// WARNING: the returned type may have empty upper bounds, which may violate common expectations
// by user code or even some of our own code. It's fine for the purpose of checking subtypes.
// Just don't ever let the user access it.
private static WildcardType canonicalizeWildcardType(TypeVariable<?> declaration, WildcardType type) {
    Type[] declared = declaration.getBounds();
    List<Type> upperBounds = new ArrayList<>();
    for (Type bound : type.getUpperBounds()) {
        if (!any(declared).isSubtypeOf(bound)) {
            upperBounds.add(canonicalizeWildcardsInType(bound));
        }
    }
    return new Types.WildcardTypeImpl(type.getLowerBounds(), upperBounds.toArray(new Type[0]));
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ArrayList(java.util.ArrayList)

Example 85 with WildcardType

use of java.lang.reflect.WildcardType in project dbeaver by serge-rider.

the class BeanUtilsTest method testGetCollectionType2.

@Test
public void testGetCollectionType2() {
    WildcardType wildcardType = new WildcardType() {

        @Override
        public Type[] getUpperBounds() {
            return new Type[] { String.class, Integer.class };
        }

        @Override
        public Type[] getLowerBounds() {
            return new Type[0];
        }
    };
    ParameterizedType parameterizedType = new ParameterizedType() {

        @Override
        public Type[] getActualTypeArguments() {
            return new Type[] { wildcardType };
        }

        @Override
        public Type getRawType() {
            return null;
        }

        @Override
        public Type getOwnerType() {
            return null;
        }
    };
    Assert.assertEquals(String.class, BeanUtils.getCollectionType(parameterizedType));
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) Test(org.junit.Test)

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