Search in sources :

Example 36 with TypeVariable

use of java.lang.reflect.TypeVariable in project robovm by robovm.

the class GenericArrayTypeTest method testGetGenericComponentType.

public void testGetGenericComponentType() throws Exception {
    @SuppressWarnings("unchecked") Class<? extends A> clazz = GenericArrayTypeTest.A.class;
    Field field = clazz.getDeclaredField("array");
    Type genericType = field.getGenericType();
    assertInstanceOf(GenericArrayType.class, genericType);
    Type componentType = ((GenericArrayType) genericType).getGenericComponentType();
    assertEquals(getTypeParameter(clazz), componentType);
    assertInstanceOf(TypeVariable.class, componentType);
    TypeVariable<?> componentTypeVariable = (TypeVariable<?>) componentType;
    assertEquals("T", componentTypeVariable.getName());
    assertEquals(clazz, componentTypeVariable.getGenericDeclaration());
}
Also used : Field(java.lang.reflect.Field) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 37 with TypeVariable

use of java.lang.reflect.TypeVariable in project bazel by bazelbuild.

the class GenericTypeHelper method getActualReturnType.

/**
   * Resolves the return type of a method, in particular if the generic return
   * type ({@link Method#getGenericReturnType()}) is a type variable
   * ({@link TypeVariable}), by checking all super-classes and directly
   * implemented interfaces.
   *
   * <p>The method m must be defined by the given type or by its raw class type.
   *
   * @throws AssertionError if the generic return type could not be resolved
   */
// TODO(bazel-team): also check enclosing classes and indirectly implemented
// interfaces, which can also contribute type variables. This doesn't happen
// in the existing use cases.
public static Type getActualReturnType(Type type, Method method) {
    Type returnType = method.getGenericReturnType();
    if (returnType instanceof Class<?>) {
        return returnType;
    } else if (returnType instanceof ParameterizedType) {
        return returnType;
    } else if (returnType instanceof TypeVariable<?>) {
        TypeVariable<?> variable = (TypeVariable<?>) returnType;
        while (type != null) {
            Type candidate = matchTypeVariable(type, variable);
            if (candidate != null) {
                return candidate;
            }
            Class<?> rawType = getRawType(type);
            for (Type interfaceType : rawType.getGenericInterfaces()) {
                candidate = matchTypeVariable(interfaceType, variable);
                if (candidate != null) {
                    return candidate;
                }
            }
            type = rawType.getGenericSuperclass();
        }
    }
    throw new AssertionError("The type " + returnType + " is not a Class, ParameterizedType, or TypeVariable");
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable)

Example 38 with TypeVariable

use of java.lang.reflect.TypeVariable 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 39 with TypeVariable

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

the class Types 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 : arrayOf(newComponentType);
        } else if (toResolve instanceof GenericArrayType) {
            GenericArrayType original = (GenericArrayType) toResolve;
            Type componentType = original.getGenericComponentType();
            Type newComponentType = resolve(context, contextRawType, componentType);
            return componentType == newComponentType ? original : arrayOf(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 supertypeOf(lowerBound);
                }
            } else if (originalUpperBound.length == 1) {
                Type upperBound = resolve(context, contextRawType, originalUpperBound[0]);
                if (upperBound != originalUpperBound[0]) {
                    return subtypeOf(upperBound);
                }
            }
            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 40 with TypeVariable

use of java.lang.reflect.TypeVariable in project platform_frameworks_base by android.

the class TypeReference method toString.

private static void toString(Type type, StringBuilder out) {
    if (type == null) {
        return;
    } else if (type instanceof TypeVariable<?>) {
        // T
        out.append(((TypeVariable<?>) type).getName());
    } else if (type instanceof Class<?>) {
        Class<?> klass = (Class<?>) type;
        out.append(klass.getName());
        toString(klass.getTypeParameters(), out);
    } else if (type instanceof ParameterizedType) {
        // "Foo<T1, T2, T3, ... Tn>"
        ParameterizedType p = (ParameterizedType) type;
        out.append(((Class<?>) p.getRawType()).getName());
        toString(p.getActualTypeArguments(), out);
    } else if (type instanceof GenericArrayType) {
        GenericArrayType gat = (GenericArrayType) type;
        toString(gat.getGenericComponentType(), out);
        out.append("[]");
    } else {
        // WildcardType, BoundedType
        // TODO:
        out.append(type.toString());
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType)

Aggregations

TypeVariable (java.lang.reflect.TypeVariable)424 ParameterizedType (java.lang.reflect.ParameterizedType)342 Type (java.lang.reflect.Type)341 GenericArrayType (java.lang.reflect.GenericArrayType)251 WildcardType (java.lang.reflect.WildcardType)191 Method (java.lang.reflect.Method)63 ArrayList (java.util.ArrayList)31 HashMap (java.util.HashMap)23 GenericDeclaration (java.lang.reflect.GenericDeclaration)19 Test (org.junit.Test)16 Field (java.lang.reflect.Field)15 List (java.util.List)13 Map (java.util.Map)11 CompositeType (org.apache.flink.api.common.typeutils.CompositeType)11 TypeExtractionUtils.isClassType (org.apache.flink.api.java.typeutils.TypeExtractionUtils.isClassType)11 Member (java.lang.reflect.Member)10 MediaType (javax.ws.rs.core.MediaType)10 CaptureType (com.googlecode.gentyref.CaptureType)8 ElementType (java.lang.annotation.ElementType)8 ParameterType (org.apache.cxf.jaxrs.model.ParameterType)8