Search in sources :

Example 16 with GenericDeclaration

use of java.lang.reflect.GenericDeclaration in project j2objc by google.

the class TypeVariableImpl method resolve.

void resolve() {
    // if (formalVar != null) {
    if (genericDeclaration != 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");
        }
    }
    TypeVariableImpl<D> 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 17 with GenericDeclaration

use of java.lang.reflect.GenericDeclaration in project cxf by apache.

the class InjectionUtils method getSuperType.

public static Type getSuperType(Class<?> serviceClass, TypeVariable<?> var) {
    int pos = 0;
    GenericDeclaration genericDeclaration = var.getGenericDeclaration();
    TypeVariable<?>[] vars = genericDeclaration.getTypeParameters();
    for (; pos < vars.length; pos++) {
        if (vars[pos].getName().equals(var.getName())) {
            break;
        }
    }
    ParameterizedType genericSubtype = findGenericDeclaration(genericDeclaration, serviceClass);
    Type result = null;
    if (genericSubtype != null) {
        result = genericSubtype.getActualTypeArguments()[pos];
    }
    if (result instanceof TypeVariable) {
        result = getSuperType(serviceClass, (TypeVariable<?>) result);
    }
    if (result == null || result == Object.class) {
        for (Type bound : var.getBounds()) {
            if (bound != Object.class) {
                result = bound;
                break;
            }
        }
    }
    return result;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterType(org.apache.cxf.jaxrs.model.ParameterType) MediaType(javax.ws.rs.core.MediaType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) TypeVariable(java.lang.reflect.TypeVariable) GenericDeclaration(java.lang.reflect.GenericDeclaration)

Example 18 with GenericDeclaration

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

the class FieldInfo method getInheritGenericType.

private static Type getInheritGenericType(Class<?> clazz, Type type, TypeVariable<?> tv) {
    GenericDeclaration gd = tv.getGenericDeclaration();
    Class<?> class_gd = null;
    if (gd instanceof Class) {
        class_gd = (Class<?>) tv.getGenericDeclaration();
    }
    Type[] arguments = null;
    if (class_gd == clazz) {
        if (type instanceof ParameterizedType) {
            ParameterizedType ptype = (ParameterizedType) type;
            arguments = ptype.getActualTypeArguments();
        }
    } else {
        for (Class<?> c = clazz; c != null && c != Object.class && c != class_gd; c = c.getSuperclass()) {
            Type superType = c.getGenericSuperclass();
            if (superType instanceof ParameterizedType) {
                ParameterizedType p_superType = (ParameterizedType) superType;
                Type[] p_superType_args = p_superType.getActualTypeArguments();
                getArgument(p_superType_args, c.getTypeParameters(), arguments);
                arguments = p_superType_args;
            }
        }
    }
    if (arguments == null || class_gd == null) {
        return null;
    }
    Type actualType = null;
    TypeVariable<?>[] typeVariables = class_gd.getTypeParameters();
    for (int j = 0; j < typeVariables.length; ++j) {
        if (tv.equals(typeVariables[j])) {
            actualType = arguments[j];
            break;
        }
    }
    return actualType;
}
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 19 with GenericDeclaration

use of java.lang.reflect.GenericDeclaration in project nutz by nutzam.

the class ReflectTool method getInheritGenericType.

/**
 * 获取泛型类clazz中某个TypeVariable对应的真实Type.
 * @param clazz
 * @param tv
 * @return
 */
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;
            Type rawType = ptype.getRawType();
            boolean eq = gd.equals(rawType) || (gd instanceof Class && rawType instanceof Class && ((Class) gd).isAssignableFrom((Class) rawType));
            if (eq) {
                TypeVariable<?>[] tvs = gd.getTypeParameters();
                Type[] types = ptype.getActualTypeArguments();
                for (int i = 0; i < tvs.length; i++) {
                    if (tv.equals(tvs[i])) {
                        return types[i];
                    }
                }
                return null;
            }
        }
        clazz = Lang.getTypeClass(type);
    } while (type != null);
    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)19 TypeVariable (java.lang.reflect.TypeVariable)18 Type (java.lang.reflect.Type)14 ParameterizedType (java.lang.reflect.ParameterizedType)13 GenericArrayType (java.lang.reflect.GenericArrayType)9 WildcardType (java.lang.reflect.WildcardType)6 MediaType (javax.ws.rs.core.MediaType)2 ParameterType (org.apache.cxf.jaxrs.model.ParameterType)2 JavaType (com.fasterxml.jackson.databind.JavaType)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Stream (java.util.stream.Stream)1 GenericArrayTypeImpl (sun.reflect.generics.reflectiveObjects.GenericArrayTypeImpl)1