Skip to content

Commit

Permalink
Use Editions features in Java full runtimes.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 575230560
  • Loading branch information
zhangskz committed Dec 27, 2023
1 parent 19c800c commit 1c55877
Show file tree
Hide file tree
Showing 15 changed files with 662 additions and 203 deletions.
2 changes: 2 additions & 0 deletions conformance/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ java_binary(
"//:protobuf_java_util",
"//:test_messages_proto2_java_proto",
"//:test_messages_proto3_java_proto",
"//src/google/protobuf/editions:test_messages_proto2_editions_java_proto",
"//src/google/protobuf/editions:test_messages_proto3_editions_java_proto",
],
)

Expand Down
133 changes: 69 additions & 64 deletions conformance/ConformanceJava.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.google.protobuf.ByteString;
import com.google.protobuf.CodedInputStream;
import com.google.protobuf.ExtensionRegistry;
import com.google.protobuf.GeneratedMessage;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.Parser;
import com.google.protobuf.TextFormat;
Expand Down Expand Up @@ -205,42 +206,63 @@ private <T extends AbstractMessage> T parseBinary(
return messages.get(0);
}

private Class<? extends AbstractMessage> createTestMessage(String messageType) {
if (messageType.equals("protobuf_test_messages.proto3.TestAllTypesProto3")) {
return TestAllTypesProto3.class;
} else if (messageType.equals("protobuf_test_messages.proto2.TestAllTypesProto2")) {
return TestAllTypesProto2.class;
} else if (messageType.equals("protobuf_test_messages.editions.proto3.TestAllTypesProto3")) {
return com.google.protobuf_test_messages.editions.proto3.TestMessagesProto3Editions
.TestAllTypesProto3.class;
} else if (messageType.equals("protobuf_test_messages.editions.proto2.TestAllTypesProto2")) {
return com.google.protobuf_test_messages.editions.proto2.TestMessagesProto2Editions
.TestAllTypesProto2.class;
} else {
throw new IllegalArgumentException(
"Protobuf request has unexpected payload type: " + messageType);
}
}

private Class<?> createTestFile(String messageType) {
if (messageType.equals("protobuf_test_messages.proto3.TestAllTypesProto3")) {
return TestMessagesProto3.class;
} else if (messageType.equals("protobuf_test_messages.proto2.TestAllTypesProto2")) {
return TestMessagesProto2.class;
} else if (messageType.equals("protobuf_test_messages.editions.proto3.TestAllTypesProto3")) {
return com.google.protobuf_test_messages.editions.proto3.TestMessagesProto3Editions.class;
} else if (messageType.equals("protobuf_test_messages.editions.proto2.TestAllTypesProto2")) {
return com.google.protobuf_test_messages.editions.proto2.TestMessagesProto2Editions.class;
} else {
throw new IllegalArgumentException(
"Protobuf request has unexpected payload type: " + messageType);
}
}

@SuppressWarnings("unchecked")
private Conformance.ConformanceResponse doTest(Conformance.ConformanceRequest request) {
AbstractMessage testMessage;
String messageType = request.getMessageType();
boolean isProto3 = messageType.equals("protobuf_test_messages.proto3.TestAllTypesProto3");
boolean isProto2 = messageType.equals("protobuf_test_messages.proto2.TestAllTypesProto2");

switch (request.getPayloadCase()) {
case PROTOBUF_PAYLOAD:
{
if (isProto3) {
try {
ExtensionRegistry extensions = ExtensionRegistry.newInstance();
TestMessagesProto3.registerAllExtensions(extensions);
testMessage =
parseBinary(
request.getProtobufPayload(), TestAllTypesProto3.parser(), extensions);
} catch (InvalidProtocolBufferException e) {
return Conformance.ConformanceResponse.newBuilder()
.setParseError(e.getMessage())
.build();
}
} else if (isProto2) {
try {
ExtensionRegistry extensions = ExtensionRegistry.newInstance();
TestMessagesProto2.registerAllExtensions(extensions);
testMessage =
parseBinary(
request.getProtobufPayload(), TestAllTypesProto2.parser(), extensions);
} catch (InvalidProtocolBufferException e) {
return Conformance.ConformanceResponse.newBuilder()
.setParseError(e.getMessage())
.build();
}
} else {
throw new IllegalArgumentException(
"Protobuf request has unexpected payload type: " + messageType);
try {
ExtensionRegistry extensions = ExtensionRegistry.newInstance();
createTestFile(messageType)
.getMethod("registerAllExtensions", ExtensionRegistry.class)
.invoke(null, extensions);
testMessage =
parseBinary(
request.getProtobufPayload(),
(Parser<GeneratedMessage>)
createTestMessage(messageType).getMethod("parser").invoke(null),
extensions);
} catch (InvalidProtocolBufferException e) {
return Conformance.ConformanceResponse.newBuilder()
.setParseError(e.getMessage())
.build();
} catch (Exception e) {
throw new RuntimeException(e);
}
break;
}
Expand All @@ -252,54 +274,34 @@ private Conformance.ConformanceResponse doTest(Conformance.ConformanceRequest re
== Conformance.TestCategory.JSON_IGNORE_UNKNOWN_PARSING_TEST) {
parser = parser.ignoringUnknownFields();
}
if (isProto3) {
TestMessagesProto3.TestAllTypesProto3.Builder builder =
TestMessagesProto3.TestAllTypesProto3.newBuilder();
parser.merge(request.getJsonPayload(), builder);
testMessage = builder.build();
} else if (isProto2) {
TestMessagesProto2.TestAllTypesProto2.Builder builder =
TestMessagesProto2.TestAllTypesProto2.newBuilder();
parser.merge(request.getJsonPayload(), builder);
testMessage = builder.build();
} else {
throw new IllegalArgumentException(
"Protobuf request has unexpected payload type: " + messageType);
}
GeneratedMessage.ExtendableMessage.Builder<?> builder =
(GeneratedMessage.ExtendableMessage.Builder<?>)
createTestMessage(messageType).getMethod("newBuilder").invoke(null);
parser.merge(request.getJsonPayload(), builder);
testMessage = (AbstractMessage) builder.build();
} catch (InvalidProtocolBufferException e) {
return Conformance.ConformanceResponse.newBuilder()
.setParseError(e.getMessage())
.build();
} catch (Exception e) {
throw new RuntimeException(e);
}
break;
}
case TEXT_PAYLOAD:
{
if (isProto3) {
try {
TestMessagesProto3.TestAllTypesProto3.Builder builder =
TestMessagesProto3.TestAllTypesProto3.newBuilder();
TextFormat.merge(request.getTextPayload(), builder);
testMessage = builder.build();
} catch (TextFormat.ParseException e) {
return Conformance.ConformanceResponse.newBuilder()
.setParseError(e.getMessage())
.build();
}
} else if (isProto2) {
try {
TestMessagesProto2.TestAllTypesProto2.Builder builder =
TestMessagesProto2.TestAllTypesProto2.newBuilder();
TextFormat.merge(request.getTextPayload(), builder);
testMessage = builder.build();
try {
GeneratedMessage.ExtendableMessage.Builder<?> builder =
(GeneratedMessage.ExtendableMessage.Builder<?>)
createTestMessage(messageType).getMethod("newBuilder").invoke(null);
TextFormat.merge(request.getTextPayload(), builder);
testMessage = (AbstractMessage) builder.build();
} catch (TextFormat.ParseException e) {
return Conformance.ConformanceResponse.newBuilder()
.setParseError(e.getMessage())
.build();
}
} else {
throw new IllegalArgumentException(
"Protobuf request has unexpected payload type: " + messageType);
} catch (Exception e) {
throw new RuntimeException(e);
}
break;
}
Expand Down Expand Up @@ -378,6 +380,9 @@ public void run() throws Exception {
typeRegistry =
TypeRegistry.newBuilder()
.add(TestMessagesProto3.TestAllTypesProto3.getDescriptor())
.add(
com.google.protobuf_test_messages.editions.proto3.TestMessagesProto3Editions
.TestAllTypesProto3.getDescriptor())
.build();
while (doTestIo()) {
this.testCount++;
Expand Down
39 changes: 39 additions & 0 deletions conformance/failure_list_java.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,42 @@ Required.Proto3.JsonInput.Int32FieldPlusSign
Required.Proto3.JsonInput.RepeatedFieldWrongElementTypeExpectingStringsGotBool
Required.Proto3.JsonInput.RepeatedFieldWrongElementTypeExpectingStringsGotInt
Required.Proto3.JsonInput.StringFieldNotAString
Recommended.Editions_Proto3.FieldMaskNumbersDontRoundTrip.JsonOutput
Recommended.Editions_Proto3.FieldMaskPathsDontRoundTrip.JsonOutput
Recommended.Editions_Proto3.FieldMaskTooManyUnderscore.JsonOutput
Recommended.Editions_Proto3.JsonInput.BoolFieldAllCapitalFalse
Recommended.Editions_Proto3.JsonInput.BoolFieldAllCapitalTrue
Recommended.Editions_Proto3.JsonInput.BoolFieldCamelCaseFalse
Recommended.Editions_Proto3.JsonInput.BoolFieldCamelCaseTrue
Recommended.Editions_Proto3.JsonInput.BoolFieldDoubleQuotedFalse
Recommended.Editions_Proto3.JsonInput.BoolFieldDoubleQuotedTrue
Recommended.Editions_Proto3.JsonInput.BoolMapFieldKeyNotQuoted
Recommended.Editions_Proto3.JsonInput.DoubleFieldInfinityNotQuoted
Recommended.Editions_Proto3.JsonInput.DoubleFieldNanNotQuoted
Recommended.Editions_Proto3.JsonInput.DoubleFieldNegativeInfinityNotQuoted
Recommended.Editions_Proto3.JsonInput.FieldMaskInvalidCharacter
Recommended.Editions_Proto3.JsonInput.FieldNameDuplicate
Recommended.Editions_Proto3.JsonInput.FieldNameNotQuoted
Recommended.Editions_Proto3.JsonInput.FloatFieldInfinityNotQuoted
Recommended.Editions_Proto3.JsonInput.FloatFieldNanNotQuoted
Recommended.Editions_Proto3.JsonInput.FloatFieldNegativeInfinityNotQuoted
Recommended.Editions_Proto3.JsonInput.Int32MapFieldKeyNotQuoted
Recommended.Editions_Proto3.JsonInput.Int64MapFieldKeyNotQuoted
Recommended.Editions_Proto3.JsonInput.JsonWithComments
Recommended.Editions_Proto3.JsonInput.StringFieldSingleQuoteBoth
Recommended.Editions_Proto3.JsonInput.StringFieldSingleQuoteKey
Recommended.Editions_Proto3.JsonInput.StringFieldSingleQuoteValue
Recommended.Editions_Proto3.JsonInput.StringFieldSurrogateInWrongOrder
Recommended.Editions_Proto3.JsonInput.StringFieldUnpairedHighSurrogate
Recommended.Editions_Proto3.JsonInput.StringFieldUnpairedLowSurrogate
Recommended.Editions_Proto3.JsonInput.Uint32MapFieldKeyNotQuoted
Recommended.Editions_Proto3.JsonInput.Uint64MapFieldKeyNotQuoted
Recommended.Editions_Proto2.JsonInput.FieldNameExtension.Validator
Required.Editions_Proto3.JsonInput.EnumFieldNotQuoted
Required.Editions_Proto3.JsonInput.Int32FieldLeadingZero
Required.Editions_Proto3.JsonInput.Int32FieldNegativeWithLeadingZero
Required.Editions_Proto3.JsonInput.Int32FieldPlusSign
Required.Editions_Proto3.JsonInput.RepeatedFieldWrongElementTypeExpectingStringsGotBool
Required.Editions_Proto3.JsonInput.RepeatedFieldWrongElementTypeExpectingStringsGotInt
Required.Editions_Proto3.JsonInput.StringFieldNotAString

8 changes: 8 additions & 0 deletions conformance/text_format_failure_list_java.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ Recommended.Proto3.ProtobufInput.RepeatedUnknownFields_Drop.TextFormatOutput
Recommended.Proto3.ProtobufInput.ScalarUnknownFields_Drop.TextFormatOutput
Required.Proto3.TextFormatInput.AnyField.ProtobufOutput
Required.Proto3.TextFormatInput.AnyField.TextFormatOutput
Recommended.Editions_Proto3.ProtobufInput.GroupUnknownFields_Drop.TextFormatOutput
Recommended.Editions_Proto3.ProtobufInput.MessageUnknownFields_Drop.TextFormatOutput
Recommended.Editions_Proto3.ProtobufInput.RepeatedUnknownFields_Drop.TextFormatOutput
Recommended.Editions_Proto3.ProtobufInput.ScalarUnknownFields_Drop.TextFormatOutput
Required.Editions_Proto3.TextFormatInput.AnyField.ProtobufOutput
Required.Editions_Proto3.TextFormatInput.AnyField.TextFormatOutput

Required.Proto3.TextFormatInput.StringFieldBadUTF8Hex
Required.Proto3.TextFormatInput.StringFieldBadUTF8Octal
Required.Editions_Proto3.TextFormatInput.StringFieldBadUTF8Hex
Required.Editions_Proto3.TextFormatInput.StringFieldBadUTF8Octal
33 changes: 33 additions & 0 deletions java/core/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ load("//:protobuf_version.bzl", "PROTOBUF_JAVA_VERSION")
load("//build_defs:java_opts.bzl", "protobuf_java_export", "protobuf_java_library", "protobuf_versioned_java_library")
load("//conformance:defs.bzl", "conformance_test")
load("//java/internal:testing.bzl", "junit_tests")
load("//src/google/protobuf/editions:defaults.bzl", "compile_edition_defaults", "embed_edition_defaults")

LITE_SRCS = [
# Keep in sync with `//java/lite:pom.xml`.
Expand Down Expand Up @@ -172,9 +173,16 @@ proto_library(
deps = ["//:descriptor_proto"],
)

filegroup(
name = "java_features_proto_srcs",
srcs = ["src/main/java/com/google/protobuf/java_features.proto"],
visibility = ["//pkg:__pkg__"],
)

internal_gen_well_known_protos_java(
name = "gen_well_known_protos_java",
deps = [
":java_features_proto",
"//:any_proto",
"//:api_proto",
"//:compiler_plugin_proto",
Expand All @@ -198,6 +206,7 @@ java_library(
],
exclude = LITE_SRCS,
) + [
"src/main/java/com/google/protobuf/JavaEditionDefaults.java",
":gen_well_known_protos_java",
],
visibility = ["//visibility:public"],
Expand All @@ -217,6 +226,7 @@ protobuf_versioned_java_library(
],
exclude = LITE_SRCS,
) + [
"src/main/java/com/google/protobuf/JavaEditionDefaults.java",
":gen_well_known_protos_java",
],
automatic_module_name = "com.google.protobuf",
Expand All @@ -240,6 +250,7 @@ protobuf_java_export(
maven_coordinates = "com.google.protobuf:protobuf-java:%s" % PROTOBUF_JAVA_VERSION,
pom_template = "pom_template.xml",
resources = [
":java_features_proto_srcs",
"//:well_known_type_protos",
"//src/google/protobuf:descriptor_proto_srcs",
],
Expand All @@ -266,6 +277,7 @@ proto_lang_toolchain(
name = "toolchain",
# keep this in sync w/ WELL_KNOWN_PROTO_MAP in //:BUILD
blacklisted_protos = [
":java_features_proto",
"//:any_proto",
"//:api_proto",
"//:compiler_plugin_proto",
Expand Down Expand Up @@ -355,6 +367,7 @@ build_test(
conformance_test(
name = "conformance_test",
failure_list = "//conformance:failure_list_java.txt",
maximum_edition = "2023",
testee = "//conformance:conformance_java",
text_format_failure_list = "//conformance:text_format_failure_list_java.txt",
)
Expand Down Expand Up @@ -527,10 +540,29 @@ junit_tests(
],
)

compile_edition_defaults(
name = "java_edition_defaults",
srcs = [
":java_features_proto",
"//:descriptor_proto",
],
maximum_edition = "2023",
minimum_edition = "PROTO2",
)

embed_edition_defaults(
name = "embedded_java_edition_defaults_generate",
defaults = "java_edition_defaults",
output = "src/main/java/com/google/protobuf/JavaEditionDefaults.java",
placeholder = "DEFAULTS_VALUE",
template = "src/main/java/com/google/protobuf/JavaEditionDefaults.java.template",
)

pkg_files(
name = "dist_files",
srcs = glob([
"src/main/java/com/google/protobuf/*.java",
"src/main/java/com/google/protobuf/*.proto",
"src/test/java/**/*.java",
"src/test/proto/**/*.proto",
]) + [
Expand All @@ -539,6 +571,7 @@ pkg_files(
"generate-test-sources-build.xml",
"pom.xml",
"pom_template.xml",
"src/main/java/com/google/protobuf/JavaEditionDefaults.java",
],
strip_prefix = strip_prefix.from_root(""),
visibility = ["//java:__pkg__"],
Expand Down
1 change: 1 addition & 0 deletions java/core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<include>google/protobuf/duration.proto</include>
<include>google/protobuf/empty.proto</include>
<include>google/protobuf/field_mask.proto</include>
<include>google/protobuf/java_features.proto</include>
<include>google/protobuf/source_context.proto</include>
<include>google/protobuf/struct.proto</include>
<include>google/protobuf/timestamp.proto</include>
Expand Down
Loading

0 comments on commit 1c55877

Please sign in to comment.