Exemple #1
0
        /**
         * Returns the standard object inspector that can be used to translate an
         * object of that typeInfo to a standard object type.
         */
        public static ObjectInspector getStandardJavaObjectInspectorFromTypeInfo(
            TypeInfo typeInfo)
        {
            ObjectInspector result = cachedStandardJavaObjectInspector.get(typeInfo);

            if (result == null)
            {
                switch (typeInfo.getCategory())
                {
                case ObjectInspectorCategory.PRIMITIVE:
                {
                    // NOTE: we use JavaPrimitiveObjectInspector instead of
                    // StandardPrimitiveObjectInspector
                    result = PrimitiveObjectInspectorFactory
                             .getPrimitiveJavaObjectInspector((PrimitiveTypeInfo)typeInfo);
                    break;
                }

                case ObjectInspectorCategory.LIST:
                {
                    ObjectInspector elementObjectInspector =
                        getStandardJavaObjectInspectorFromTypeInfo(((ListTypeInfo)typeInfo)
                                                                   .getListElementTypeInfo());
                    result = ObjectInspectorFactory
                             .getStandardListObjectInspector(elementObjectInspector);
                    break;
                }

                case ObjectInspectorCategory.MAP:
                {
                    MapTypeInfo     mapTypeInfo        = (MapTypeInfo)typeInfo;
                    ObjectInspector keyObjectInspector = getStandardJavaObjectInspectorFromTypeInfo(mapTypeInfo
                                                                                                    .getMapKeyTypeInfo());
                    ObjectInspector valueObjectInspector =
                        getStandardJavaObjectInspectorFromTypeInfo(mapTypeInfo.getMapValueTypeInfo());
                    result = ObjectInspectorFactory.getStandardMapObjectInspector(
                        keyObjectInspector, valueObjectInspector);
                    break;
                }

                case ObjectInspectorCategory.STRUCT:
                {
                    StructTypeInfo  strucTypeInfo  = (StructTypeInfo)typeInfo;
                    List <String>   fieldNames     = strucTypeInfo.getAllStructFieldNames();
                    List <TypeInfo> fieldTypeInfos = strucTypeInfo
                                                     .getAllStructFieldTypeInfos();
                    List <ObjectInspector> fieldObjectInspectors = new List <ObjectInspector>(
                        fieldTypeInfos.Count);
                    for (int i = 0; i < fieldTypeInfos.Count; i++)
                    {
                        fieldObjectInspectors
                        .Add(getStandardJavaObjectInspectorFromTypeInfo(fieldTypeInfos[i]));
                    }
                    result = ObjectInspectorFactory.getStandardStructObjectInspector(
                        fieldNames, fieldObjectInspectors);
                    break;
                }

                case ObjectInspectorCategory.UNION:
                {
                    UnionTypeInfo   unionTypeInfo   = (UnionTypeInfo)typeInfo;
                    List <TypeInfo> objectTypeInfos = unionTypeInfo
                                                      .getAllUnionObjectTypeInfos();
                    List <ObjectInspector> fieldObjectInspectors =
                        new List <ObjectInspector>(objectTypeInfos.Count);
                    for (int i = 0; i < objectTypeInfos.Count; i++)
                    {
                        fieldObjectInspectors
                        .Add(getStandardJavaObjectInspectorFromTypeInfo(objectTypeInfos[i]));
                    }
                    result = ObjectInspectorFactory.getStandardUnionObjectInspector(
                        fieldObjectInspectors);
                    break;
                }

                default:
                {
                    result = null;
                    break;
                }
                }
                ObjectInspector prev =
                    cachedStandardJavaObjectInspector.putIfAbsent(typeInfo, result);
                if (prev != null)
                {
                    result = prev;
                }
            }
            return(result);
        }
Exemple #2
0
        /**
         * Return the extended TypeInfo from a Java type. By extended TypeInfo, we
         * allow unknownType for java.lang.Object.
         *
         * @param t
         *          The Java type.
         * @param m
         *          The method, only used for generating error messages.
         */
        private static TypeInfo getExtendedTypeInfoFromJavaType(Type t, MethodInfo m)
        {
            if (t == typeof(object))
            {
                return(TypeInfoFactory.unknownTypeInfo);
            }

            if (t.IsGenericType)
            {
                Type   bt = t.GetGenericTypeDefinition();
                Type[] pt = t.GenericTypeArguments;

                // List?
                if (bt == typeof(List <>))
                {
                    return(TypeInfoFactory.getListTypeInfo(getExtendedTypeInfoFromJavaType(
                                                               pt[0], m)));
                }
                // Map?
                if (bt == typeof(Dictionary <,>))
                {
                    return(TypeInfoFactory.getMapTypeInfo(
                               getExtendedTypeInfoFromJavaType(pt[0], m),
                               getExtendedTypeInfoFromJavaType(pt[1], m)));
                }
            }

            // Java Primitive Type?
            if (PrimitiveObjectInspectorUtils.isPrimitiveJavaType(t))
            {
                return(TypeInfoUtils
                       .getTypeInfoFromObjectInspector(PrimitiveObjectInspectorFactory
                                                       .getPrimitiveJavaObjectInspector(PrimitiveObjectInspectorUtils
                                                                                        .getTypeEntryFromPrimitiveJavaType(t).primitiveCategory)));
            }

            // Java Primitive Class?
            if (PrimitiveObjectInspectorUtils.isPrimitiveJavaClass(t))
            {
                return(TypeInfoUtils
                       .getTypeInfoFromObjectInspector(PrimitiveObjectInspectorFactory
                                                       .getPrimitiveJavaObjectInspector(PrimitiveObjectInspectorUtils
                                                                                        .getTypeEntryFromPrimitiveJavaClass(t).primitiveCategory)));
            }

            // Primitive Writable class?
            if (PrimitiveObjectInspectorUtils.isPrimitiveWritableClass(t))
            {
                return(TypeInfoUtils
                       .getTypeInfoFromObjectInspector(PrimitiveObjectInspectorFactory
                                                       .getPrimitiveWritableObjectInspector(PrimitiveObjectInspectorUtils
                                                                                            .getTypeEntryFromPrimitiveWritableClass(t).primitiveCategory)));
            }

            // Must be a struct
            FieldInfo[]     fields         = ObjectInspectorUtils.getDeclaredNonStaticFields(t);
            List <string>   fieldNames     = new List <string>(fields.Length);
            List <TypeInfo> fieldTypeInfos = new List <TypeInfo>(fields.Length);

            foreach (FieldInfo field in fields)
            {
                fieldNames.Add(field.Name);
                fieldTypeInfos.Add(getExtendedTypeInfoFromJavaType(
                                       field.FieldType, m));
            }
            return(TypeInfoFactory.getStructTypeInfo(fieldNames, fieldTypeInfos));
        }