@Override public Object deserialize(Writable field) throws SerDeException { Object obj = super.deserialize(field); try { ((TBase) obj).read(inProtocol); } catch (Exception e) { throw new SerDeException(e); } return obj; }
boolean found = false; while (true) { TFieldIdEnum fieldIdEnum = resultObj.fieldForId(index++); if (fieldIdEnum == null) { break; Object realResult = result.getResult(); String fieldName = resultObj.fieldForId(0).getFieldName(); String setMethodName = ThriftUtils.generateSetMethodName(fieldName); String getMethodName = ThriftUtils.generateGetMethodName(fieldName); break; case TMessageType.REPLY: resultObj.write(protocol); break; default:
try { t = this._thriftClass.newInstance(); t.read(_binaryIn); } catch (Exception e) { throw new RuntimeException("Caught exception while serialize thrift instance", e); if (_fieldNameToIndexMap.containsKey(fieldName)) { int tFieldId = _fieldNameToIndexMap.get(fieldName); TFieldIdEnum tFieldIdEnum = t.fieldForId(tFieldId); Object thriftValue = t.getFieldValue(tFieldIdEnum); Object value = null; if (fieldSpec.isSingleValueField()) {
@Override protected Object getField(Object record, String name, int pos, Object state) { TFieldIdEnum f = ((TFieldIdEnum[])state)[pos]; TBase struct = (TBase)record; if (struct.isSet(f)) return struct.getFieldValue(f); return null; }
public static Map<String, Object> thriftToMap( org.apache.thrift.TBase thriftObj) { Map<String, Object> ret = new HashMap<>(); int i = 1; TFieldIdEnum field = thriftObj.fieldForId(i); while (field != null) { if (thriftObj.isSet(field)) { Object obj = thriftObj.getFieldValue(field); ret.put(field.getFieldName(), thriftToObject(obj)); } field = thriftObj.fieldForId(++i); } return ret; }
@Override public void serialize(TBase value, JsonGenerator gen, SerializerProvider provider) throws IOException { gen.writeRawValue(writeThriftObjectAsTText(protocol -> { try { value.write(protocol); } catch (TException ex) { throw new IllegalArgumentException(ex); } })); } }
TFieldIdEnum fieldIdEnum = resultObj.fieldForId(index++); if (fieldIdEnum == null) { break; if (causeException != null && getMethod.getReturnType().equals(causeException.getClass())) { found = true; resultObj.setFieldValue(fieldIdEnum, causeException); break; Object realResult = (response.getResult()).getReturnVal(); if (realResult != null) { resultObj.setFieldValue(resultObj.fieldForId(0), realResult); break; case TMessageType.REPLY: resultObj.write(protocol); break; default:
args.read(protocol); protocol.readMessageEnd(); } catch (TException e) { TFieldIdEnum fieldIdEnum = args.fieldForId(index++); result.read(protocol); protocol.readMessageEnd(); } catch (TException e) { TFieldIdEnum fieldIdEnum = result.fieldForId(index++);
/** * Gets the value of the specified struct field. */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static Object get(TBase<?, ?> struct, TFieldIdEnum field) { final Object value = ((TBase) struct).getFieldValue(field); if (value instanceof byte[]) { return ByteBuffer.wrap((byte[]) value); } else { return value; } }
/** * Returns a new empty result instance. */ public TBase<?, ?> newResult() { assert result != null; return result.deepCopy(); }
/** * Sets the value of the specified struct field. */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static void set(TBase<?, ?> struct, TFieldIdEnum field, Object value) { ((TBase) struct).setFieldValue(field, value); }
/** * Tells whether the specified struct field is set or not. */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static boolean isSet(TBase<?, ?> struct, TFieldIdEnum field) { return ((TBase) struct).isSet(field); }
public ThriftRecordReader(File dataFile, Schema schema, ThriftRecordReaderConfig recordReaderConfig) throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException { this._schema = schema; this._dataFile = dataFile; this._recordReaderConfig = recordReaderConfig; this._thriftClass = initThriftInstanceCreator(); this._bufferIn = RecordReaderUtils.getFileBufferStream(dataFile); this._binaryIn = new TBinaryProtocol(new TIOStreamTransport(_bufferIn)); this._fieldNameToIndexMap = new HashMap(); TBase t = this._thriftClass.newInstance(); int index = 1; TFieldIdEnum fieldIdEnum = null; do { fieldIdEnum = t.fieldForId(index); if (fieldIdEnum != null) { _fieldNameToIndexMap.put(fieldIdEnum.getFieldName(), index); } index = index + 1; } while (fieldIdEnum != null); }
public byte[] toBytes(T object) { MemoryBuffer buffer = new MemoryBuffer(); TProtocol protocol = createThriftProtocol(buffer); try { object.write(protocol); } catch(TException e) { throw new SerializationException(e); } return buffer.toByteArray(); }
private byte[] doSerializeRequest(DefaultRequest request) throws Exception { RpcInvocation rpcInvocation = request.getData(); TMessage message = new TMessage(rpcInvocation.getMethod().getName(), TMessageType.CALL, request.getSeqToInt()); String argsClassName = ThriftUtil.generateArgsClassName( request.getServiceInterface().getName(), rpcInvocation.getMethod().getName()); TBase args = getClazzInstance(argsClassName); Object[] arguments = rpcInvocation.getArguments(); if (arguments != null) { for (int i = 0; i < arguments.length; i++) { if (arguments[i] != null) { args.setFieldValue(args.fieldForId(i + 1), arguments[i]); } } } TMemoryBuffer transport = new TMemoryBuffer(1024); TBinaryProtocol protocol = new TBinaryProtocol(transport); protocol.writeMessageBegin(message); if (!request.isOctoProtocol()) { // 不影响原生thrift解码 RequestHeader requestHeader = MetaUtil.convertRequestToOldProtocolHeader(request); protocol.writeFieldBegin(MTRACE_FIELD_DESC); requestHeader.write(protocol); protocol.writeFieldEnd(); } args.write(protocol); protocol.writeMessageEnd(); protocol.getTransport().flush(); return transport.getArray(); }
args.read(protocol); protocol.readMessageEnd(); } catch (TException e) { TFieldIdEnum fieldIdEnum = args.fieldForId(index++); result.read(protocol); protocol.readMessageEnd(); } catch (TException e) { TFieldIdEnum fieldIdEnum = result.fieldForId(index++);
Object getFieldValue(final int i, TBase tBase) { return tBase.getFieldValue(new TFieldIdEnum() { @Override public short getThriftFieldId() { return ids.get(i); } @Override public String getFieldName() { return fields.get(i).name; } }); }
private static Object deepCopyObject(Object o) { if (o instanceof TBase) { return ((TBase)o).deepCopy(); } else if (o instanceof ByteBuffer) { return TBaseHelper.copyBinary((ByteBuffer)o); } else if (o instanceof List) { return deepCopyList((List)o); } else if (o instanceof Set) { return deepCopySet((Set)o); } else if (o instanceof Map) { return deepCopyMap((Map)o); } else { return o; } }
@Override protected void setField(Object r, String n, int pos, Object v, Object state) { if (v == null && r instanceof TUnion) return; ((TBase)r).setFieldValue(((TFieldIdEnum[])state)[pos], v); }
/** * Tells whether the specified struct field is set or not. */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static boolean isSet(TBase<?, ?> struct, TFieldIdEnum field) { return ((TBase) struct).isSet(field); }