Search in sources :

Example 91 with GenericArrayType

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

the class ObjectArrayCodec method deserialze.

@SuppressWarnings({ "unchecked", "rawtypes" })
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
    final JSONLexer lexer = parser.lexer;
    if (lexer.token() == JSONToken.NULL) {
        lexer.nextToken(JSONToken.COMMA);
        return null;
    }
    if (lexer.token() == JSONToken.LITERAL_STRING) {
        byte[] bytes = lexer.bytesValue();
        lexer.nextToken(JSONToken.COMMA);
        return (T) bytes;
    }
    Class componentClass;
    Type componentType;
    if (type instanceof GenericArrayType) {
        GenericArrayType clazz = (GenericArrayType) type;
        componentType = clazz.getGenericComponentType();
        if (componentType instanceof TypeVariable) {
            TypeVariable typeVar = (TypeVariable) componentType;
            Type objType = parser.getContext().type;
            if (objType instanceof ParameterizedType) {
                ParameterizedType objParamType = (ParameterizedType) objType;
                Type objRawType = objParamType.getRawType();
                Type actualType = null;
                if (objRawType instanceof Class) {
                    TypeVariable[] objTypeParams = ((Class) objRawType).getTypeParameters();
                    for (int i = 0; i < objTypeParams.length; ++i) {
                        if (objTypeParams[i].getName().equals(typeVar.getName())) {
                            actualType = objParamType.getActualTypeArguments()[i];
                        }
                    }
                }
                if (actualType instanceof Class) {
                    componentClass = (Class) actualType;
                } else {
                    componentClass = Object.class;
                }
            } else {
                componentClass = TypeUtils.getClass(typeVar.getBounds()[0]);
            }
        } else {
            componentClass = TypeUtils.getClass(componentType);
        }
    } else {
        Class clazz = (Class) type;
        componentType = componentClass = clazz.getComponentType();
    }
    JSONArray array = new JSONArray();
    parser.parseArray(componentType, array, fieldName);
    return (T) toObjectArray(parser, componentClass, array);
}
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) JSONArray(com.alibaba.fastjson.JSONArray) JSONLexer(com.alibaba.fastjson.parser.JSONLexer) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 92 with GenericArrayType

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

the class FieldInfo method getFieldType.

public static Type getFieldType(final Class<?> clazz, final Type type, Type fieldType) {
    if (clazz == null || type == null) {
        return fieldType;
    }
    if (fieldType instanceof GenericArrayType) {
        GenericArrayType genericArrayType = (GenericArrayType) fieldType;
        Type componentType = genericArrayType.getGenericComponentType();
        Type componentTypeX = getFieldType(clazz, type, componentType);
        if (componentType != componentTypeX) {
            Type fieldTypeX = Array.newInstance(TypeUtils.getClass(componentTypeX), 0).getClass();
            return fieldTypeX;
        }
        return fieldType;
    }
    if (!TypeUtils.isGenericParamType(type)) {
        return fieldType;
    }
    if (fieldType instanceof TypeVariable) {
        ParameterizedType paramType = (ParameterizedType) TypeUtils.getGenericParamType(type);
        Class<?> parameterizedClass = TypeUtils.getClass(paramType);
        final TypeVariable<?> typeVar = (TypeVariable<?>) fieldType;
        TypeVariable<?>[] typeVariables = parameterizedClass.getTypeParameters();
        for (int i = 0; i < typeVariables.length; ++i) {
            if (typeVariables[i].getName().equals(typeVar.getName())) {
                fieldType = paramType.getActualTypeArguments()[i];
                return fieldType;
            }
        }
    }
    if (fieldType instanceof ParameterizedType) {
        ParameterizedType parameterizedFieldType = (ParameterizedType) fieldType;
        Type[] arguments = parameterizedFieldType.getActualTypeArguments();
        boolean changed = false;
        TypeVariable<?>[] typeVariables = null;
        Type[] actualTypes = null;
        ParameterizedType paramType = null;
        if (type instanceof ParameterizedType) {
            paramType = (ParameterizedType) type;
            typeVariables = clazz.getTypeParameters();
        } else if (clazz.getGenericSuperclass() instanceof ParameterizedType) {
            paramType = (ParameterizedType) clazz.getGenericSuperclass();
            typeVariables = clazz.getSuperclass().getTypeParameters();
        }
        for (int i = 0; i < arguments.length && paramType != null; ++i) {
            Type feildTypeArguement = arguments[i];
            if (feildTypeArguement instanceof TypeVariable) {
                TypeVariable<?> typeVar = (TypeVariable<?>) feildTypeArguement;
                for (int j = 0; j < typeVariables.length; ++j) {
                    if (typeVariables[j].getName().equals(typeVar.getName())) {
                        if (actualTypes == null) {
                            actualTypes = paramType.getActualTypeArguments();
                        }
                        if (arguments[i] != actualTypes[j]) {
                            arguments[i] = actualTypes[j];
                            changed = true;
                        }
                    }
                }
            }
        }
        if (changed) {
            fieldType = new ParameterizedTypeImpl(arguments, parameterizedFieldType.getOwnerType(), parameterizedFieldType.getRawType());
            return fieldType;
        }
    }
    return fieldType;
}
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) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 93 with GenericArrayType

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

the class MappedField method discoverType.

@SuppressWarnings("unchecked")
protected void discoverType(final Mapper mapper) {
    if (genericType instanceof TypeVariable) {
        realType = extractTypeVariable((TypeVariable) genericType);
    } else if (genericType instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) genericType;
        final Type[] types = pt.getActualTypeArguments();
        realType = toClass(pt);
        for (Type type : types) {
            if (type instanceof ParameterizedType) {
                typeParameters.add(new EphemeralMappedField((ParameterizedType) type, this, mapper));
            } else {
                if (type instanceof WildcardType) {
                    type = ((WildcardType) type).getUpperBounds()[0];
                }
                typeParameters.add(new EphemeralMappedField(type, this, mapper));
            }
        }
    } else if (genericType instanceof WildcardType) {
        final WildcardType wildcardType = (WildcardType) genericType;
        final Type[] types = wildcardType.getUpperBounds();
        realType = toClass(types[0]);
    } else if (genericType instanceof Class) {
        realType = (Class) genericType;
    } else if (genericType instanceof GenericArrayType) {
        final Type genericComponentType = ((GenericArrayType) genericType).getGenericComponentType();
        if (genericComponentType instanceof ParameterizedType) {
            ParameterizedType pt = (ParameterizedType) genericComponentType;
            realType = toClass(genericType);
            final Type[] types = pt.getActualTypeArguments();
            for (Type type : types) {
                if (type instanceof ParameterizedType) {
                    typeParameters.add(new EphemeralMappedField((ParameterizedType) type, this, mapper));
                } else {
                    if (type instanceof WildcardType) {
                        type = ((WildcardType) type).getUpperBounds()[0];
                    }
                    typeParameters.add(new EphemeralMappedField(type, this, mapper));
                }
            }
        } else {
            if (genericComponentType instanceof TypeVariable) {
                realType = toClass(genericType);
            } else {
                realType = (Class) genericComponentType;
            }
        }
    }
    if (Object.class.equals(realType) || Object[].class.equals(realType)) {
        if (LOG.isWarningEnabled()) {
            LOG.warning(format("Parameterized types are treated as untyped Objects. See field '%s' on %s", field.getName(), field.getDeclaringClass()));
        }
    }
    if (realType == null) {
        throw new MappingException(format("A type could not be found for the field %s.%s", getType(), getField()));
    }
}
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 94 with GenericArrayType

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

the class ReflectionUtils method getParameterizedType.

/**
     * Returns the parameterized type for a field
     *
     * @param field the field to examine
     * @param index the location of the parameter to return
     * @return the type
     */
public static Type getParameterizedType(final Field field, final int index) {
    if (field != null) {
        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) {
                //((GenericArrayType) paramType).getGenericComponentType();
                return paramType;
            } else {
                if (paramType instanceof ParameterizedType) {
                    return paramType;
                } else {
                    if (paramType instanceof TypeVariable) {
                        // paramType).getName() + "> = " + ((TypeVariable) paramType).getBounds()[0]);
                        return paramType;
                    } else if (paramType instanceof WildcardType) {
                        return paramType;
                    } else if (paramType instanceof Class) {
                        return paramType;
                    } else {
                        throw new MappingException("Unknown type... pretty bad... call for help, wave your hands... yeah!");
                    }
                }
            }
        }
        // Not defined on field, but may be on class or super class...
        return getParameterizedClass(field.getType());
    }
    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) WildcardType(java.lang.reflect.WildcardType) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType) MappingException(org.mongodb.morphia.mapping.MappingException)

Example 95 with GenericArrayType

use of java.lang.reflect.GenericArrayType in project mybatis-3 by mybatis.

the class Reflector method typeToClass.

private Class<?> typeToClass(Type src) {
    Class<?> result = null;
    if (src instanceof Class) {
        result = (Class<?>) src;
    } else if (src instanceof ParameterizedType) {
        result = (Class<?>) ((ParameterizedType) src).getRawType();
    } else if (src instanceof GenericArrayType) {
        Type componentType = ((GenericArrayType) src).getGenericComponentType();
        if (componentType instanceof Class) {
            result = Array.newInstance((Class<?>) componentType, 0).getClass();
        } else {
            Class<?> componentClass = typeToClass(componentType);
            result = Array.newInstance((Class<?>) componentClass, 0).getClass();
        }
    }
    if (result == null) {
        result = Object.class;
    }
    return result;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) GenericArrayType(java.lang.reflect.GenericArrayType)

Aggregations

GenericArrayType (java.lang.reflect.GenericArrayType)126 ParameterizedType (java.lang.reflect.ParameterizedType)120 Type (java.lang.reflect.Type)97 TypeVariable (java.lang.reflect.TypeVariable)75 WildcardType (java.lang.reflect.WildcardType)72 Field (java.lang.reflect.Field)8 ArrayList (java.util.ArrayList)7 List (java.util.List)7 Method (java.lang.reflect.Method)4 Test (org.junit.Test)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 ElementType (java.lang.annotation.ElementType)3 Collection (java.util.Collection)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 GenericType (javax.ws.rs.core.GenericType)3 CompositeType (org.apache.flink.api.common.typeutils.CompositeType)3 EqualsTester (com.google.common.testing.EqualsTester)2 TypeLiteral (com.google.inject.TypeLiteral)2 DBObject (com.mongodb.DBObject)2