public ThriftCodec<?> getCodec(Type javaType) { ThriftType thriftType = catalog.getThriftType(javaType); Preconditions.checkArgument(thriftType != null, "Unsupported java type %s", javaType); return getCodec(thriftType); }
public <T> ThriftCodec<T> getCachedCodecIfPresent(Class<T> javaType) { ThriftType thriftType = catalog.getThriftType(javaType); Preconditions.checkArgument(thriftType != null, "Unsupported java type %s", javaType.getName()); return (ThriftCodec<T>) getCachedCodecIfPresent(thriftType); }
public <T> ThriftCodec<T> getCodec(Class<T> javaType) { ThriftType thriftType = catalog.getThriftType(javaType); Preconditions.checkArgument(thriftType != null, "Unsupported java type %s", javaType.getName()); return (ThriftCodec<T>) getCodec(thriftType); }
public ThriftCodec<?> getCachedCodecIfPresent(Type javaType) { ThriftType thriftType = catalog.getThriftType(javaType); Preconditions.checkArgument(thriftType != null, "Unsupported java type %s", javaType); return getCachedCodecIfPresent(thriftType); }
if (method.isAnnotationPresent(ToThrift.class)) { verifyCoercionMethod(method); ThriftType thriftType = getThriftType(method.getGenericReturnType()); ThriftType coercedType = thriftType.coerceTo(method.getGenericParameterTypes()[0]); ThriftType thriftType = getThriftType(method.getGenericParameterTypes()[0]); ThriftType coercedType = thriftType.coerceTo(method.getGenericReturnType());
if (method.isAnnotationPresent(ToThrift.class)) { verifyCoercionMethod(method); ThriftType thriftType = getThriftType(method.getGenericReturnType()); ThriftType coercedType = thriftType.coerceTo(method.getGenericParameterTypes()[0]); ThriftType thriftType = getThriftType(method.getGenericParameterTypes()[0]); ThriftType coercedType = thriftType.coerceTo(method.getGenericReturnType());
/** * Verifies that the the fields all have a supported Java type and that all fields map to the * exact same ThriftType. */ protected final void verifyFieldType(short id, String name, Collection<FieldMetadata> fields, ThriftCatalog catalog) { boolean isSupportedType = true; for (FieldMetadata field : fields) { if (!catalog.isSupportedStructFieldType(field.getJavaType())) { metadataErrors.addError("Thrift class '%s' field '%s(%s)' type '%s' is not a supported Java type", structName, name, id, TypeToken.of(field.getJavaType())); isSupportedType = false; // only report the error once break; } } // fields must have the same type if (isSupportedType) { Set<ThriftType> types = new HashSet<>(); for (FieldMetadata field : fields) { types.add(catalog.getThriftType(field.getJavaType())); } if (types.size() > 1) { metadataErrors.addError("Thrift class '%s' field '%s(%s)' has multiple types: %s", structName, name, id, types); } } }
return array(getThriftType(elementType)); return map(getThriftType(mapKeyType), getThriftType(mapValueType)); return set(getThriftType(elementType)); return list(getThriftType(elementType)); return getThriftType(returnType);
private Object convertToThrift(Class<?> cls) { Set<ThriftService> serviceAnnotations = ReflectionHelper.getEffectiveClassAnnotations(cls, ThriftService.class); if (!serviceAnnotations.isEmpty()) { // it's a service ThriftServiceMetadata serviceMetadata = new ThriftServiceMetadata(cls, codecManager.getCatalog()); if (verbose) { LOG.info("Found thrift service: %s", cls.getSimpleName()); } return serviceMetadata; } else { // it's a type (will throw if it's not) ThriftType thriftType = codecManager.getCatalog().getThriftType(cls); if (verbose) { LOG.info("Found thrift type: %s", thriftTypeRenderer.toString(thriftType)); } return thriftType; } }
private <T> void testRoundTripSerialize(T value, TProtocolFactory protocolFactory) throws Exception { testRoundTripSerialize(codecManager.getCatalog().getThriftType(value.getClass()), value, new TCompactProtocol.Factory()); }
return getThriftType(returnType);
@Test public void testArraysManual() throws Exception { ThriftCatalog catalog = new ThriftCatalog(); ThriftType thriftType = catalog.getThriftType(ArrayField.class); ArrayFieldThriftCodec arrayFieldCodec = new ArrayFieldThriftCodec(thriftType); // manual codec does not implement the Map fields ArrayField arrayField = new ArrayField( new boolean[] {true, false, false, true}, new short[] {0, 1, 2, 3}, new int[] {10, 11, 12, 13}, new long[] {20, Long.MAX_VALUE, Long.MIN_VALUE}, new double[] {3.0, Double.MAX_VALUE, Double.MIN_VALUE}, "hello".getBytes(UTF_8)); testRoundTripSerialize(arrayFieldCodec, arrayFieldCodec, arrayField, new TCompactProtocol.Factory()); }
@Test public void testStructFieldsManual() throws Exception { ThriftCatalog catalog = new ThriftCatalog(); ThriftType bonkFieldType = catalog.getThriftType(BonkField.class); BonkFieldThriftCodec bonkFieldCodec = new BonkFieldThriftCodec(bonkFieldType); BonkField bonkField = new BonkField("message", 42); testRoundTripSerialize(bonkFieldCodec, bonkFieldCodec, bonkField, new TCompactProtocol.Factory()); }
ThriftType bonkFieldType = catalog.getThriftType(BonkField.class); ThriftType unionFieldType = catalog.getThriftType(UnionField.class); ThriftType fruitType = catalog.getThriftType(Fruit.class); UnionFieldThriftCodec unionFieldCodec = new UnionFieldThriftCodec(unionFieldType, fruitCodec); ThriftType oneOfEverythingType = catalog.getThriftType(OneOfEverything.class);
@Test public void testMatchByJavaNameWithThriftNameOverride() throws Exception { ThriftCatalog catalog = readCodecManager.getCatalog(); ThriftType thriftType = catalog.getThriftType(BonkConstructorNameOverride.class); ThriftStructMetadata structMetadata = thriftType.getStructMetadata(); assertEquals(structMetadata.getField(1).getName(), "myMessage"); assertEquals(structMetadata.getField(2).getName(), "myType"); BonkConstructorNameOverride bonk = new BonkConstructorNameOverride("message", 42); testRoundTripSerialize(bonk, new TCompactProtocol.Factory()); }
@BeforeMethod protected void setUp() throws Exception { codecManager = new ThriftCodecManager(new ThriftCodecFactory() { @Override public ThriftCodec<?> generateThriftTypeCodec(ThriftCodecManager codecManager, ThriftStructMetadata metadata) { throw new UnsupportedOperationException(); } }); ThriftCatalog catalog = codecManager.getCatalog(); catalog.addDefaultCoercions(DefaultJavaCoercions.class); ThriftType fruitType = catalog.getThriftType(Fruit.class); codecManager.addCodec(new EnumThriftCodec<Fruit>(fruitType)); }
@Test public void testAddStructCodec() throws Exception { BonkField bonk = new BonkField("message", 42); // no codec for BonkField so this will fail try { testRoundTripSerialize(bonk); fail("Expected exception"); } catch (Exception ignored) { } // add the codec ThriftType type = codecManager.getCatalog().getThriftType(BonkField.class); codecManager.addCodec(new BonkFieldThriftCodec(type)); // try again testRoundTripSerialize(bonk); testRoundTripSerialize(bonk, new TJSONProtocol.Factory()); }
@Test public void testUnionFieldsManual() throws Exception { ThriftCatalog catalog = new ThriftCatalog(); ThriftType unionFieldType = catalog.getThriftType(UnionField.class); ThriftType fruitType = catalog.getThriftType(Fruit.class); ThriftCodec<Fruit> fruitCodec = new EnumThriftCodec<Fruit>(fruitType); UnionFieldThriftCodec unionFieldCodec = new UnionFieldThriftCodec(unionFieldType, fruitCodec); UnionField unionField = new UnionField(); unionField._id = 1; unionField.stringValue = "Hello, World"; testRoundTripSerialize(unionFieldCodec, unionFieldCodec, unionField, new TCompactProtocol.Factory()); unionField = new UnionField(); unionField._id = 2; unionField.longValue = 4815162342L; testRoundTripSerialize(unionFieldCodec, unionFieldCodec, unionField, new TCompactProtocol.Factory()); unionField = new UnionField(); unionField._id = 3; unionField.fruitValue = Fruit.APPLE; // The best fruit! testRoundTripSerialize(unionFieldCodec, unionFieldCodec, unionField, new TCompactProtocol.Factory()); }
name = fieldMetadata.getName(); requiredness = fieldMetadata.getRequiredness(); type = catalog.getThriftType(fieldMetadata.getJavaType());
@Test public void testAddUnionCodec() throws Exception { UnionField union = new UnionField(); union._id= 1; union.stringValue = "Hello, World"; // no codec for UnionField so this will fail try { testRoundTripSerialize(union); fail("Expected exception"); } catch (Exception ignored) { } // add the codec ThriftType type = codecManager.getCatalog().getThriftType(UnionField.class); codecManager.addCodec(new UnionFieldThriftCodec(type, codecManager.getCodec(Fruit.class))); // try again testRoundTripSerialize(union); union = new UnionField(); union._id = 2; union.longValue = 4815162342L; testRoundTripSerialize(union); union = new UnionField(); union._id = 3; union.fruitValue = Fruit.BANANA; testRoundTripSerialize(union); testRoundTripSerialize(union, new TJSONProtocol.Factory()); }