public ThriftStructTransformer(Class<L> left, Class<R> right) { this.leftMetadata= CATALOG.getThriftStructMetadata(checkNotNull(left,"left is null")); this.rightMetadata= CATALOG.getThriftStructMetadata(checkNotNull(right,"right is null")); } @Override
public ThriftCodecManager(ThriftCodecFactory factory, Set<ThriftCodec<?>> codecs) { this(factory, new ThriftCatalog(), codecs); }
public ThriftCodec<?> getCodec(Type javaType) { ThriftType thriftType = catalog.getThriftType(javaType); Preconditions.checkArgument(thriftType != null, "Unsupported java type %s", javaType); return getCodec(thriftType); }
/** * Gets the ThriftStructMetadata for the specified struct class. The struct class must be * annotated with @ThriftStruct or @ThriftUnion. */ public <T> ThriftStructMetadata getThriftStructMetadata(Type structType) { ThriftStructMetadata structMetadata = structs.get(structType); Class<?> structClass = TypeToken.of(structType).getRawType(); if (structMetadata == null) { if (structClass.isAnnotationPresent(ThriftStruct.class)) { structMetadata = extractThriftStructMetadata(structType); } else if (structClass.isAnnotationPresent(ThriftUnion.class)) { structMetadata = extractThriftUnionMetadata(structType); } else { throw new IllegalStateException("getThriftStructMetadata called on a class that has no @ThriftStruct or @ThriftUnion annotation"); } ThriftStructMetadata current = structs.putIfAbsent(structType, structMetadata); if (current != null) { structMetadata = current; } } return structMetadata; }
ThriftEnumMetadata<? extends Enum<?>> thriftEnumMetadata = getThriftEnumMetadata(rawType); return enumType(thriftEnumMetadata); return array(getCollectionElementThriftTypeReference(elementType)); return map(getMapKeyThriftTypeReference(mapKeyType), getMapValueThriftTypeReference(mapValueType)); return set(getCollectionElementThriftTypeReference(elementType)); return list(getCollectionElementThriftTypeReference(elementType)); return VOID; if (isStructType(rawType)) { ThriftStructMetadata structMetadata = getThriftStructMetadata(javaType); return getThriftType(returnType);
ThriftEnumMetadata<? extends Enum<?>> thriftEnumMetadata = getThriftEnumMetadata(enumClass); return enumType(thriftEnumMetadata); return array(getThriftType(elementType)); return map(getThriftType(mapKeyType), getThriftType(mapValueType)); return set(getThriftType(elementType)); return list(getThriftType(elementType)); ThriftStructMetadata structMetadata = getThriftStructMetadata(javaType); return struct(structMetadata); ThriftStructMetadata structMetadata = getThriftStructMetadata(javaType); return getThriftType(returnType);
@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()); }
@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)); }
name = fieldMetadata.getName(); requiredness = fieldMetadata.getRequiredness(); type = catalog.getThriftType(fieldMetadata.getJavaType()); coercion = catalog.getDefaultCoercion(type.getJavaType());
for (Method method : coercionsClass.getDeclaredMethods()) { if (method.isAnnotationPresent(ToThrift.class)) { verifyCoercionMethod(method); ThriftType thriftType = getThriftType(method.getGenericReturnType()); ThriftType coercedType = thriftType.coerceTo(method.getGenericParameterTypes()[0]); verifyCoercionMethod(method); 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); } } }
@SuppressWarnings("PMD.EmptyCatchBlock") public static ImmutableList<String> getThriftDocumentation(Class<?> objectClass) { ThriftDocumentation documentation = objectClass.getAnnotation(ThriftDocumentation.class); if (documentation == null) { try { Class<?> swiftDocsClass = getSwiftMetaClassOf(objectClass); documentation = swiftDocsClass.getAnnotation(ThriftDocumentation.class); } catch (ClassNotFoundException e) { // ignored } } return documentation == null ? ImmutableList.<String>of() : ImmutableList.copyOf(documentation.value()); }
protected AbstractThriftMetadataBuilder(ThriftCatalog catalog, Type structType) { this.catalog = checkNotNull(catalog, "catalog is null"); this.structType = checkNotNull(structType, "structType is null"); this.metadataErrors = new MetadataErrors(catalog.getMonitor()); // assign the struct name from the annotation or from the Java class structName = extractName(); // get the builder type from the annotation or from the Java class builderType = extractBuilderType(); // grab any documentation from the annotation or saved JavaDocs documentation = ThriftCatalog.getThriftDocumentation(getStructClass()); // extract all of the annotated constructor and report an error if // there is more than one or none // also extract thrift fields from the annotated parameters and verify extractFromConstructors(); // extract thrift fields from the annotated fields and verify extractFromFields(); // extract thrift fields from the annotated methods (and parameters) and verify extractFromMethods(); }
@VisibleForTesting public ThriftCatalog(Monitor monitor) { this.monitor = monitor; addDefaultCoercions(DefaultJavaCoercions.class); }
@SuppressWarnings("PMD.EmptyCatchBlock") public static <T extends Enum<T>> ImmutableList<String> getThriftDocumentation(Enum<T> enumConstant) { try { Field f = enumConstant.getDeclaringClass().getField(enumConstant.name()); return getThriftDocumentation(f); } catch (ReflectiveOperationException e) { // ignore } return ImmutableList.<String>of(); }
idlAnnotations = fieldMetadata.getIdlAnnotations(); fieldType = fieldMetadata.getType(); thriftTypeReference = catalog.getFieldThriftTypeReference(fieldMetadata); coercion = catalog.getDefaultCoercion(thriftTypeReference.get().getJavaType());
if (type.isCoerced()) { ThriftCodec<?> codec = getCodec(type.getUncoercedType()); TypeCoercion coercion = catalog.getDefaultCoercion(type.getJavaType()); return new CoercionThriftCodec<>(codec, coercion);
@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()); }
requiredness = fieldMetadata.getRequiredness(); fieldType = fieldMetadata.getType(); thriftType = catalog.getThriftType(fieldMetadata.getJavaType()); coercion = catalog.getDefaultCoercion(thriftType.getJavaType());
for (Method method : coercionsClass.getDeclaredMethods()) { if (method.isAnnotationPresent(ToThrift.class)) { verifyCoercionMethod(method); ThriftType thriftType = getThriftType(method.getGenericReturnType()); ThriftType coercedType = thriftType.coerceTo(method.getGenericParameterTypes()[0]); verifyCoercionMethod(method); ThriftType thriftType = getThriftType(method.getGenericParameterTypes()[0]); ThriftType coercedType = thriftType.coerceTo(method.getGenericReturnType());