public CoercionThriftCodec(ThriftCodec<?> codec, TypeCoercion typeCoercion) { this.codec = (ThriftCodec<Object>) codec; this.typeCoercion = typeCoercion; this.thriftType = typeCoercion.getThriftType(); }
@Override public T read(TProtocol protocol) throws Exception { Object thriftValue = codec.read(protocol); T javaValue = (T) typeCoercion.getFromThrift().invoke(null, thriftValue); return javaValue; }
@Override public void write(T javaValue, TProtocol protocol) throws Exception { Object thriftValue = typeCoercion.getToThrift().invoke(null, javaValue); codec.write(thriftValue, protocol); } }
coercionsClass.getName(), type); TypeCoercion coercion = new TypeCoercion(type, toThriftMethod, fromThriftMethod); coercions.put(type.getJavaType(), coercion);
coercionsClass.getName(), type); TypeCoercion coercion = new TypeCoercion(type, toThriftMethod, fromThriftMethod); coercions.put(type.getJavaType(), coercion);
return coercion.getThriftType().getProtocolType();
private static boolean needsCastAfterRead(ThriftFieldMetadata field, Method readMethod) { Class<?> methodReturn = readMethod.getReturnType(); Class<?> fieldType; if (field.getCoercion().isPresent()) { fieldType = field.getCoercion().get().getFromThrift().getParameterTypes()[0]; } else { fieldType = TypeToken.of(field.getThriftType().getJavaType()).getRawType(); } boolean needsCast = !fieldType.isAssignableFrom(methodReturn); return needsCast; }
write.invokeStatic(field.getCoercion().get().getToThrift());
if (elementType == byte.class) { return coercions.get(byte[].class).getThriftType(); return coercion.getThriftType();
read.invokeStatic(field.getCoercion().get().getFromThrift());
if (elementType == byte.class) { return coercions.get(javaType).getThriftType(); return coercion.getThriftType();
read.invokeStatic(field.getCoercion().get().getFromThrift());