Search in sources :

Example 6 with GenericDeclaration

use of java.lang.reflect.GenericDeclaration in project fastjson by alibaba.

the class FieldInfo method getInheritGenericType.

public static Type getInheritGenericType(Class<?> clazz, TypeVariable<?> tv) {
    Type type = null;
    GenericDeclaration gd = tv.getGenericDeclaration();
    Type superGenericType = clazz.getGenericSuperclass();
    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 = 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 7 with GenericDeclaration

use of java.lang.reflect.GenericDeclaration in project XobotOS by xamarin.

the class ImplForVariable 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 = (ImplForVariable<D>) var;
    this.genericDeclaration = formalVar.genericDeclaration;
    this.bounds = formalVar.bounds;
}
Also used : TypeVariable(java.lang.reflect.TypeVariable) GenericDeclaration(java.lang.reflect.GenericDeclaration)

Aggregations

GenericDeclaration (java.lang.reflect.GenericDeclaration)7 TypeVariable (java.lang.reflect.TypeVariable)7 GenericArrayType (java.lang.reflect.GenericArrayType)3 ParameterizedType (java.lang.reflect.ParameterizedType)3 Type (java.lang.reflect.Type)3 WildcardType (java.lang.reflect.WildcardType)2