Search in sources :

Example 36 with ParameterizedType

use of java.lang.reflect.ParameterizedType in project retrofit by square.

the class Utils method resolve.

static Type resolve(Type context, Class<?> contextRawType, Type toResolve) {
    // This implementation is made a little more complicated in an attempt to avoid object-creation.
    while (true) {
        if (toResolve instanceof TypeVariable) {
            TypeVariable<?> typeVariable = (TypeVariable<?>) toResolve;
            toResolve = resolveTypeVariable(context, contextRawType, typeVariable);
            if (toResolve == typeVariable) {
                return toResolve;
            }
        } else if (toResolve instanceof Class && ((Class<?>) toResolve).isArray()) {
            Class<?> original = (Class<?>) toResolve;
            Type componentType = original.getComponentType();
            Type newComponentType = resolve(context, contextRawType, componentType);
            return componentType == newComponentType ? original : new GenericArrayTypeImpl(newComponentType);
        } else if (toResolve instanceof GenericArrayType) {
            GenericArrayType original = (GenericArrayType) toResolve;
            Type componentType = original.getGenericComponentType();
            Type newComponentType = resolve(context, contextRawType, componentType);
            return componentType == newComponentType ? original : new GenericArrayTypeImpl(newComponentType);
        } else if (toResolve instanceof ParameterizedType) {
            ParameterizedType original = (ParameterizedType) toResolve;
            Type ownerType = original.getOwnerType();
            Type newOwnerType = resolve(context, contextRawType, ownerType);
            boolean changed = newOwnerType != ownerType;
            Type[] args = original.getActualTypeArguments();
            for (int t = 0, length = args.length; t < length; t++) {
                Type resolvedTypeArgument = resolve(context, contextRawType, args[t]);
                if (resolvedTypeArgument != args[t]) {
                    if (!changed) {
                        args = args.clone();
                        changed = true;
                    }
                    args[t] = resolvedTypeArgument;
                }
            }
            return changed ? new ParameterizedTypeImpl(newOwnerType, original.getRawType(), args) : original;
        } else if (toResolve instanceof WildcardType) {
            WildcardType original = (WildcardType) toResolve;
            Type[] originalLowerBound = original.getLowerBounds();
            Type[] originalUpperBound = original.getUpperBounds();
            if (originalLowerBound.length == 1) {
                Type lowerBound = resolve(context, contextRawType, originalLowerBound[0]);
                if (lowerBound != originalLowerBound[0]) {
                    return new WildcardTypeImpl(new Type[] { Object.class }, new Type[] { lowerBound });
                }
            } else if (originalUpperBound.length == 1) {
                Type upperBound = resolve(context, contextRawType, originalUpperBound[0]);
                if (upperBound != originalUpperBound[0]) {
                    return new WildcardTypeImpl(new Type[] { upperBound }, EMPTY_TYPE_ARRAY);
                }
            }
            return original;
        } else {
            return toResolve;
        }
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) 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) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 37 with ParameterizedType

use of java.lang.reflect.ParameterizedType in project retrofit by square.

the class Utils method resolveTypeVariable.

private static Type resolveTypeVariable(Type context, Class<?> contextRawType, TypeVariable<?> unknown) {
    Class<?> declaredByRaw = declaringClassOf(unknown);
    // We can't reduce this further.
    if (declaredByRaw == null)
        return unknown;
    Type declaredBy = getGenericSupertype(context, contextRawType, declaredByRaw);
    if (declaredBy instanceof ParameterizedType) {
        int index = indexOf(declaredByRaw.getTypeParameters(), unknown);
        return ((ParameterizedType) declaredBy).getActualTypeArguments()[index];
    }
    return unknown;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type)

Example 38 with ParameterizedType

use of java.lang.reflect.ParameterizedType in project retrofit by square.

the class CallAdapterTest method parameterizedTypeInvalidIndex.

@Test
public void parameterizedTypeInvalidIndex() {
    ParameterizedType listOfString = (ParameterizedType) new TypeToken<List<String>>() {
    }.getType();
    try {
        getParameterUpperBound(-1, listOfString);
        fail();
    } catch (IllegalArgumentException e) {
        assertThat(e).hasMessage("Index -1 not in range [0,1) for java.util.List<java.lang.String>");
    }
    try {
        getParameterUpperBound(1, listOfString);
        fail();
    } catch (IllegalArgumentException e) {
        assertThat(e).hasMessage("Index 1 not in range [0,1) for java.util.List<java.lang.String>");
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) TypeToken(com.google.common.reflect.TypeToken) Test(org.junit.Test)

Example 39 with ParameterizedType

use of java.lang.reflect.ParameterizedType in project javapoet by square.

the class ParameterizedTypeName method get.

/** Returns a parameterized type equivalent to {@code type}. */
static ParameterizedTypeName get(ParameterizedType type, Map<Type, TypeVariableName> map) {
    ClassName rawType = ClassName.get((Class<?>) type.getRawType());
    ParameterizedType ownerType = (type.getOwnerType() instanceof ParameterizedType) && !Modifier.isStatic(((Class<?>) type.getRawType()).getModifiers()) ? (ParameterizedType) type.getOwnerType() : null;
    List<TypeName> typeArguments = TypeName.list(type.getActualTypeArguments(), map);
    return (ownerType != null) ? get(ownerType, map).nestedClass(rawType.simpleName(), typeArguments) : new ParameterizedTypeName(null, rawType, typeArguments);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType)

Example 40 with ParameterizedType

use of java.lang.reflect.ParameterizedType in project moshi by square.

the class TypesTest method getFirstTypeArgument.

/**
   * Given a parameterized type {@code A<B, C>}, returns B. If the specified type is not a generic
   * type, returns null.
   */
public static Type getFirstTypeArgument(Type type) throws Exception {
    if (!(type instanceof ParameterizedType))
        return null;
    ParameterizedType ptype = (ParameterizedType) type;
    Type[] actualTypeArguments = ptype.getActualTypeArguments();
    if (actualTypeArguments.length == 0)
        return null;
    return Types.canonicalize(actualTypeArguments[0]);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type)

Aggregations

ParameterizedType (java.lang.reflect.ParameterizedType)704 Type (java.lang.reflect.Type)532 GenericArrayType (java.lang.reflect.GenericArrayType)226 WildcardType (java.lang.reflect.WildcardType)182 TypeVariable (java.lang.reflect.TypeVariable)137 ArrayList (java.util.ArrayList)94 Method (java.lang.reflect.Method)71 Test (org.junit.Test)59 List (java.util.List)55 Field (java.lang.reflect.Field)49 Map (java.util.Map)47 HashMap (java.util.HashMap)42 Collection (java.util.Collection)26 MediaType (javax.ws.rs.core.MediaType)23 Annotation (java.lang.annotation.Annotation)18 HashSet (java.util.HashSet)13 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)13 ImmutableList (com.google.common.collect.ImmutableList)12 TypeLiteral (com.google.inject.TypeLiteral)12 IOException (java.io.IOException)11