Search in sources :

Example 1 with GenericArrayType

use of java.lang.reflect.GenericArrayType in project hive by apache.

the class ObjectInspectorFactory method getReflectionObjectInspectorNoCache.

private static ObjectInspector getReflectionObjectInspectorNoCache(Type t, ObjectInspectorOptions options, boolean ensureInited) {
    if (t instanceof GenericArrayType) {
        GenericArrayType at = (GenericArrayType) t;
        return getStandardListObjectInspector(getReflectionObjectInspector(at.getGenericComponentType(), options, ensureInited));
    }
    if (t instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) t;
        // List?
        if (List.class.isAssignableFrom((Class<?>) pt.getRawType()) || Set.class.isAssignableFrom((Class<?>) pt.getRawType())) {
            return getStandardListObjectInspector(getReflectionObjectInspector(pt.getActualTypeArguments()[0], options, ensureInited));
        }
        // Map?
        if (Map.class.isAssignableFrom((Class<?>) pt.getRawType())) {
            return getStandardMapObjectInspector(getReflectionObjectInspector(pt.getActualTypeArguments()[0], options, ensureInited), getReflectionObjectInspector(pt.getActualTypeArguments()[1], options, ensureInited));
        }
        // Otherwise convert t to RawType so we will fall into the following if
        // block.
        t = pt.getRawType();
    }
    // Must be a class.
    if (!(t instanceof Class)) {
        throw new RuntimeException(ObjectInspectorFactory.class.getName() + " internal error:" + t);
    }
    Class<?> c = (Class<?>) t;
    // Java Primitive Type?
    if (PrimitiveObjectInspectorUtils.isPrimitiveJavaType(c)) {
        return PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveObjectInspectorUtils.getTypeEntryFromPrimitiveJavaType(c).primitiveCategory);
    }
    // Java Primitive Class?
    if (PrimitiveObjectInspectorUtils.isPrimitiveJavaClass(c)) {
        return PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveObjectInspectorUtils.getTypeEntryFromPrimitiveJavaClass(c).primitiveCategory);
    }
    // Primitive Writable class?
    if (PrimitiveObjectInspectorUtils.isPrimitiveWritableClass(c)) {
        return PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(PrimitiveObjectInspectorUtils.getTypeEntryFromPrimitiveWritableClass(c).primitiveCategory);
    }
    // Enum class?
    if (Enum.class.isAssignableFrom(c)) {
        return PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveObjectInspector.PrimitiveCategory.STRING);
    }
    // Must be struct because List and Map need to be ParameterizedType
    assert (!List.class.isAssignableFrom(c));
    assert (!Map.class.isAssignableFrom(c));
    // Create StructObjectInspector
    ReflectionStructObjectInspector oi;
    switch(options) {
        case JAVA:
            oi = new ReflectionStructObjectInspector();
            break;
        case THRIFT:
            oi = TUnion.class.isAssignableFrom(c) ? new ThriftUnionObjectInspector() : new ThriftStructObjectInspector();
            break;
        case PROTOCOL_BUFFERS:
            oi = new ProtocolBuffersStructObjectInspector();
            break;
        default:
            throw new RuntimeException(ObjectInspectorFactory.class.getName() + ": internal error.");
    }
    // put it into the cache BEFORE it is initialized to make sure we can catch
    // recursive types.
    ReflectionStructObjectInspector prev = (ReflectionStructObjectInspector) objectInspectorCache.putIfAbsent(t, oi);
    if (prev != null) {
        oi = prev;
    } else {
        try {
            oi.init(t, c, options);
        } finally {
            if (!oi.inited) {
                // Failed to init, remove it from cache
                objectInspectorCache.remove(t, oi);
            }
        }
    }
    return oi;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Set(java.util.Set) HashSet(java.util.HashSet) ArrayList(java.util.ArrayList) List(java.util.List) GenericArrayType(java.lang.reflect.GenericArrayType) PrimitiveObjectInspectorFactory(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory)

Example 2 with GenericArrayType

use of java.lang.reflect.GenericArrayType in project elasticsearch by elastic.

the class InjectorImpl method createTypeLiteralBinding.

/**
     * Converts a binding for a {@code Key<TypeLiteral<T>>} to the value {@code TypeLiteral<T>}. It's
     * a bit awkward because we have to pull out the inner type in the type literal.
     */
private <T> BindingImpl<TypeLiteral<T>> createTypeLiteralBinding(Key<TypeLiteral<T>> key, Errors errors) throws ErrorsException {
    Type typeLiteralType = key.getTypeLiteral().getType();
    if (!(typeLiteralType instanceof ParameterizedType)) {
        throw errors.cannotInjectRawTypeLiteral().toException();
    }
    ParameterizedType parameterizedType = (ParameterizedType) typeLiteralType;
    Type innerType = parameterizedType.getActualTypeArguments()[0];
    // this proves problematic, we can probably fix TypeLiteral to support type variables
    if (!(innerType instanceof Class) && !(innerType instanceof GenericArrayType) && !(innerType instanceof ParameterizedType)) {
        throw errors.cannotInjectTypeLiteralOf(innerType).toException();
    }
    // by definition, innerType == T, so this is safe
    @SuppressWarnings("unchecked") TypeLiteral<T> value = (TypeLiteral<T>) TypeLiteral.get(innerType);
    InternalFactory<TypeLiteral<T>> factory = new ConstantFactory<>(Initializables.of(value));
    return new InstanceBindingImpl<>(this, key, SourceProvider.UNKNOWN_SOURCE, factory, emptySet(), value);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) InstanceBindingImpl(org.elasticsearch.common.inject.internal.InstanceBindingImpl) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 3 with GenericArrayType

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

the class ClassUtils method getGenericClass.

public static Class<?> getGenericClass(Class<?> cls, int i) {
    try {
        ParameterizedType parameterizedType = ((ParameterizedType) cls.getGenericInterfaces()[0]);
        Object genericClass = parameterizedType.getActualTypeArguments()[i];
        if (genericClass instanceof ParameterizedType) {
            // 处理多级泛型
            return (Class<?>) ((ParameterizedType) genericClass).getRawType();
        } else if (genericClass instanceof GenericArrayType) {
            // 处理数组泛型
            return (Class<?>) ((GenericArrayType) genericClass).getGenericComponentType();
        } else if (genericClass != null) {
            return (Class<?>) genericClass;
        }
    } catch (Throwable e) {
    }
    if (cls.getSuperclass() != null) {
        return getGenericClass(cls.getSuperclass(), i);
    } else {
        throw new IllegalArgumentException(cls.getName() + " generic type undefined!");
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 4 with GenericArrayType

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

the class ReflectUtils method getGenericClass.

public static Class<?> getGenericClass(Class<?> cls, int i) {
    try {
        ParameterizedType parameterizedType = ((ParameterizedType) cls.getGenericInterfaces()[0]);
        Object genericClass = parameterizedType.getActualTypeArguments()[i];
        if (genericClass instanceof ParameterizedType) {
            // 处理多级泛型
            return (Class<?>) ((ParameterizedType) genericClass).getRawType();
        } else if (genericClass instanceof GenericArrayType) {
            // 处理数组泛型
            return (Class<?>) ((GenericArrayType) genericClass).getGenericComponentType();
        } else if (((Class) genericClass).isArray()) {
            // 在 JDK 7 以上的版本, Foo<int[]> 不再是 GenericArrayType
            return ((Class) genericClass).getComponentType();
        } else {
            return (Class<?>) genericClass;
        }
    } catch (Throwable e) {
        throw new IllegalArgumentException(cls.getName() + " generic type undefined!", e);
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) CtClass(javassist.CtClass) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 5 with GenericArrayType

use of java.lang.reflect.GenericArrayType 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)

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