/** * 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)); }
/** * Returns the standard object inspector that can be used to translate an * object of that typeInfo to a standard object type. */ public static ObjectInspector getStandardWritableObjectInspectorFromTypeInfo( TypeInfo typeInfo) { ObjectInspector result = cachedStandardObjectInspector.get(typeInfo); if (result == null) { switch (typeInfo.getCategory()) { case ObjectInspectorCategory.PRIMITIVE: { result = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector( (PrimitiveTypeInfo)typeInfo); break; } case ObjectInspectorCategory.LIST: { ObjectInspector elementObjectInspector = getStandardWritableObjectInspectorFromTypeInfo(((ListTypeInfo)typeInfo) .getListElementTypeInfo()); result = ObjectInspectorFactory .getStandardListObjectInspector(elementObjectInspector); break; } case ObjectInspectorCategory.MAP: { MapTypeInfo mapTypeInfo = (MapTypeInfo)typeInfo; ObjectInspector keyObjectInspector = getStandardWritableObjectInspectorFromTypeInfo(mapTypeInfo.getMapKeyTypeInfo()); ObjectInspector valueObjectInspector = getStandardWritableObjectInspectorFromTypeInfo(mapTypeInfo.getMapValueTypeInfo()); result = ObjectInspectorFactory.getStandardMapObjectInspector( keyObjectInspector, valueObjectInspector); break; } case ObjectInspectorCategory.STRUCT: { StructTypeInfo structTypeInfo = (StructTypeInfo)typeInfo; List <String> fieldNames = structTypeInfo.getAllStructFieldNames(); List <TypeInfo> fieldTypeInfos = structTypeInfo .getAllStructFieldTypeInfos(); List <ObjectInspector> fieldObjectInspectors = new List <ObjectInspector>( fieldTypeInfos.Count); for (int i = 0; i < fieldTypeInfos.Count; i++) { fieldObjectInspectors .Add(getStandardWritableObjectInspectorFromTypeInfo(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(getStandardWritableObjectInspectorFromTypeInfo(objectTypeInfos[i])); } result = ObjectInspectorFactory.getStandardUnionObjectInspector( fieldObjectInspectors); break; } default: { result = null; break; } } ObjectInspector prev = cachedStandardObjectInspector.putIfAbsent(typeInfo, result); if (prev != null) { result = prev; } } return(result); }