private ThriftStructMetadata extractThriftStructMetadata(Type structType) { Preconditions.checkNotNull(structType, "structType is null"); Deque<Type> stack = this.stack.get(); if (stack.contains(structType)) { String path = Joiner.on("->").join(transform(concat(stack, ImmutableList.of(structType)), new Function<Type, Object>() { @Override public Object apply(Type input) { return TypeToken.of(input).getRawType().getName(); } })); throw new IllegalArgumentException("Circular references are not allowed: " + path); } stack.push(structType); try { ThriftStructMetadataBuilder builder = new ThriftStructMetadataBuilder(this, structType); ThriftStructMetadata structMetadata = builder.build(); return structMetadata; } finally { Type top = stack.pop(); checkState(structType.equals(top), "ThriftCatalog circularity detection stack is corrupt: expected %s, but got %s", structType, top); } }
ThriftStructMetadata structMetadata = builder.build(); return structMetadata;
@Test public void testNonFinalStructsOk() { ThriftStructMetadataBuilder builder = new ThriftStructMetadataBuilder(new ThriftCatalog(), NotFinalStruct.class); builder.build(); }
@Test public void testGenericBuilder() { Type structType = new TypeToken<GenericStruct<String>>() {}.getType(); ThriftStructMetadataBuilder builder = new ThriftStructMetadataBuilder(new ThriftCatalog(), structType); builder.build(); }
@Test(expectedExceptions = { MetadataErrorException.class }) public void testGenericBuilderForNonGenericStruct() { Type structType = new TypeToken<NonGenericStruct>() {}.getType(); ThriftStructMetadataBuilder builder = new ThriftStructMetadataBuilder(new ThriftCatalog(), structType); builder.build(); }
@Test public void testLegacyIdCorrectlyAnnotated() { ThriftStructMetadataBuilder builder = new ThriftStructMetadataBuilder(new ThriftCatalog(), LegacyIdCorrect.class); ThriftStructMetadata metadata = builder.build(); Set<Integer> seen = new HashSet<>(); for (ThriftFieldMetadata field : metadata.getFields()) { seen.add((int) field.getId()); } assertThat(seen) .as("fields found in LegacyIdCorrect") .isEqualTo(LegacyIdCorrect.IDS); }
@Test public void testMergeableRequiredness() { ThriftStructMetadata metadata = new ThriftStructMetadataBuilder(new ThriftCatalog(), MergeableRequiredness.class).build(); assertThat(metadata.getField(1).getRequiredness()) .as("requiredness of field 'foo'") .isEqualTo(Requiredness.OPTIONAL); }