private static NamedTypeInfo newNamedTypeInfo(TypeSignature typeSignature) { final Optional<Object> namedTypeDescriptor = typeSignature.namedTypeDescriptor(); if (!namedTypeDescriptor.isPresent()) { throw new IllegalArgumentException("cannot create a named type from: " + typeSignature); } final Class<?> type = (Class<?>) namedTypeDescriptor.get(); if (type.isEnum()) { @SuppressWarnings("unchecked") final Class<? extends Enum<?>> enumType = (Class<? extends Enum<?>>) type; return new EnumInfo(enumType); } if (TException.class.isAssignableFrom(type)) { @SuppressWarnings("unchecked") final Class<? extends TException> castType = (Class<? extends TException>) type; return newExceptionInfo(castType); } assert TBase.class.isAssignableFrom(type); @SuppressWarnings("unchecked") final Class<? extends TBase<?, ?>> castType = (Class<? extends TBase<?, ?>>) type; return newStructInfo(castType); }
@Test public void testNewStructInfoTest() throws Exception { final TypeSignature string = TypeSignature.ofBase("string"); final List<FieldInfo> fields = new ArrayList<>(); fields.add(new FieldInfo("boolVal", FieldRequirement.DEFAULT, TypeSignature.ofBase("bool"))); fields.add(new FieldInfo("byteVal", FieldRequirement.DEFAULT, TypeSignature.ofBase("i8"))); fields.add(new FieldInfo("i16Val", FieldRequirement.DEFAULT, TypeSignature.ofBase("i16"))); fields.add(new FieldInfo("i32Val", FieldRequirement.DEFAULT, TypeSignature.ofBase("i32"))); fields.add(new FieldInfo("i64Val", FieldRequirement.DEFAULT, TypeSignature.ofBase("i64"))); fields.add(new FieldInfo("doubleVal", FieldRequirement.DEFAULT, TypeSignature.ofBase("double"))); fields.add(new FieldInfo("stringVal", FieldRequirement.DEFAULT, string)); fields.add(new FieldInfo("binaryVal", FieldRequirement.DEFAULT, TypeSignature.ofBase("binary"))); fields.add(new FieldInfo("enumVal", FieldRequirement.DEFAULT, TypeSignature.ofNamed(FooEnum.class))); fields.add(new FieldInfo("unionVal", FieldRequirement.DEFAULT, TypeSignature.ofNamed(FooUnion.class))); fields.add(new FieldInfo("mapVal", FieldRequirement.DEFAULT, TypeSignature.ofMap(string, TypeSignature.ofNamed(FooEnum.class)))); fields.add(new FieldInfo("setVal", FieldRequirement.DEFAULT, TypeSignature.ofSet(FooUnion.class))); fields.add(new FieldInfo("listVal", FieldRequirement.DEFAULT, TypeSignature.ofList(string))); fields.add(new FieldInfo("selfRef", FieldRequirement.OPTIONAL, TypeSignature.ofNamed(FooStruct.class))); final StructInfo fooStruct = newStructInfo(FooStruct.class); assertThat(fooStruct).isEqualTo(new StructInfo(FooStruct.class.getName(), fields)); }
private static NamedTypeInfo newNamedTypeInfo(TypeSignature typeSignature) { final Class<?> type = (Class<?>) typeSignature.namedTypeDescriptor().get(); if (type.isEnum()) { return newEnumInfo(type); } if (TException.class.isAssignableFrom(type)) { @SuppressWarnings("unchecked") final Class<? extends TException> castType = (Class<? extends TException>) type; return newExceptionInfo(castType); } assert TBase.class.isAssignableFrom(type); @SuppressWarnings("unchecked") final Class<? extends TBase<?, ?>> castType = (Class<? extends TBase<?, ?>>) type; return newStructInfo(castType); }
private static NamedTypeInfo newNamedTypeInfo(TypeSignature typeSignature) { final Optional<Object> namedTypeDescriptor = typeSignature.namedTypeDescriptor(); if (!namedTypeDescriptor.isPresent()) { throw new IllegalArgumentException("cannot create a named type from: " + typeSignature); } final Class<?> type = (Class<?>) namedTypeDescriptor.get(); if (type.isEnum()) { @SuppressWarnings("unchecked") final Class<? extends Enum<?>> enumType = (Class<? extends Enum<?>>) type; return new EnumInfo(enumType); } if (TException.class.isAssignableFrom(type)) { @SuppressWarnings("unchecked") final Class<? extends TException> castType = (Class<? extends TException>) type; return newExceptionInfo(castType); } assert TBase.class.isAssignableFrom(type); @SuppressWarnings("unchecked") final Class<? extends TBase<?, ?>> castType = (Class<? extends TBase<?, ?>>) type; return newStructInfo(castType); }
private static NamedTypeInfo newNamedTypeInfo(TypeSignature typeSignature) { final Optional<Object> namedTypeDescriptor = typeSignature.namedTypeDescriptor(); if (!namedTypeDescriptor.isPresent()) { throw new IllegalArgumentException("cannot create a named type from: " + typeSignature); } final Class<?> type = (Class<?>) namedTypeDescriptor.get(); if (type.isEnum()) { @SuppressWarnings("unchecked") final Class<? extends Enum<?>> enumType = (Class<? extends Enum<?>>) type; return new EnumInfo(enumType); } if (TException.class.isAssignableFrom(type)) { @SuppressWarnings("unchecked") final Class<? extends TException> castType = (Class<? extends TException>) type; return newExceptionInfo(castType); } assert TBase.class.isAssignableFrom(type); @SuppressWarnings("unchecked") final Class<? extends TBase<?, ?>> castType = (Class<? extends TBase<?, ?>>) type; return newStructInfo(castType); }