private MethodSpec setter( NameAllocator nameAllocator, TypeName builderType, OneOf oneOf, Field field) { TypeName javaType = fieldType(field); String fieldName = nameAllocator.get(field); MethodSpec.Builder result = MethodSpec.methodBuilder(fieldName) .addModifiers(PUBLIC) .addParameter(javaType, fieldName) .returns(builderType); if (!field.documentation().isEmpty()) { result.addJavadoc("$L\n", sanitizeJavadoc(field.documentation())); } if (field.isDeprecated()) { result.addAnnotation(Deprecated.class); } if (field.isRepeated() || field.type().isMap()) { result.addStatement("$T.checkElementsNotNull($L)", Internal.class, fieldName); } result.addStatement("this.$L = $L", fieldName, fieldName); if (oneOf != null) { for (Field other : oneOf.fields()) { if (field != other) { result.addStatement("this.$L = null", nameAllocator.get(other)); } } } result.addStatement("return this"); return result.build(); }
@Test public void fieldIsDeprecated() throws Exception { Schema schema = new RepoBuilder() .add("message.proto", "" + "message Message {\n" + " optional int32 a = 1;\n" + " optional int32 b = 2 [deprecated=false];\n" + " optional int32 c = 3 [deprecated=true];\n" + "}\n") .schema(); MessageType message = (MessageType) schema.getType("Message"); assertThat(message.field("a").isDeprecated()).isFalse(); assertThat(message.field("b").isDeprecated()).isFalse(); assertThat(message.field("c").isDeprecated()).isTrue(); }
@Test public void specialOptionsNotPruned() throws Exception { Schema schema = new RepoBuilder() .add("message.proto", "" + "option java_package = \"p\";\n" + "\n" + "message Message {\n" + " optional int32 a = 1 [packed = true, deprecated = true, default = 5];\n" + "}\n" + "enum Enum {\n" + " option allow_alias = true;\n" + " A = 1;\n" + " B = 1;\n" + "}\n") .schema(); Schema pruned = schema.prune(new IdentifierSet.Builder() .exclude("google.protobuf.*") .build()); ProtoFile protoFile = pruned.protoFile("message.proto"); assertThat(protoFile.javaPackage()).isEqualTo("p"); MessageType message = (MessageType) pruned.getType("Message"); Field field = message.field("a"); assertThat(field.getDefault()).isEqualTo("5"); assertThat(field.isDeprecated()).isTrue(); assertThat(field.isPacked()).isTrue(); EnumType enumType = (EnumType) pruned.getType("Enum"); assertThat(enumType.allowAlias()).isTrue(); } }
fieldBuilder.addJavadoc("Extension source: $L\n", field.location().withPathOnly()); if (field.isDeprecated()) { fieldBuilder.addAnnotation(Deprecated.class);
private MethodSpec setter( NameAllocator nameAllocator, TypeName builderType, OneOf oneOf, Field field) { TypeName javaType = fieldType(field); String fieldName = nameAllocator.get(field); MethodSpec.Builder result = MethodSpec.methodBuilder(fieldName) .addModifiers(PUBLIC) .addParameter(javaType, fieldName) .returns(builderType); if (!field.documentation().isEmpty()) { result.addJavadoc("$L\n", sanitizeJavadoc(field.documentation())); } if (field.isDeprecated()) { result.addAnnotation(Deprecated.class); } if (field.isRepeated() || field.type().isMap()) { result.addStatement("$T.checkElementsNotNull($L)", Internal.class, fieldName); } result.addStatement("this.$L = $L", fieldName, fieldName); if (oneOf != null) { for (Field other : oneOf.fields()) { if (field != other) { result.addStatement("this.$L = null", nameAllocator.get(other)); } } } result.addStatement("return this"); return result.build(); }
fieldBuilder.addJavadoc("Extension source: $L\n", field.location().withPathOnly()); if (field.isDeprecated()) { fieldBuilder.addAnnotation(Deprecated.class);
fieldBuilder.addJavadoc("Extension source: $L\n", field.location().withoutBase()); if (field.isDeprecated()) { fieldBuilder.addAnnotation(Deprecated.class);