Skip to content

Commit

Permalink
Enable editions support for Java Lite.
Browse files Browse the repository at this point in the history
java_features.proto and descriptor.proto should now both be included in the Java Lite and Kotlin Lite maven releases.

Fixes #7331

PiperOrigin-RevId: 618251348
  • Loading branch information
zhangskz authored and Copybara-Service committed Mar 22, 2024
1 parent d452a16 commit 500c953
Show file tree
Hide file tree
Showing 48 changed files with 1,377 additions and 776 deletions.
2 changes: 2 additions & 0 deletions conformance/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@ java_binary(
"//:protobuf_javalite",
"//:test_messages_proto2_java_proto_lite",
"//:test_messages_proto3_java_proto_lite",
"//src/google/protobuf/editions:test_messages_proto2_editions_java_proto_lite",
"//src/google/protobuf/editions:test_messages_proto3_editions_java_proto_lite",
],
)

Expand Down
85 changes: 54 additions & 31 deletions conformance/ConformanceJavaLite.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd

import com.google.protobuf.AbstractMessageLite;
import com.google.protobuf.ByteString;
import com.google.protobuf.CodedInputStream;
import com.google.protobuf.ExtensionRegistryLite;
Expand All @@ -15,6 +16,8 @@
import com.google.protobuf_test_messages.proto2.TestMessagesProto2;
import com.google.protobuf_test_messages.proto2.TestMessagesProto2.TestAllTypesProto2;
import com.google.protobuf_test_messages.proto3.TestMessagesProto3;
import com.google.protobuf_test_messages.editions.proto2.TestMessagesProto2Editions;
import com.google.protobuf_test_messages.editions.proto3.TestMessagesProto3Editions;
import com.google.protobuf_test_messages.proto3.TestMessagesProto3.TestAllTypesProto3;
import java.nio.ByteBuffer;
import java.util.ArrayList;
Expand Down Expand Up @@ -203,42 +206,62 @@ private <T extends MessageLite> T parseBinary(
return messages.get(0);
}

private Class<? extends AbstractMessageLite> createTestMessage(String messageType) {
switch (messageType) {
case "protobuf_test_messages.proto3.TestAllTypesProto3":
return TestAllTypesProto3.class;
case "protobuf_test_messages.proto2.TestAllTypesProto2":
return TestAllTypesProto2.class;
case "protobuf_test_messages.editions.proto3.TestAllTypesProto3":
return TestMessagesProto3Editions.TestAllTypesProto3.class;
case "protobuf_test_messages.editions.proto2.TestAllTypesProto2":
return TestMessagesProto2Editions.TestAllTypesProto2.class;
default:
throw new IllegalArgumentException(
"Protobuf request has unexpected payload type: " + messageType);
}
}

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

@SuppressWarnings("unchecked")
private Conformance.ConformanceResponse doTest(Conformance.ConformanceRequest request) {
com.google.protobuf.MessageLite testMessage;
boolean isProto3 =
request.getMessageType().equals("protobuf_test_messages.proto3.TestAllTypesProto3");
boolean isProto2 =
request.getMessageType().equals("protobuf_test_messages.proto2.TestAllTypesProto2");

String messageType = request.getMessageType();
switch (request.getPayloadCase()) {
case PROTOBUF_PAYLOAD:
{
if (isProto3) {
try {
ExtensionRegistryLite extensions = ExtensionRegistryLite.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 {
ExtensionRegistryLite extensions = ExtensionRegistryLite.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 RuntimeException("Protobuf request doesn't have specific payload type.");
try {
ExtensionRegistryLite extensions = ExtensionRegistryLite.newInstance();
createTestFile(messageType)
.getMethod("registerAllExtensions", ExtensionRegistryLite.class)
.invoke(null, extensions);
testMessage =
parseBinary(
request.getProtobufPayload(),
(Parser<AbstractMessageLite>)
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 Down
4 changes: 4 additions & 0 deletions conformance/failure_list_java_lite.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ Required.Proto3.ProtobufInput.PrematureEofInDelimitedDataForKnownNonRepeatedValu
Required.Proto3.ProtobufInput.PrematureEofInDelimitedDataForKnownRepeatedValue.MESSAGE
Required.Proto2.ProtobufInput.PrematureEofInDelimitedDataForKnownNonRepeatedValue.MESSAGE
Required.Proto2.ProtobufInput.PrematureEofInDelimitedDataForKnownRepeatedValue.MESSAGE
Required.Editions_Proto3.ProtobufInput.PrematureEofInDelimitedDataForKnownNonRepeatedValue.MESSAGE
Required.Editions_Proto3.ProtobufInput.PrematureEofInDelimitedDataForKnownRepeatedValue.MESSAGE
Required.Editions_Proto2.ProtobufInput.PrematureEofInDelimitedDataForKnownNonRepeatedValue.MESSAGE
Required.Editions_Proto2.ProtobufInput.PrematureEofInDelimitedDataForKnownRepeatedValue.MESSAGE
10 changes: 9 additions & 1 deletion java/core/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,10 @@ internal_gen_well_known_protos_java(
name = "gen_well_known_protos_javalite",
javalite = True,
deps = [
":java_features_proto",
"//:any_proto",
"//:api_proto",
"//:descriptor_proto",
"//:duration_proto",
"//:empty_proto",
"//:field_mask_proto",
Expand Down Expand Up @@ -155,7 +157,9 @@ protobuf_java_export(
maven_coordinates = "com.google.protobuf:protobuf-javalite:%s" % PROTOBUF_JAVA_VERSION,
pom_template = "//java/lite:pom_template.xml",
resources = [
":java_features_proto",
"//:lite_well_known_protos",
"//src/google/protobuf:descriptor_proto_srcs",
],
tags = ["manual"],
runtime_deps = [":lite_bundle"],
Expand All @@ -170,7 +174,11 @@ proto_library(
name = "java_features_proto",
srcs = ["src/main/java/com/google/protobuf/java_features.proto"],
strip_import_prefix = "/java/core/src/main/java/com",
visibility = ["//pkg:__pkg__"],
visibility = [
"//java/kotlin-lite:__pkg__",
"//java/lite:__pkg__",
"//pkg:__pkg__",
],
deps = ["//:descriptor_proto"],
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd

syntax = "proto3";
edition = "2023";

package any_test;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// https://developers.google.com/open-source/licenses/bsd

// A regression test for b/74087933
syntax = "proto2";
edition = "2023";

package protobuf_unittest;

Expand All @@ -17,6 +17,6 @@ option optimize_for = CODE_SIZE;
option java_multiple_files = true;

message TestCachedFieldSizeMessage {
optional protobuf_unittest.TestPackedTypes proto2_child = 1;
optional proto3_unittest.TestPackedTypes proto3_child = 2;
protobuf_unittest.TestPackedTypes proto2_child = 1;
proto3_unittest.TestPackedTypes proto3_child = 2;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd

syntax = "proto2";
edition = "2023";

package dynamic_message_test;

option features.utf8_validation = NONE;
option java_package = "dynamicmessagetest";
option java_outer_classname = "DynamicMessageTestProto";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd

syntax = "proto3";
edition = "2023";

package field_presence_test;

import "google/protobuf/unittest.proto";

option features.field_presence = IMPLICIT;
option java_package = "com.google.protobuf";
option java_outer_classname = "FieldPresenceTestProto";

Expand All @@ -20,6 +21,7 @@ message TestAllTypes {
BAR = 1;
BAZ = 2;
}

message NestedMessage {
int32 value = 1;
}
Expand Down Expand Up @@ -48,7 +50,7 @@ message TestAllTypes {
repeated NestedEnum repeated_nested_enum = 24;
repeated NestedMessage repeated_nested_message = 25;
repeated protobuf_unittest.TestRequired repeated_proto2_message = 26;
repeated NestedEnum packed_nested_enum = 27 [packed = true];
repeated NestedEnum packed_nested_enum = 27;
}

message TestOptionalFieldsOnly {
Expand Down
30 changes: 18 additions & 12 deletions java/core/src/test/proto/com/google/protobuf/lazy_fields_lite.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@
//
// A proto file with lazy fields

syntax = "proto2";
edition = "2023";

package protobuf_unittest;

option features.utf8_validation = NONE;

message LazyMessageLite {
optional int32 num = 1;
optional int32 num_with_default = 2 [default = 421];
optional LazyInnerMessageLite inner = 3 [lazy = true];
repeated LazyInnerMessageLite repeated_inner = 4 ;
int32 num = 1;
int32 num_with_default = 2 [default = 421];

LazyInnerMessageLite inner = 3 [lazy = true];

repeated LazyInnerMessageLite repeated_inner = 4;

oneof oneof_field {
int32 oneof_num = 5;
Expand All @@ -26,21 +30,23 @@ message LazyMessageLite {
}

message LazyInnerMessageLite {
optional int32 num = 1;
optional int32 num_with_default = 2 [default = 42];
optional LazyNestedInnerMessageLite nested = 3 [lazy = true];
int32 num = 1;
int32 num_with_default = 2 [default = 42];

LazyNestedInnerMessageLite nested = 3 [lazy = true];

extensions 1000 to max;
}

message LazyExtension {
extend LazyInnerMessageLite {
optional LazyExtension extension = 1000;
LazyExtension extension = 1000;
}
optional string name = 1;

string name = 1;
}

message LazyNestedInnerMessageLite {
optional int32 num = 1;
optional int32 num_with_default = 2 [default = 4];
int32 num = 1;
int32 num_with_default = 2 [default = 4];
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

// Author: pbogle@google.com (Phil Bogle)

syntax = "proto2";
edition = "2023";

package protobuf_unittest.lite_equals_and_hash;

option features.utf8_validation = NONE;

message TestOneofEquals {
oneof oneof_field {
Expand All @@ -20,41 +21,46 @@ message TestOneofEquals {
}

message Foo {
optional int32 value = 1;
int32 value = 1;
repeated Bar bar = 2;
map<string, string> my_map = 3;

oneof Single {
sint64 sint64 = 4;
// LINT: ALLOW_GROUPS
group MyGroup = 5 {
optional int32 value = 1;
}
MyGroup mygroup = 5 [features.message_encoding = DELIMITED];
}

// LINT: ALLOW_GROUPS
message MyGroup {
int32 value = 1;
}

extensions 100 to max;
}

message Bar {
extend Foo {
optional Bar foo_ext = 100;
Bar foo_ext = 100;
}

optional string name = 1;
string name = 1;
}

message BarPrime {
optional string name = 1;
string name = 1;
}

message Empty {}

extend Foo {
optional int32 varint = 101;
optional fixed32 fixed32 = 102;
optional fixed64 fixed64 = 103;
optional group MyGroup = 104 {
optional string group_value = 1;
}
int32 varint = 101;
fixed32 fixed32 = 102;
fixed64 fixed64 = 103;
MyGroup mygroup = 104 [features.message_encoding = DELIMITED];
}

message MyGroup {
string group_value = 1;
}

message TestRecursiveOneof {
Expand Down
Loading

0 comments on commit 500c953

Please sign in to comment.