Search in sources :

Example 6 with FieldMetaData

use of org.apache.thrift.meta_data.FieldMetaData in project commons by twitter.

the class StructContext method computeFieldNameMap.

/**
   * Compute a new field name map for the current thrift message
   * we are parsing.
   */
private Map<String, TField> computeFieldNameMap() {
    Map<String, TField> map = new HashMap<String, TField>();
    Class<? extends TBase> clazz = getCurrentThriftMessageClass();
    // Get the metaDataMap for this Thrift class
    Map<? extends TFieldIdEnum, FieldMetaData> metaDataMap = FieldMetaData.getStructMetaDataMap(clazz);
    for (TFieldIdEnum key : metaDataMap.keySet()) {
        final String fieldName = key.getFieldName();
        final FieldMetaData metaData = metaDataMap.get(key);
        // Workaround a bug in the generated thrift message read()
        // method by mapping the ENUM type to the INT32 type
        // The thrift generated parsing code requires that, when expecting
        // a value of enum, we actually parse a value of type int32. The
        // generated read() method then looks up the enum value in a map.
        byte type = (TType.ENUM == metaData.valueMetaData.type) ? TType.I32 : metaData.valueMetaData.type;
        map.put(fieldName, new TField(fieldName, type, key.getThriftFieldId()));
    }
    return map;
}
Also used : FieldMetaData(org.apache.thrift.meta_data.FieldMetaData) TField(org.apache.thrift.protocol.TField) HashMap(java.util.HashMap) TFieldIdEnum(org.apache.thrift.TFieldIdEnum)

Example 7 with FieldMetaData

use of org.apache.thrift.meta_data.FieldMetaData in project hive by apache.

the class ThriftUnionObjectInspector method init.

/**
 * This method is only intended to be used by Utilities class in this package.
 * The reason that this method is not recursive by itself is because we want
 * to allow recursive types.
 */
@Override
protected void init(Type type, Class<?> objectClass, ObjectInspectorFactory.ObjectInspectorOptions options) {
    this.type = type;
    verifyObjectClassType(objectClass);
    this.objectClass = objectClass;
    final Field fieldMetaData;
    try {
        fieldMetaData = objectClass.getDeclaredField(FIELD_METADATA_MAP);
        assert (Map.class.isAssignableFrom(fieldMetaData.getType()));
        fieldMetaData.setAccessible(true);
    } catch (NoSuchFieldException e) {
        throw new RuntimeException("Unable to find field metadata for thrift union field ", e);
    }
    try {
        final Map<? extends TFieldIdEnum, FieldMetaData> fieldMap = (Map<? extends TFieldIdEnum, FieldMetaData>) fieldMetaData.get(null);
        synchronized (this) {
            fields = new ArrayList<StandardStructObjectInspector.MyField>(fieldMap.size());
            this.ois = new ArrayList<ObjectInspector>();
            for (Map.Entry<? extends TFieldIdEnum, FieldMetaData> metadata : fieldMap.entrySet()) {
                int fieldId = metadata.getKey().getThriftFieldId();
                String fieldName = metadata.getValue().fieldName;
                final Type fieldType = ThriftObjectInspectorUtils.getFieldType(objectClass, fieldName);
                final ObjectInspector reflectionObjectInspector = ObjectInspectorFactory.getReflectionObjectInspector(fieldType, options, false);
                fields.add(new StandardStructObjectInspector.MyField(fieldId, fieldName, reflectionObjectInspector));
                this.ois.add(reflectionObjectInspector);
            }
            inited = true;
        }
    } catch (IllegalAccessException e) {
        throw new RuntimeException("Unable to find field metadata for thrift union field ", e);
    }
}
Also used : Field(java.lang.reflect.Field) FieldMetaData(org.apache.thrift.meta_data.FieldMetaData) Type(java.lang.reflect.Type) TFieldIdEnum(org.apache.thrift.TFieldIdEnum) Map(java.util.Map)

Aggregations

FieldMetaData (org.apache.thrift.meta_data.FieldMetaData)7 LinkedList (java.util.LinkedList)4 HashMap (java.util.HashMap)3 BatteryDetails (edu.berkeley.cs.amplab.carat.thrift.BatteryDetails)2 CpuStatus (edu.berkeley.cs.amplab.carat.thrift.CpuStatus)2 Feature (edu.berkeley.cs.amplab.carat.thrift.Feature)2 NetworkDetails (edu.berkeley.cs.amplab.carat.thrift.NetworkDetails)2 ProcessInfo (edu.berkeley.cs.amplab.carat.thrift.ProcessInfo)2 Sample._Fields (edu.berkeley.cs.amplab.carat.thrift.Sample._Fields)2 TFieldIdEnum (org.apache.thrift.TFieldIdEnum)2 Sample (edu.berkeley.cs.amplab.carat.thrift.Sample)1 Field (java.lang.reflect.Field)1 Type (java.lang.reflect.Type)1 List (java.util.List)1 Map (java.util.Map)1 TField (org.apache.thrift.protocol.TField)1