Search in sources :

Example 1 with TypeVariable

use of java.lang.reflect.TypeVariable in project morphia by mongodb.

the class MappedField method toClass.

protected Class toClass(final Type t) {
    if (t == null) {
        return null;
    } else if (t instanceof Class) {
        return (Class) t;
    } else if (t instanceof GenericArrayType) {
        final Type type = ((GenericArrayType) t).getGenericComponentType();
        Class aClass;
        if (type instanceof ParameterizedType) {
            aClass = (Class) ((ParameterizedType) type).getRawType();
        } else if (type instanceof TypeVariable) {
            aClass = ReflectionUtils.getTypeArgument(persistedClass, (TypeVariable<?>) type);
            if (aClass == null) {
                aClass = Object.class;
            }
        } else {
            aClass = (Class) type;
        }
        return Array.newInstance(aClass, 0).getClass();
    } else if (t instanceof ParameterizedType) {
        return (Class) ((ParameterizedType) t).getRawType();
    } else if (t instanceof WildcardType) {
        return (Class) ((WildcardType) t).getUpperBounds()[0];
    }
    throw new RuntimeException("Generic TypeVariable not supported!");
}
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) DBObject(com.mongodb.DBObject) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 2 with TypeVariable

use of java.lang.reflect.TypeVariable in project morphia by mongodb.

the class ReflectionUtils method getTypeArgument.

/**
     * Returns the type argument
     *
     * @param clazz the Class to examine
     * @param tv    the TypeVariable to look for
     * @param <T>   the type of the Class
     * @return the Class type
     */
public static <T> Class<?> getTypeArgument(final Class<? extends T> clazz, final TypeVariable<? extends GenericDeclaration> tv) {
    final Map<Type, Type> resolvedTypes = new HashMap<Type, Type>();
    Type type = clazz;
    // start walking up the inheritance hierarchy until we hit the end
    while (type != null && !Object.class.equals(getClass(type))) {
        if (type instanceof Class) {
            // there is no useful information for us in raw types, so just
            // keep going.
            type = ((Class) type).getGenericSuperclass();
        } else {
            final ParameterizedType parameterizedType = (ParameterizedType) type;
            final Class<?> rawType = (Class) parameterizedType.getRawType();
            final Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
            final TypeVariable<?>[] typeParameters = rawType.getTypeParameters();
            for (int i = 0; i < actualTypeArguments.length; i++) {
                if (typeParameters[i].equals(tv)) {
                    final Class cls = getClass(actualTypeArguments[i]);
                    if (cls != null) {
                        return cls;
                    }
                    //We don't know that the type we want is the one in the map, if this argument has been
                    //passed through multiple levels of the hierarchy.  Walk back until we run out.
                    Type typeToTest = resolvedTypes.get(actualTypeArguments[i]);
                    while (typeToTest != null) {
                        final Class classToTest = getClass(typeToTest);
                        if (classToTest != null) {
                            return classToTest;
                        }
                        typeToTest = resolvedTypes.get(typeToTest);
                    }
                }
                resolvedTypes.put(typeParameters[i], actualTypeArguments[i]);
            }
            if (!rawType.equals(Object.class)) {
                type = rawType.getGenericSuperclass();
            }
        }
    }
    return null;
}
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) HashMap(java.util.HashMap) TypeVariable(java.lang.reflect.TypeVariable) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

Example 3 with TypeVariable

use of java.lang.reflect.TypeVariable in project morphia by mongodb.

the class ReflectionUtils method getParameterizedClass.

/**
     * Get the class that parameterizes the Field supplied, at the index supplied (field can be parameterized with multiple param classes).
     *
     * @param field the field
     * @param index the index of the parameterizing class
     * @return the class that parameterizes the field, or null if field is not parameterized
     */
public static Class getParameterizedClass(final Field field, final int index) {
    if (field.getGenericType() instanceof ParameterizedType) {
        final ParameterizedType type = (ParameterizedType) field.getGenericType();
        if ((type.getActualTypeArguments() != null) && (type.getActualTypeArguments().length <= index)) {
            return null;
        }
        final Type paramType = type.getActualTypeArguments()[index];
        if (paramType instanceof GenericArrayType) {
            final Class arrayType = (Class) ((GenericArrayType) paramType).getGenericComponentType();
            return Array.newInstance(arrayType, 0).getClass();
        } else {
            if (paramType instanceof ParameterizedType) {
                final ParameterizedType paramPType = (ParameterizedType) paramType;
                return (Class) paramPType.getRawType();
            } else {
                if (paramType instanceof TypeVariable) {
                    // from the T/V/X
                    throw new MappingException("Generic Typed Class not supported:  <" + ((TypeVariable) paramType).getName() + "> = " + ((TypeVariable) paramType).getBounds()[0]);
                } else if (paramType instanceof Class) {
                    return (Class) paramType;
                } else {
                    throw new MappingException("Unknown type... pretty bad... call for help, wave your hands... yeah!");
                }
            }
        }
    }
    return getParameterizedClass(field.getType());
}
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) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType) MappingException(org.mongodb.morphia.mapping.MappingException)

Example 4 with TypeVariable

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

the class GenericMethodListTest method testGeneriInterfaceClasses.

@Test
public void testGeneriInterfaceClasses() {
    MethodList ml = new MethodList(IResource.class);
    Iterator<AnnotatedMethod> i = ml.iterator();
    assertTrue(i.hasNext());
    AnnotatedMethod am = i.next();
    Method m = am.getMethod();
    Type[] types = m.getGenericParameterTypes();
    assertTrue(types[0] instanceof TypeVariable);
    assertTrue(types[1] instanceof Class);
    assertTrue(types[2] instanceof TypeVariable);
}
Also used : Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 5 with TypeVariable

use of java.lang.reflect.TypeVariable in project okhttp-OkGo by jeasonlzy.

the class TypeUtils 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;
        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 {
        // This isn't a type we support!
        return false;
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) 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