Search in sources :

Example 1 with GenericDeclaration

use of java.lang.reflect.GenericDeclaration in project jdk8u_jdk by JetBrains.

the class TestPlainArrayNotGeneric method check2.

private static void check2(Type t, String what) {
    if (t instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) t;
        check(pt.getActualTypeArguments(), "type argument", what);
    } else if (t instanceof TypeVariable) {
        TypeVariable<?> tv = (TypeVariable<?>) t;
        check(tv.getBounds(), "bound", what);
        GenericDeclaration gd = tv.getGenericDeclaration();
        if (gd instanceof Type)
            check((Type) gd, "declaration containing " + what);
    } else if (t instanceof WildcardType) {
        WildcardType wt = (WildcardType) t;
        check(wt.getLowerBounds(), "lower bound", "wildcard type in " + what);
        check(wt.getUpperBounds(), "upper bound", "wildcard type in " + what);
    } else if (t instanceof Class<?>) {
        Class<?> c = (Class<?>) t;
        check(c.getGenericInterfaces(), "superinterface", c.toString());
        check(c.getGenericSuperclass(), "superclass of " + c);
        check(c.getTypeParameters(), "type parameter", c.toString());
    } else if (t instanceof GenericArrayType) {
        GenericArrayType gat = (GenericArrayType) t;
        Type comp = gat.getGenericComponentType();
        if (comp instanceof Class) {
            fail("Type " + t + " uses GenericArrayType when plain " + "array would do, in " + what);
        } else
            check(comp, "component type of " + what);
    } else {
        fail("TEST BUG: mutant Type " + t + " (a " + t.getClass().getName() + ")");
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) WildcardType(java.lang.reflect.WildcardType) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType) GenericDeclaration(java.lang.reflect.GenericDeclaration)

Example 2 with GenericDeclaration

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

the class TypeVariableImpl method resolve.

void resolve() {
    if (formalVar != null) {
        return;
    }
    GenericDeclaration curLayer = declOfVarUser;
    TypeVariable var;
    while ((var = findFormalVar(curLayer, name)) == null) {
        curLayer = nextLayer(curLayer);
        if (curLayer == null) {
            throw new AssertionError("illegal type variable reference");
        }
    }
    formalVar = (TypeVariableImpl<D>) var;
    this.genericDeclaration = formalVar.genericDeclaration;
    this.bounds = formalVar.bounds;
}
Also used : TypeVariable(java.lang.reflect.TypeVariable) GenericDeclaration(java.lang.reflect.GenericDeclaration)

Example 3 with GenericDeclaration

use of java.lang.reflect.GenericDeclaration in project uavstack by uavorg.

the class FieldInfo method getInheritGenericType.

public static Type getInheritGenericType(Class<?> clazz, TypeVariable<?> tv) {
    Type type = null;
    GenericDeclaration gd = tv.getGenericDeclaration();
    do {
        type = clazz.getGenericSuperclass();
        if (type == null) {
            return null;
        }
        if (type instanceof ParameterizedType) {
            ParameterizedType ptype = (ParameterizedType) type;
            if (ptype.getRawType() == gd) {
                TypeVariable<?>[] tvs = gd.getTypeParameters();
                Type[] types = ptype.getActualTypeArguments();
                for (int i = 0; i < tvs.length; i++) {
                    if (tvs[i] == tv)
                        return types[i];
                }
                return null;
            }
        }
        clazz = TypeUtils.getClass(type);
    } while (type != null);
    return null;
}
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) GenericDeclaration(java.lang.reflect.GenericDeclaration)

Example 4 with GenericDeclaration

use of java.lang.reflect.GenericDeclaration in project BIMserver by opensourceBIM.

the class SServicesMap method getGenericType.

@SuppressWarnings("restriction")
public Class<?> getGenericType(Method method) {
    Type genericReturnType = method.getGenericReturnType();
    if (method.getGenericReturnType() instanceof ParameterizedType) {
        ParameterizedType parameterizedTypeImpl = (ParameterizedType) genericReturnType;
        Type first = parameterizedTypeImpl.getActualTypeArguments()[0];
        if (first instanceof WildcardType) {
            return null;
        } else if (first instanceof ParameterizedType) {
            return null;
        } else if (first instanceof sun.reflect.generics.reflectiveObjects.TypeVariableImpl) {
            sun.reflect.generics.reflectiveObjects.TypeVariableImpl<?> typeVariableImpl = (sun.reflect.generics.reflectiveObjects.TypeVariableImpl<?>) first;
            GenericDeclaration genericDeclaration = typeVariableImpl.getGenericDeclaration();
            if (genericDeclaration instanceof Class) {
                return (Class<?>) genericDeclaration;
            }
        } else {
            return (Class<?>) first;
        }
    }
    return (Class<?>) method.getGenericReturnType();
}
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) GenericDeclaration(java.lang.reflect.GenericDeclaration)

Example 5 with GenericDeclaration

use of java.lang.reflect.GenericDeclaration in project airlift by airlift.

the class TypeParameterUtils method getTypeParameters.

public static Type[] getTypeParameters(Class<?> desiredType, Type type) {
    if (type instanceof Class) {
        Class<?> rawClass = (Class<?>) type;
        // if this is the collection class we're done
        if (desiredType.equals(type)) {
            return null;
        }
        for (Type iface : rawClass.getGenericInterfaces()) {
            Type[] collectionType = getTypeParameters(desiredType, iface);
            if (collectionType != null) {
                return collectionType;
            }
        }
        return getTypeParameters(desiredType, rawClass.getGenericSuperclass());
    }
    if (type instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) type;
        Type rawType = parameterizedType.getRawType();
        if (desiredType.equals(rawType)) {
            return parameterizedType.getActualTypeArguments();
        }
        Type[] collectionTypes = getTypeParameters(desiredType, rawType);
        if (collectionTypes != null) {
            for (int i = 0; i < collectionTypes.length; i++) {
                if (collectionTypes[i] instanceof TypeVariable) {
                    TypeVariable<?> typeVariable = (TypeVariable<?>) collectionTypes[i];
                    TypeVariable<?>[] rawTypeParams = ((GenericDeclaration) rawType).getTypeParameters();
                    for (int j = 0; j < rawTypeParams.length; j++) {
                        if (typeVariable.getName().equals(rawTypeParams[j].getName())) {
                            collectionTypes[i] = parameterizedType.getActualTypeArguments()[j];
                        }
                    }
                }
            }
        }
        return collectionTypes;
    }
    return null;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) GenericDeclaration(java.lang.reflect.GenericDeclaration)

Aggregations

GenericDeclaration (java.lang.reflect.GenericDeclaration)16 TypeVariable (java.lang.reflect.TypeVariable)14 Type (java.lang.reflect.Type)11 ParameterizedType (java.lang.reflect.ParameterizedType)10 GenericArrayType (java.lang.reflect.GenericArrayType)7 WildcardType (java.lang.reflect.WildcardType)5 JavaType (com.fasterxml.jackson.databind.JavaType)1 MediaType (javax.ws.rs.core.MediaType)1 ParameterType (org.apache.cxf.jaxrs.model.ParameterType)1 GenericArrayTypeImpl (sun.reflect.generics.reflectiveObjects.GenericArrayTypeImpl)1