Search in sources :

Example 61 with TypeVariable

use of java.lang.reflect.TypeVariable 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 62 with TypeVariable

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

the class ArrayListTypeFieldDeserializer method parseArray.

@SuppressWarnings({ "unchecked", "rawtypes" })
public final void parseArray(DefaultJSONParser parser, Type objectType, Collection array) {
    Type itemType = this.itemType;
    ObjectDeserializer itemTypeDeser = this.deserializer;
    if (objectType instanceof ParameterizedType) {
        if (itemType instanceof TypeVariable) {
            TypeVariable typeVar = (TypeVariable) itemType;
            ParameterizedType paramType = (ParameterizedType) objectType;
            Class<?> objectClass = null;
            if (paramType.getRawType() instanceof Class) {
                objectClass = (Class<?>) paramType.getRawType();
            }
            int paramIndex = -1;
            if (objectClass != null) {
                for (int i = 0, size = objectClass.getTypeParameters().length; i < size; ++i) {
                    TypeVariable item = objectClass.getTypeParameters()[i];
                    if (item.getName().equals(typeVar.getName())) {
                        paramIndex = i;
                        break;
                    }
                }
            }
            if (paramIndex != -1) {
                itemType = paramType.getActualTypeArguments()[paramIndex];
                if (!itemType.equals(this.itemType)) {
                    itemTypeDeser = parser.getConfig().getDeserializer(itemType);
                }
            }
        } else if (itemType instanceof ParameterizedType) {
            ParameterizedType parameterizedItemType = (ParameterizedType) itemType;
            Type[] itemActualTypeArgs = parameterizedItemType.getActualTypeArguments();
            if (itemActualTypeArgs.length == 1 && itemActualTypeArgs[0] instanceof TypeVariable) {
                TypeVariable typeVar = (TypeVariable) itemActualTypeArgs[0];
                ParameterizedType paramType = (ParameterizedType) objectType;
                Class<?> objectClass = null;
                if (paramType.getRawType() instanceof Class) {
                    objectClass = (Class<?>) paramType.getRawType();
                }
                int paramIndex = -1;
                if (objectClass != null) {
                    for (int i = 0, size = objectClass.getTypeParameters().length; i < size; ++i) {
                        TypeVariable item = objectClass.getTypeParameters()[i];
                        if (item.getName().equals(typeVar.getName())) {
                            paramIndex = i;
                            break;
                        }
                    }
                }
                if (paramIndex != -1) {
                    itemActualTypeArgs[0] = paramType.getActualTypeArguments()[paramIndex];
                    itemType = new ParameterizedTypeImpl(itemActualTypeArgs, parameterizedItemType.getOwnerType(), parameterizedItemType.getRawType());
                }
            }
        }
    }
    final JSONLexer lexer = parser.lexer;
    if (lexer.token() == JSONToken.LBRACKET) {
        if (itemTypeDeser == null) {
            itemTypeDeser = deserializer = parser.getConfig().getDeserializer(itemType);
            itemFastMatchToken = deserializer.getFastMatchToken();
        }
        lexer.nextToken(itemFastMatchToken);
        for (int i = 0; ; ++i) {
            if (lexer.isEnabled(Feature.AllowArbitraryCommas)) {
                while (lexer.token() == JSONToken.COMMA) {
                    lexer.nextToken();
                    continue;
                }
            }
            if (lexer.token() == JSONToken.RBRACKET) {
                break;
            }
            Object val = itemTypeDeser.deserialze(parser, itemType, i);
            array.add(val);
            parser.checkListResolve(array);
            if (lexer.token() == JSONToken.COMMA) {
                lexer.nextToken(itemFastMatchToken);
                continue;
            }
        }
        lexer.nextToken(JSONToken.COMMA);
    } else {
        if (itemTypeDeser == null) {
            itemTypeDeser = deserializer = parser.getConfig().getDeserializer(itemType);
        }
        Object val = itemTypeDeser.deserialze(parser, itemType, 0);
        array.add(val);
        parser.checkListResolve(array);
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) JSONLexer(com.alibaba.fastjson.parser.JSONLexer) ParameterizedTypeImpl(com.alibaba.fastjson.util.ParameterizedTypeImpl)

Example 63 with TypeVariable

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

the class JavaObjectDeserializer method deserialze.

@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
    if (type instanceof GenericArrayType) {
        Type componentType = ((GenericArrayType) type).getGenericComponentType();
        if (componentType instanceof TypeVariable) {
            TypeVariable<?> componentVar = (TypeVariable<?>) componentType;
            componentType = componentVar.getBounds()[0];
        }
        List<Object> list = new ArrayList<Object>();
        parser.parseArray(componentType, list);
        Class<?> componentClass;
        if (componentType instanceof Class) {
            componentClass = (Class<?>) componentType;
            Object[] array = (Object[]) Array.newInstance(componentClass, list.size());
            list.toArray(array);
            return (T) array;
        } else {
            return (T) list.toArray();
        }
    }
    if (type instanceof Class && type != Object.class && type != Serializable.class) {
        return (T) parser.parseObject(type);
    }
    return (T) parser.parse(fieldName);
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) Type(java.lang.reflect.Type) Serializable(java.io.Serializable) TypeVariable(java.lang.reflect.TypeVariable) ArrayList(java.util.ArrayList) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 64 with TypeVariable

use of java.lang.reflect.TypeVariable in project android_frameworks_base by AOSPA.

the class TypeReference method toString.

private static void toString(Type type, StringBuilder out) {
    if (type == null) {
        return;
    } else if (type instanceof TypeVariable<?>) {
        // T
        out.append(((TypeVariable<?>) type).getName());
    } else if (type instanceof Class<?>) {
        Class<?> klass = (Class<?>) type;
        out.append(klass.getName());
        toString(klass.getTypeParameters(), out);
    } else if (type instanceof ParameterizedType) {
        // "Foo<T1, T2, T3, ... Tn>"
        ParameterizedType p = (ParameterizedType) type;
        out.append(((Class<?>) p.getRawType()).getName());
        toString(p.getActualTypeArguments(), out);
    } else if (type instanceof GenericArrayType) {
        GenericArrayType gat = (GenericArrayType) type;
        toString(gat.getGenericComponentType(), out);
        out.append("[]");
    } else {
        // WildcardType, BoundedType
        // TODO:
        out.append(type.toString());
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 65 with TypeVariable

use of java.lang.reflect.TypeVariable in project undertow by undertow-io.

the class EncodingFactory method resolveReturnType.

private static Class<?> resolveReturnType(Method method, Class<? extends Decoder> decoder) {
    Type genericReturnType = method.getGenericReturnType();
    if (genericReturnType instanceof Class) {
        return (Class<?>) genericReturnType;
    } else if (genericReturnType instanceof TypeVariable) {
        TypeVariable type = ((TypeVariable) genericReturnType);
        List<Class> classes = new ArrayList<>();
        Class c = decoder;
        while (c != method.getDeclaringClass() && c != null) {
            classes.add(c);
            c = c.getSuperclass();
        }
        Collections.reverse(classes);
        String currentName = type.getName();
        int currentPos = -1;
        for (Class clz : classes) {
            for (int i = 0; i < clz.getSuperclass().getTypeParameters().length; ++i) {
                TypeVariable<? extends Class<?>> param = clz.getSuperclass().getTypeParameters()[i];
                if (param.getName().equals(currentName)) {
                    currentPos = i;
                    break;
                }
            }
            Type gs = clz.getGenericSuperclass();
            if (gs instanceof ParameterizedType) {
                ParameterizedType pt = (ParameterizedType) gs;
                Type at = pt.getActualTypeArguments()[currentPos];
                if (at instanceof Class) {
                    return (Class<?>) at;
                } else if (at instanceof TypeVariable) {
                    TypeVariable tv = (TypeVariable) at;
                    currentName = tv.getName();
                }
            }
        }
        //todo: should we throw an exception here? It should never actually be reached
        return method.getReturnType();
    } else {
        return method.getReturnType();
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) ArrayList(java.util.ArrayList) List(java.util.List)

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