Search in sources :

Example 56 with TypeVariable

use of java.lang.reflect.TypeVariable in project atmosphere by Atmosphere.

the class TypeResolver method resolveArguments.

/**
     * Resolves the arguments for the {@code genericType} using the type variable information for the
     * {@code targetType}. Returns {@code null} if {@code genericType} is not parameterized or if
     * arguments cannot be resolved.
     */
public static Class<?>[] resolveArguments(Type genericType, Class<?> targetType) {
    Class<?>[] result = null;
    if (genericType instanceof ParameterizedType) {
        ParameterizedType paramType = (ParameterizedType) genericType;
        Type[] arguments = paramType.getActualTypeArguments();
        result = new Class[arguments.length];
        for (int i = 0; i < arguments.length; i++) result[i] = resolveClass(arguments[i], targetType);
    } else if (genericType instanceof TypeVariable) {
        result = new Class[1];
        result[0] = resolveClass(genericType, targetType);
    }
    return result;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable)

Example 57 with TypeVariable

use of java.lang.reflect.TypeVariable in project atmosphere by Atmosphere.

the class TypeResolver method resolveClass.

/**
     * Resolves the raw class for the given {@code genericType}, using the type variable information
     * from the {@code targetType}.
     */
public static Class<?> resolveClass(Type genericType, Class<?> targetType) {
    if (genericType instanceof Class) {
        return (Class<?>) genericType;
    } else if (genericType instanceof ParameterizedType) {
        return resolveClass(((ParameterizedType) genericType).getRawType(), targetType);
    } else if (genericType instanceof GenericArrayType) {
        GenericArrayType arrayType = (GenericArrayType) genericType;
        Class<?> compoment = resolveClass(arrayType.getGenericComponentType(), targetType);
        return Array.newInstance(compoment, 0).getClass();
    } else if (genericType instanceof TypeVariable) {
        TypeVariable<?> variable = (TypeVariable<?>) genericType;
        genericType = getTypeVariableMap(targetType).get(variable);
        genericType = genericType == null ? resolveBound(variable) : resolveClass(genericType, targetType);
    }
    return genericType instanceof Class ? (Class<?>) genericType : Unknown.class;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 58 with TypeVariable

use of java.lang.reflect.TypeVariable in project QuickAndroid by ImKarl.

the class $Type$Types method resolve.

public static Type resolve(Type context, Class<?> contextRawType, Type toResolve) {
    // 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 ? newParameterizedTypeWithOwner(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 59 with TypeVariable

use of java.lang.reflect.TypeVariable in project QuickAndroid by ImKarl.

the class $Type$Types method equals.

/**
     * Returns true if {@code a} and {@code b} are equal.
     */
public static boolean equals(Type a, Type b) {
    if (a == b) {
        // also handles (a == null && b == null)
        return true;
    } else if (a instanceof Class) {
        // Class already specifies equals().
        return a.equals(b);
    } else if (a instanceof ParameterizedType) {
        if (!(b instanceof ParameterizedType)) {
            return false;
        }
        // TODO: save a .clone() call
        ParameterizedType pa = (ParameterizedType) a;
        ParameterizedType pb = (ParameterizedType) b;
        return equal(pa.getOwnerType(), pb.getOwnerType()) && pa.getRawType().equals(pb.getRawType()) && Arrays.equals(pa.getActualTypeArguments(), pb.getActualTypeArguments());
    } else if (a instanceof GenericArrayType) {
        if (!(b instanceof GenericArrayType)) {
            return false;
        }
        GenericArrayType ga = (GenericArrayType) a;
        GenericArrayType gb = (GenericArrayType) b;
        return equals(ga.getGenericComponentType(), gb.getGenericComponentType());
    } else if (a instanceof WildcardType) {
        if (!(b instanceof WildcardType)) {
            return false;
        }
        WildcardType wa = (WildcardType) a;
        WildcardType wb = (WildcardType) b;
        return Arrays.equals(wa.getUpperBounds(), wb.getUpperBounds()) && Arrays.equals(wa.getLowerBounds(), wb.getLowerBounds());
    } else if (a instanceof TypeVariable) {
        if (!(b instanceof TypeVariable)) {
            return false;
        }
        TypeVariable<?> va = (TypeVariable<?>) a;
        TypeVariable<?> vb = (TypeVariable<?>) b;
        return va.getGenericDeclaration() == vb.getGenericDeclaration() && va.getName().equals(vb.getName());
    } else {
        // wildcard type, etc.
        return false;
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 60 with TypeVariable

use of java.lang.reflect.TypeVariable in project QuickAndroid by ImKarl.

the class QATypeToken method isAssignableFrom.

/**
     * Private recursive helper function to actually do the type-safe checking
     * of assignability.
     */
private static boolean isAssignableFrom(Type from, ParameterizedType to, Map<String, Type> typeVarMap) {
    if (from == null) {
        return false;
    }
    if (to.equals(from)) {
        return true;
    }
    // First figure out the class and any type information.
    Class<?> clazz = $Type$Types.getRawType(from);
    ParameterizedType ptype = null;
    if (from instanceof ParameterizedType) {
        ptype = (ParameterizedType) from;
    }
    // Load up parameterized variable info if it was parameterized.
    if (ptype != null) {
        Type[] tArgs = ptype.getActualTypeArguments();
        TypeVariable<?>[] tParams = clazz.getTypeParameters();
        for (int i = 0; i < tArgs.length; i++) {
            Type arg = tArgs[i];
            TypeVariable<?> var = tParams[i];
            while (arg instanceof TypeVariable<?>) {
                TypeVariable<?> v = (TypeVariable<?>) arg;
                arg = typeVarMap.get(v.getName());
            }
            typeVarMap.put(var.getName(), arg);
        }
        // check if they are equivalent under our current mapping.
        if (typeEquals(ptype, to, typeVarMap)) {
            return true;
        }
    }
    for (Type itype : clazz.getGenericInterfaces()) {
        if (isAssignableFrom(itype, to, new HashMap<String, Type>(typeVarMap))) {
            return true;
        }
    }
    // Interfaces didn't work, try the superclass.
    Type sType = clazz.getGenericSuperclass();
    return isAssignableFrom(sType, to, new HashMap<String, Type>(typeVarMap));
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable)

Aggregations

TypeVariable (java.lang.reflect.TypeVariable)218 Type (java.lang.reflect.Type)169 ParameterizedType (java.lang.reflect.ParameterizedType)168 GenericArrayType (java.lang.reflect.GenericArrayType)122 WildcardType (java.lang.reflect.WildcardType)100 Method (java.lang.reflect.Method)44 ArrayList (java.util.ArrayList)18 HashMap (java.util.HashMap)11 GenericDeclaration (java.lang.reflect.GenericDeclaration)8 List (java.util.List)8 Map (java.util.Map)8 ElementType (java.lang.annotation.ElementType)7 Field (java.lang.reflect.Field)7 IOException (java.io.IOException)6 AccessibleObject (java.lang.reflect.AccessibleObject)6 MediaType (javax.ws.rs.core.MediaType)6 CompositeType (org.apache.flink.api.common.typeutils.CompositeType)6 TypeExtractionUtils.isClassType (org.apache.flink.api.java.typeutils.TypeExtractionUtils.isClassType)6 Test (org.junit.Test)6 VisibleForTesting (com.google.common.annotations.VisibleForTesting)5