@VisibleForTesting static StructInfo newStructInfo(Class<? extends TBase<?, ?>> structClass) { final String name = structClass.getName(); final Map<?, FieldMetaData> metaDataMap = FieldMetaData.getStructMetaDataMap(structClass); final List<FieldInfo> fields = metaDataMap.values().stream() .map(fieldMetaData -> newFieldInfo(structClass, fieldMetaData)) .collect(Collectors.toList()); return new StructInfo(name, fields); }
@Override @SuppressWarnings("unchecked") protected Object getRecordState(Object r, Schema s) { TFieldIdEnum[] fields = fieldCache.get(s); if (fields == null) { // cache miss fields = new TFieldIdEnum[s.getFields().size()]; Class c = r.getClass(); for (TFieldIdEnum f : FieldMetaData.getStructMetaDataMap((Class<? extends TBase>) c).keySet()) fields[s.getField(f.getFieldName()).pos()] = f; fieldCache.put(s, fields); // update cache } return fields; }
@SuppressWarnings("unchecked") final Map<TFieldIdEnum, FieldMetaData> metaDataMap = (Map<TFieldIdEnum, FieldMetaData>) FieldMetaData.getStructMetaDataMap(resultType);
private static RpcRequest toRpcRequest(Class<?> serviceType, String method, TBase<?, ?> thriftArgs) { requireNonNull(thriftArgs, "thriftArgs"); // NB: The map returned by FieldMetaData.getStructMetaDataMap() is an EnumMap, // so the parameter ordering is preserved correctly during iteration. final Set<? extends TFieldIdEnum> fields = FieldMetaData.getStructMetaDataMap(thriftArgs.getClass()).keySet(); // Handle the case where the number of arguments is 0 or 1. final int numFields = fields.size(); switch (numFields) { case 0: return RpcRequest.of(serviceType, method); case 1: return RpcRequest.of(serviceType, method, ThriftFieldAccess.get(thriftArgs, fields.iterator().next())); } // Handle the case where the number of arguments is greater than 1. final List<Object> list = new ArrayList<>(numFields); for (TFieldIdEnum field : fields) { list.add(ThriftFieldAccess.get(thriftArgs, field)); } return RpcRequest.of(serviceType, method, list); }
FieldMetaData.getStructMetaDataMap(argsClass).values().stream() .map(fieldMetaData -> newFieldInfo(argsClass, fieldMetaData)) .collect(toImmutableList()); if (resultClass != null) { // Function isn't "oneway" function final Map<? extends TFieldIdEnum, FieldMetaData> resultMetaData = FieldMetaData.getStructMetaDataMap(resultClass);
List<Field> fields = new ArrayList<>(); for (FieldMetaData f : FieldMetaData.getStructMetaDataMap((Class<? extends TBase>) c).values()) { Schema s = getSchema(f.valueMetaData); if (f.requirementType == TFieldRequirementType.OPTIONAL
FieldMetaData.getStructMetaDataMap((Class<? extends TBase<?, ?>>) clazz);
@VisibleForTesting static StructInfo newStructInfo(Class<? extends TBase<?, ?>> structClass) { final String name = structClass.getName(); final Map<?, FieldMetaData> metaDataMap = FieldMetaData.getStructMetaDataMap(structClass); final List<FieldInfo> fields = metaDataMap.values().stream() .map(fieldMetaData -> newFieldInfo(structClass, fieldMetaData)) .collect(Collectors.toList()); return new StructInfo(name, fields); }
@VisibleForTesting static StructInfo newStructInfo(Class<? extends TBase<?, ?>> structClass) { final String name = structClass.getName(); final Map<?, FieldMetaData> metaDataMap = FieldMetaData.getStructMetaDataMap(structClass); final List<FieldInfo> fields = metaDataMap.values().stream() .map(fieldMetaData -> newFieldInfo(structClass, fieldMetaData)) .collect(Collectors.toList()); return new StructInfo(name, fields); }
@VisibleForTesting static StructInfo newStructInfo(Class<? extends TBase<?, ?>> structClass) { final String name = structClass.getName(); final Map<?, FieldMetaData> metaDataMap = FieldMetaData.getStructMetaDataMap(structClass); final List<FieldInfo> fields = metaDataMap.values().stream() .map(fieldMetaData -> newFieldInfo(structClass, fieldMetaData)) .collect(Collectors.toList()); return new StructInfo(name, fields); }
public ThriftObsDescriptor(Class<? extends TBase> thriftClass) { Map<? extends TFieldIdEnum, FieldMetaData> mdm = FieldMetaData.getStructMetaDataMap(thriftClass); this.fields = Lists.newArrayListWithExpectedSize(mdm.size()); this.ids = Lists.newArrayListWithExpectedSize(mdm.size()); this.fieldNames = Maps.newHashMap(); for (Map.Entry<? extends TFieldIdEnum, FieldMetaData> e : mdm.entrySet()) { fields.add(new Field(e.getValue().fieldName, getFieldType(e.getValue()))); ids.add(e.getKey().getThriftFieldId()); fieldNames.put(e.getValue().fieldName, fields.size() - 1); } }
private void build(Class<? extends TBase<?, ?>> tClass) { Map<? extends TFieldIdEnum, FieldMetaData> fieldMap = FieldMetaData.getStructMetaDataMap(tClass); Field[] arr = new Field[fieldMap.size()]; isUnion = TUnion.class.isAssignableFrom(tClass); int idx = 0; for (Entry<? extends TFieldIdEnum, FieldMetaData> e : fieldMap.entrySet()) { String fieldName = e.getKey().getFieldName(); arr[idx++] = new Field(e.getKey(), e.getValue(), fieldName, ThriftUtils.getFieldType(tClass, fieldName)); } // make it immutable since users have access. fields = ImmutableList.copyOf(arr); }
private static RpcRequest toRpcRequest(Class<?> serviceType, String method, TBase<?, ?> thriftArgs) { requireNonNull(thriftArgs, "thriftArgs"); // NB: The map returned by FieldMetaData.getStructMetaDataMap() is an EnumMap, // so the parameter ordering is preserved correctly during iteration. final Set<? extends TFieldIdEnum> fields = FieldMetaData.getStructMetaDataMap(thriftArgs.getClass()).keySet(); // Handle the case where the number of arguments is 0 or 1. final int numFields = fields.size(); switch (numFields) { case 0: return RpcRequest.of(serviceType, method); case 1: return RpcRequest.of(serviceType, method, ThriftFieldAccess.get(thriftArgs, fields.iterator().next())); } // Handle the case where the number of arguments is greater than 1. final List<Object> list = new ArrayList<>(numFields); for (TFieldIdEnum field : fields) { list.add(ThriftFieldAccess.get(thriftArgs, field)); } return RpcRequest.of(serviceType, method, list); }
@SuppressWarnings("unchecked") final Map<TFieldIdEnum, FieldMetaData> metaDataMap = (Map<TFieldIdEnum, FieldMetaData>) FieldMetaData.getStructMetaDataMap(resultType);
private static RpcRequest toRpcRequest(Class<?> serviceType, String method, TBase<?, ?> thriftArgs) { requireNonNull(thriftArgs, "thriftArgs"); // NB: The map returned by FieldMetaData.getStructMetaDataMap() is an EnumMap, // so the parameter ordering is preserved correctly during iteration. final Set<? extends TFieldIdEnum> fields = FieldMetaData.getStructMetaDataMap(thriftArgs.getClass()).keySet(); // Handle the case where the number of arguments is 0 or 1. final int numFields = fields.size(); switch (numFields) { case 0: return RpcRequest.of(serviceType, method); case 1: return RpcRequest.of(serviceType, method, ThriftFieldAccess.get(thriftArgs, fields.iterator().next())); } // Handle the case where the number of arguments is greater than 1. final List<Object> list = new ArrayList<>(numFields); for (TFieldIdEnum field : fields) { list.add(ThriftFieldAccess.get(thriftArgs, field)); } return RpcRequest.of(serviceType, method, list); }
private static RpcRequest toRpcRequest(Class<?> serviceType, String method, TBase<?, ?> thriftArgs) { requireNonNull(thriftArgs, "thriftArgs"); // NB: The map returned by FieldMetaData.getStructMetaDataMap() is an EnumMap, // so the parameter ordering is preserved correctly during iteration. final Set<? extends TFieldIdEnum> fields = FieldMetaData.getStructMetaDataMap(thriftArgs.getClass()).keySet(); // Handle the case where the number of arguments is 0 or 1. final int numFields = fields.size(); switch (numFields) { case 0: return RpcRequest.of(serviceType, method); case 1: return RpcRequest.of(serviceType, method, ThriftFieldAccess.get(thriftArgs, fields.iterator().next())); } // Handle the case where the number of arguments is greater than 1. final List<Object> list = new ArrayList<>(numFields); for (TFieldIdEnum field : fields) { list.add(ThriftFieldAccess.get(thriftArgs, field)); } return RpcRequest.of(serviceType, method, list); }
FieldMetaData.getStructMetaDataMap(argsClass).values().stream() .map(fieldMetaData -> newFieldInfo(argsClass, fieldMetaData)) .collect(toImmutableList()); if (resultClass != null) { // Function isn't "oneway" function final Map<? extends TFieldIdEnum, FieldMetaData> resultMetaData = FieldMetaData.getStructMetaDataMap(resultClass);
@SuppressWarnings("unchecked") final Map<TFieldIdEnum, FieldMetaData> metaDataMap = (Map<TFieldIdEnum, FieldMetaData>) FieldMetaData.getStructMetaDataMap(resultType);
FieldMetaData.getStructMetaDataMap((Class<? extends TBase<?, ?>>) clazz);
@SuppressWarnings("unchecked") public P convert(T thriftObj) { Descriptor protoDesc = protoObj_.getDescriptorForType(); Map<? extends org.apache.thrift.TFieldIdEnum, FieldMetaData> fieldMap = FieldMetaData.getStructMetaDataMap(thriftObj.getClass()); for (Map.Entry<? extends TFieldIdEnum, FieldMetaData> e : fieldMap.entrySet()) { final TFieldIdEnum tFieldIdEnum = e.getKey(); final FieldValueMetaData thriftMetadata = e.getValue().valueMetaData; FieldDescriptor protoFieldDesc = protoDesc.findFieldByName(tFieldIdEnum.getFieldName()); if ( protoFieldDesc == null ) { throw new RuntimeException("Field " + tFieldIdEnum.getFieldName() + " not found in " + protoObj_.getClass().getCanonicalName()); } else if (!typesMatch(protoFieldDesc, thriftMetadata)) { throw new RuntimeException("Field " + tFieldIdEnum.getFieldName() + " type does not match: " + "thrift " + thriftMetadata.type + "vs " + protoFieldDesc.getType()); } Object fieldValue = thriftObj.getFieldValue(tFieldIdEnum); if (protoFieldDesc.getType() == Type.BYTES) { protoBuilder_.setField(protoFieldDesc, (ByteString.copyFrom((byte [])fieldValue))); } else { protoBuilder_.setField(protoFieldDesc, fieldValue); } } return (P) protoBuilder_.build(); }