diff --git a/WORKSPACE b/WORKSPACE index 26bb91dc78..4891082fce 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -233,10 +233,6 @@ http_archive( url = "https://github.com/protocolbuffers/protobuf/releases/download/v25.0/protobuf-25.0.tar.gz", ) -# Needed as a dependency of @com_google_protobuf_v25.x, which was before -# utf8_range was merged in. -http_archive( - name = "utf8_range", - strip_prefix = "utf8_range-d863bc33e15cba6d873c878dcca9e6fe52b2f8cb", - url = "https://github.com/protocolbuffers/utf8_range/archive/d863bc33e15cba6d873c878dcca9e6fe52b2f8cb.zip", -) +# Needed as a dependency of @com_google_protobuf_v25.0 +load("@com_google_protobuf_v25.0//:protobuf_deps.bzl", protobuf_v25_deps="protobuf_deps") +protobuf_v25_deps() diff --git a/compatibility/BUILD.bazel b/compatibility/BUILD.bazel index d55f61956e..d391bf1b2e 100644 --- a/compatibility/BUILD.bazel +++ b/compatibility/BUILD.bazel @@ -6,6 +6,26 @@ load("//compatibility:runtime_conformance.bzl", "java_runtime_conformance") +java_library( + name = "v25_test_protos_srcjar", + testonly = True, + srcs = glob([ + "v3.25.0/*.srcjar", + ]), + visibility = ["//java/core:__pkg__"], + deps = ["//java/core"], +) + +java_library( + name = "v25_test_protos_jar", + testonly = True, + srcs = glob([ + "v3.25.0/*.srcjar", + ]), + visibility = ["//java/core:__pkg__"], + deps = ["@com_google_protobuf_v25.0//java/core"], +) + # main gencode builds with main runtime as a proof of concept. java_runtime_conformance( name = "java_conformance_main", diff --git a/compatibility/v3.25.0/generic_test_protos-speed.srcjar b/compatibility/v3.25.0/generic_test_protos-speed.srcjar new file mode 100755 index 0000000000..8d3a110d67 Binary files /dev/null and b/compatibility/v3.25.0/generic_test_protos-speed.srcjar differ diff --git a/compatibility/v3.25.0/java_test_protos-speed.srcjar b/compatibility/v3.25.0/java_test_protos-speed.srcjar new file mode 100755 index 0000000000..e3dcad663b Binary files /dev/null and b/compatibility/v3.25.0/java_test_protos-speed.srcjar differ diff --git a/compatibility/v3.25.0/lite_test_protos-speed.srcjar b/compatibility/v3.25.0/lite_test_protos-speed.srcjar new file mode 100755 index 0000000000..b23b76007f Binary files /dev/null and b/compatibility/v3.25.0/lite_test_protos-speed.srcjar differ diff --git a/java/core/BUILD.bazel b/java/core/BUILD.bazel index 096fc0debf..6fe4ccff6a 100644 --- a/java/core/BUILD.bazel +++ b/java/core/BUILD.bazel @@ -581,6 +581,102 @@ junit_tests( ], ) +protobuf_java_library( + name = "v25_test_util_srcjar", + testonly = True, + srcs = [ + "src/test/java/com/google/protobuf/TestUtil.java", + "src/test/java/com/google/protobuf/TestUtilLite.java", + ], + deps = [ + ":core", + "//compatibility:v25_test_protos_srcjar", + "@maven//:com_google_guava_guava", + "@maven//:junit_junit", + ], +) + +# Tests source compatibility against v25 gencode jar compiled against current runtime +junit_tests( + name = "v25_core_tests_srcjar", + size = "small", + srcs = glob( + ["src/test/java/**/*.java"], + exclude = [ + # Depends on test protos or API changes added in v4.x.x (e.g. editions) + "src/test/java/com/google/protobuf/TextFormatTest.java", + "src/test/java/com/google/protobuf/DescriptorsTest.java", + "src/test/java/com/google/protobuf/DebugFormatTest.java", + "src/test/java/com/google/protobuf/CodedOutputStreamTest.java", + "src/test/java/com/google/protobuf/ProtobufToStringOutputTest.java", + # Excluded in core_tests + "src/test/java/com/google/protobuf/DecodeUtf8Test.java", + "src/test/java/com/google/protobuf/IsValidUtf8Test.java", + "src/test/java/com/google/protobuf/TestUtil.java", + "src/test/java/com/google/protobuf/TestUtilLite.java", + "src/test/java/com/google/protobuf/RuntimeVersionTest.java", + ], + ), + test_prefix = "v25SrcJar", + deps = [ + ":core", + ":v25_test_util_srcjar", + "//compatibility:v25_test_protos_srcjar", + "@maven//:com_google_guava_guava", + "@maven//:com_google_truth_truth", + "@maven//:junit_junit", + "@maven//:org_mockito_mockito_core", + ], +) + +protobuf_java_library( + name = "v25_test_util_jar", + testonly = True, + srcs = [ + "src/test/java/com/google/protobuf/TestUtil.java", + "src/test/java/com/google/protobuf/TestUtilLite.java", + ], + deps = [ + ":core", + "//compatibility:v25_test_protos_jar", + "@maven//:com_google_guava_guava", + "@maven//:junit_junit", + ], +) + +# Tests binary compatibility against v25 gencode ja compiled against v25 runtime +junit_tests( + name = "v25_core_tests_jar", + size = "small", + srcs = glob( + ["src/test/java/**/*.java"], + exclude = [ + # Depends on test protos or API changes added in v4.x.x (e.g. editions) + "src/test/java/com/google/protobuf/TextFormatTest.java", + "src/test/java/com/google/protobuf/DescriptorsTest.java", + "src/test/java/com/google/protobuf/DebugFormatTest.java", + "src/test/java/com/google/protobuf/CodedOutputStreamTest.java", + "src/test/java/com/google/protobuf/ProtobufToStringOutputTest.java", + # Excluded in core_tests + "src/test/java/com/google/protobuf/DecodeUtf8Test.java", + "src/test/java/com/google/protobuf/IsValidUtf8Test.java", + "src/test/java/com/google/protobuf/TestUtil.java", + "src/test/java/com/google/protobuf/TestUtilLite.java", + "src/test/java/com/google/protobuf/RuntimeVersionTest.java", + ], + ), + test_prefix = "v25Jar", + deps = [ + ":core", + ":v25_test_util_jar", + "//compatibility:v25_test_protos_jar", + "@maven//:com_google_guava_guava", + "@maven//:com_google_truth_truth", + "@maven//:junit_junit", + "@maven//:org_mockito_mockito_core", + ], +) + pkg_files( name = "dist_files", srcs = glob([ diff --git a/java/core/src/main/java/com/google/protobuf/GeneratedMessage.java b/java/core/src/main/java/com/google/protobuf/GeneratedMessage.java index 1ed3add663..9702cbc544 100644 --- a/java/core/src/main/java/com/google/protobuf/GeneratedMessage.java +++ b/java/core/src/main/java/com/google/protobuf/GeneratedMessage.java @@ -886,16 +886,16 @@ public abstract class GeneratedMessage extends AbstractMessage implements Serial Message getDefaultInstanceForType(); /** Check if a singular extension is present. */ - boolean hasExtension(ExtensionLite extension); + boolean hasExtension(ExtensionLite extension); /** Get the number of elements in a repeated extension. */ - int getExtensionCount(ExtensionLite> extension); + int getExtensionCount(ExtensionLite> extension); /** Get the value of an extension. */ - T getExtension(ExtensionLite extension); + T getExtension(ExtensionLite extension); /** Get one element of a repeated extension. */ - T getExtension(ExtensionLite> extension, int index); + T getExtension(ExtensionLite> extension, int index); } /** @@ -946,7 +946,7 @@ public abstract class GeneratedMessage extends AbstractMessage implements Serial this.extensions = builder.buildExtensions(); } - private void verifyExtensionContainingType(final Extension extension) { + private void verifyExtensionContainingType(final Extension extension) { if (extension.getDescriptor().getContainingType() != getDescriptorForType()) { // This can only happen if someone uses unchecked operations. throw new IllegalArgumentException( @@ -960,7 +960,8 @@ public abstract class GeneratedMessage extends AbstractMessage implements Serial /** Check if a singular extension is present. */ @Override - public final boolean hasExtension(final ExtensionLite extensionLite) { + public final boolean hasExtension( + final ExtensionLite extensionLite) { Extension extension = checkNotLite(extensionLite); verifyExtensionContainingType(extension); @@ -969,7 +970,8 @@ public abstract class GeneratedMessage extends AbstractMessage implements Serial /** Get the number of elements in a repeated extension. */ @Override - public final int getExtensionCount(final ExtensionLite> extensionLite) { + public final int getExtensionCount( + final ExtensionLite> extensionLite) { Extension> extension = checkNotLite(extensionLite); verifyExtensionContainingType(extension); @@ -980,7 +982,7 @@ public abstract class GeneratedMessage extends AbstractMessage implements Serial /** Get the value of an extension. */ @Override @SuppressWarnings("unchecked") - public final T getExtension(final ExtensionLite extensionLite) { + public final T getExtension(final ExtensionLite extensionLite) { Extension extension = checkNotLite(extensionLite); verifyExtensionContainingType(extension); @@ -1003,7 +1005,7 @@ public abstract class GeneratedMessage extends AbstractMessage implements Serial @Override @SuppressWarnings("unchecked") public final T getExtension( - final ExtensionLite> extensionLite, final int index) { + final ExtensionLite> extensionLite, final int index) { Extension> extension = checkNotLite(extensionLite); verifyExtensionContainingType(extension); @@ -1036,7 +1038,8 @@ public abstract class GeneratedMessage extends AbstractMessage implements Serial private Map.Entry next; private final boolean messageSetWireFormat; - private ExtensionWriter(final boolean messageSetWireFormat) { + // TODO: Should be marked private in v5.x.x once GeneratedMessageV3 is removed. + protected ExtensionWriter(final boolean messageSetWireFormat) { if (iter.hasNext()) { next = iter.next(); } @@ -1254,7 +1257,8 @@ public abstract class GeneratedMessage extends AbstractMessage implements Serial /** Check if a singular extension is present. */ @Override - public final boolean hasExtension(final ExtensionLite extensionLite) { + public final boolean hasExtension( + final ExtensionLite extensionLite) { Extension extension = checkNotLite(extensionLite); verifyExtensionContainingType(extension); @@ -1263,7 +1267,8 @@ public abstract class GeneratedMessage extends AbstractMessage implements Serial /** Get the number of elements in a repeated extension. */ @Override - public final int getExtensionCount(final ExtensionLite> extensionLite) { + public final int getExtensionCount( + final ExtensionLite> extensionLite) { Extension> extension = checkNotLite(extensionLite); verifyExtensionContainingType(extension); @@ -1273,7 +1278,7 @@ public abstract class GeneratedMessage extends AbstractMessage implements Serial /** Get the value of an extension. */ @Override - public final T getExtension(final ExtensionLite extensionLite) { + public final T getExtension(final ExtensionLite extensionLite) { Extension extension = checkNotLite(extensionLite); verifyExtensionContainingType(extension); @@ -1295,7 +1300,7 @@ public abstract class GeneratedMessage extends AbstractMessage implements Serial /** Get one element of a repeated extension. */ @Override public final T getExtension( - final ExtensionLite> extensionLite, final int index) { + final ExtensionLite> extensionLite, final int index) { Extension> extension = checkNotLite(extensionLite); verifyExtensionContainingType(extension); @@ -1309,7 +1314,7 @@ public abstract class GeneratedMessage extends AbstractMessage implements Serial /** Set the value of an extension. */ public final BuilderT setExtension( - final ExtensionLite extensionLite, final T value) { + final ExtensionLite extensionLite, final T value) { Extension extension = checkNotLite(extensionLite); verifyExtensionContainingType(extension); @@ -1322,7 +1327,9 @@ public abstract class GeneratedMessage extends AbstractMessage implements Serial /** Set the value of one element of a repeated extension. */ public final BuilderT setExtension( - final ExtensionLite> extensionLite, final int index, final T value) { + final ExtensionLite> extensionLite, + final int index, + final T value) { Extension> extension = checkNotLite(extensionLite); verifyExtensionContainingType(extension); @@ -1335,7 +1342,7 @@ public abstract class GeneratedMessage extends AbstractMessage implements Serial /** Append a value to a repeated extension. */ public final BuilderT addExtension( - final ExtensionLite> extensionLite, final T value) { + final ExtensionLite> extensionLite, final T value) { Extension> extension = checkNotLite(extensionLite); verifyExtensionContainingType(extension); @@ -1347,7 +1354,8 @@ public abstract class GeneratedMessage extends AbstractMessage implements Serial } /** Clear an extension. */ - public final BuilderT clearExtension(final ExtensionLite extensionLite) { + public final BuilderT clearExtension( + final ExtensionLite extensionLite) { Extension extension = checkNotLite(extensionLite); verifyExtensionContainingType(extension); @@ -1563,7 +1571,8 @@ public abstract class GeneratedMessage extends AbstractMessage implements Serial } } - protected final void mergeExtensionFields(final ExtendableMessage other) { + // TODO: Should be marked final in v5.x.x once GeneratedMessageV3 is removed. + protected void mergeExtensionFields(final ExtendableMessage other) { if (other.extensions != null) { ensureExtensionsIsMutable(); extensions.mergeFrom(other.extensions); @@ -1939,7 +1948,8 @@ public abstract class GeneratedMessage extends AbstractMessage implements Serial * Users should ignore this class. This class provides the implementation with access to the * fields of a message object using Java reflection. */ - public static final class FieldAccessorTable { + // TODO: Should be marked final in v5.x.x once GeneratedMessageV3 is removed. + public static class FieldAccessorTable { /** * Construct a FieldAccessorTable for a particular message class. Only one FieldAccessorTable @@ -3136,7 +3146,7 @@ public abstract class GeneratedMessage extends AbstractMessage implements Serial * Checks that the {@link Extension} is non-Lite and returns it as a {@link GeneratedExtension}. */ private static , T> - Extension checkNotLite(ExtensionLite extension) { + Extension checkNotLite(ExtensionLite extension) { if (extension.isLite()) { throw new IllegalArgumentException("Expected non-lite extension."); } diff --git a/java/core/src/main/java/com/google/protobuf/GeneratedMessageV3.java b/java/core/src/main/java/com/google/protobuf/GeneratedMessageV3.java index 9071495866..9816ee5383 100644 --- a/java/core/src/main/java/com/google/protobuf/GeneratedMessageV3.java +++ b/java/core/src/main/java/com/google/protobuf/GeneratedMessageV3.java @@ -7,58 +7,538 @@ package com.google.protobuf; +import com.google.protobuf.Descriptors.Descriptor; +import com.google.protobuf.Descriptors.FieldDescriptor; +import com.google.protobuf.Descriptors.OneofDescriptor; import java.util.List; /** * Stub for GeneratedMessageV3 wrapping GeneratedMessage for compatibility with older gencode. * + *

Extends GeneratedMessage.ExtendableMessage instead of GeneratedMessage to allow "multiple + * inheritance" for GeneratedMessageV3.ExtendableMessage subclass. + * * @deprecated This class is deprecated, and slated for removal in the next Java breaking change - * (5.x in 2025 Q1). Users should update gencode to >= 4.26.x which uses GeneratedMessage - * instead of GeneratedMessageV3. + * (5.x). Users should update gencode to >= 4.26.x which uses GeneratedMessage instead. */ @Deprecated -public abstract class GeneratedMessageV3 extends GeneratedMessage { +public abstract class GeneratedMessageV3 + extends GeneratedMessage.ExtendableMessage { private static final long serialVersionUID = 1L; + @Deprecated protected GeneratedMessageV3() { super(); } + @Deprecated protected GeneratedMessageV3(Builder builder) { super(builder); } + /* Overrides abstract GeneratedMessage.internalGetFieldAccessorTable(). + * + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * GeneratedMessage.internalGetFieldAccessorTable() instead. + */ + @Deprecated + @Override + protected FieldAccessorTable internalGetFieldAccessorTable() { + throw new UnsupportedOperationException("Should be overridden in gencode."); + } + + /** + * Stub for GeneratedMessageV3.UnusedPrivateParameter for compatibility with older gencode. + * + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * GeneratedMessage.UnusedPrivateParameter instead. + */ + @Deprecated + protected static final class UnusedPrivateParameter { + static final UnusedPrivateParameter INSTANCE = new UnusedPrivateParameter(); + + private UnusedPrivateParameter() {} + } + + /* Stub for method overridden from old generated code + + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which overrides + * GeneratedMessage.newInstance() instead. + */ + @Deprecated + @SuppressWarnings({"unused"}) + protected Object newInstance(UnusedPrivateParameter unused) { + throw new UnsupportedOperationException("This method must be overridden by the subclass."); + } + + @Deprecated + protected interface BuilderParent extends AbstractMessage.BuilderParent {} + + @Deprecated + protected abstract Message.Builder newBuilderForType(BuilderParent parent); + + /* Removed from GeneratedMessage in + * https://github.com/protocolbuffers/protobuf/commit/787447430fc9a69c071393e85a380b664d261ab4 + * + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which no longer uses this method. + */ + @Deprecated + @Override + protected Message.Builder newBuilderForType(final AbstractMessage.BuilderParent parent) { + return newBuilderForType( + new BuilderParent() { + @Override + public void markDirty() { + parent.markDirty(); + } + }); + } + + /** + * Stub for GeneratedMessageV3.Builder wrapping GeneratedMessage.Builder for compatibility with + * older gencode. + * + *

Extends GeneratedMessage.ExtendableBuilder instead of GeneratedMessage.Builder to allow + * "multiple inheritance" for GeneratedMessageV3.ExtendableBuilder subclass. + * + * @deprecated This class is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses GeneratedMessage.Builder + * instead. + */ + @Deprecated + public abstract static class Builder> + extends GeneratedMessage.ExtendableBuilder { + + private BuilderParentImpl meAsParent; + + @Deprecated + protected Builder() { + super(null); + } + + @Deprecated + protected Builder(BuilderParent builderParent) { + super(builderParent); + } + + /* Stub for method overridden from old generated code removed in + * https://github.com/protocolbuffers/protobuf/commit/7bff169d32710b143951ec6ce2c4ea9a56e2ad24 + * + * @deprecated This class is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which overrides + * GeneratedMessage.Builder.clone() instead. */ + @Deprecated + @Override + public BuilderT clone() { + return super.clone(); + } + + /* Stub for method overridden from old generated code + * + * @deprecated This class is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which overrides + * GeneratedMessage.Builder.setField() instead. */ + @Deprecated + @Override + public BuilderT clear() { + return super.clear(); + } + + /* Overrides abstract GeneratedMessage.Builder.internalGetFieldAccessorTable(). + * + * @deprecated This method is deprecated, and slated for removal in the next Java breaking + * change (5.x). Users should update gencode to >= 4.26.x which overrides + * GeneratedMessage.Builder.internalGetFieldAccessorTable() instead. + */ + @Deprecated + @Override + protected FieldAccessorTable internalGetFieldAccessorTable() { + throw new UnsupportedOperationException("Should be overridden in gencode."); + } + + /* Stub for method overridden from old generated code removed in + * https://github.com/protocolbuffers/protobuf/commit/7bff169d32710b143951ec6ce2c4ea9a56e2ad24 + * + * @deprecated This class is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which overrides + * GeneratedMessage.Builder.setField() instead. */ + @Deprecated + @Override + public BuilderT setField(final FieldDescriptor field, final Object value) { + return super.setField(field, value); + } + + /* Stub for method overridden from old generated code removed in + * https://github.com/protocolbuffers/protobuf/commit/7bff169d32710b143951ec6ce2c4ea9a56e2ad24 + * + * @deprecated This class is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which overrides + * GeneratedMessage.Builder.clearField() instead. */ + @Deprecated + @Override + public BuilderT clearField(final FieldDescriptor field) { + return super.clearField(field); + } + + /* Stub for method overridden from old generated code removed in + * https://github.com/protocolbuffers/protobuf/commit/7bff169d32710b143951ec6ce2c4ea9a56e2ad24 + * + * @deprecated This class is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which overrides + * GeneratedMessage.Builder.clearOneof() instead. */ + @Deprecated + @Override + public BuilderT clearOneof(final OneofDescriptor oneof) { + return super.clearOneof(oneof); + } + + /* Stub for method overridden from old generated code removed in + * https://github.com/protocolbuffers/protobuf/commit/7bff169d32710b143951ec6ce2c4ea9a56e2ad24 + * + * @deprecated This class is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which overrides + * GeneratedMessage.Builder.setRepeatedField() instead. */ + @Deprecated + @Override + public BuilderT setRepeatedField( + final FieldDescriptor field, final int index, final Object value) { + return super.setRepeatedField(field, index, value); + } + + /* Stub for method overridden from old generated code removed in + * https://github.com/protocolbuffers/protobuf/commit/7bff169d32710b143951ec6ce2c4ea9a56e2ad24 + * + * @deprecated This class is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which overrides + * GeneratedMessage.Builder.addRepeatedField() instead. */ + @Deprecated + @Override + public BuilderT addRepeatedField(final FieldDescriptor field, final Object value) { + return super.addRepeatedField(field, value); + } + + /* Stub for method overridden from old generated code removed in + * https://github.com/protocolbuffers/protobuf/commit/7bff169d32710b143951ec6ce2c4ea9a56e2ad24 + * + * @deprecated This class is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which overrides + * GeneratedMessage.Builder.setUnknownFields() instead. */ + @Deprecated + @Override + public BuilderT setUnknownFields(final UnknownFieldSet unknownFields) { + return super.setUnknownFields(unknownFields); + } + + /* Stub for method overridden from old generated code removed in + * https://github.com/protocolbuffers/protobuf/commit/7bff169d32710b143951ec6ce2c4ea9a56e2ad24 + * + * @deprecated This class is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which overrides + * GeneratedMessage.Builder.mergeUnknownFields() instead. */ + @Deprecated + @Override + public BuilderT mergeUnknownFields(final UnknownFieldSet unknownFields) { + return super.mergeUnknownFields(unknownFields); + } + + @Deprecated + private class BuilderParentImpl implements BuilderParent { + @Override + public void markDirty() { + onChanged(); + } + } + + /* Returns GeneratedMessageV3.Builder.BuilderParent instead of + * GeneratedMessage.Builder.BuilderParent. + * + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * GeneratedMessage.Builder.getParentForChildren() instead. + */ + @Deprecated + @Override + protected BuilderParent getParentForChildren() { + if (meAsParent == null) { + meAsParent = new BuilderParentImpl(); + } + return meAsParent; + } + } + + /** + * Stub for GeneratedMessageV3.ExtendableMessageOrBuilder wrapping + * GeneratedMessage.ExtendableMessageOrBuilder for compatibility with older gencode. + * + * @deprecated This class is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * GeneratedMessage.ExtendableMessageOrBuilder. + */ + @Deprecated + public interface ExtendableMessageOrBuilder> + extends GeneratedMessage.ExtendableMessageOrBuilder { + + /* Removed from GeneratedMessage.ExtendableMessageOrBuilder in + * https://github.com/protocolbuffers/protobuf/commit/94a2a448518403341b8aa71335ab1123fbdcccd8 + * + * @deprecated This method is deprecated, and slated for removal in the next Java breaking + * change (5.x). Users should update gencode to >= 4.26.x which no longer overrides this method. + */ + @Deprecated + boolean hasExtension(GeneratedExtension extension); + + /* Removed from GeneratedMessage.ExtendableMessageOrBuilder in + * https://github.com/protocolbuffers/protobuf/commit/94a2a448518403341b8aa71335ab1123fbdcccd8 + * + * @deprecated This method is deprecated, and slated for removal in the next Java breaking + * change (5.x). Users should update gencode to >= 4.26.x which no longer overrides this method. + */ + @Deprecated + int getExtensionCount(GeneratedExtension> extension); + + /* Removed from GeneratedMessage.ExtendableMessageOrBuilder in + * https://github.com/protocolbuffers/protobuf/commit/94a2a448518403341b8aa71335ab1123fbdcccd8 + * + * @deprecated This method is deprecated, and slated for removal in the next Java breaking + * change (5.x). Users should update gencode to >= 4.26.x which no longer overrides this method. + */ + @Deprecated + T getExtension(GeneratedExtension extension); + + /* Removed from GeneratedMessage.ExtendableMessageOrBuilder in + * https://github.com/protocolbuffers/protobuf/commit/94a2a448518403341b8aa71335ab1123fbdcccd8 + * + * @deprecated This method is deprecated, and slated for removal in the next Java breaking + * change (5.x). Users should update gencode to >= 4.26.x which no longer overrides this method. + */ + @Deprecated + T getExtension(GeneratedExtension> extension, int index); + } + + /** + * Stub for GeneratedMessageV3.ExtendableMessage wrapping GeneratedMessage.ExtendableMessage for + * compatibility with older gencode. + * + * @deprecated This class is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * GeneratedMessage.ExtendableMessage. + */ + @Deprecated + public abstract static class ExtendableMessage> + extends GeneratedMessageV3 // Extends GeneratedMessage.ExtendableMessage via + // GeneratedMessageV3 + implements ExtendableMessageOrBuilder { + + @Deprecated + protected ExtendableMessage() { + super(); + } + + @Deprecated + protected ExtendableMessage(ExtendableBuilder builder) { + super(builder); + } + + /* Removed from GeneratedMessage.ExtendableMessage in + * https://github.com/protocolbuffers/protobuf/commit/94a2a448518403341b8aa71335ab1123fbdcccd8 + * + * @deprecated This method is deprecated, and slated for removal in the next Java breaking + * change (5.x). Users should update gencode to >= 4.26.x which no longer overrides this method. + */ + @Deprecated + @Override + public final boolean hasExtension(final GeneratedExtension extension) { + return hasExtension((ExtensionLite) extension); + } + + /* Removed from GeneratedMessage.ExtendableMessage in + * https://github.com/protocolbuffers/protobuf/commit/94a2a448518403341b8aa71335ab1123fbdcccd8 + * + * @deprecated This method is deprecated, and slated for removal in the next Java breaking + * change (5.x). Users should update gencode to >= 4.26.x which no longer overrides this method. + */ + @Deprecated + @Override + public final int getExtensionCount(final GeneratedExtension> extension) { + return getExtensionCount((ExtensionLite>) extension); + } + + /* Removed from GeneratedMessage.ExtendableMessage in + * https://github.com/protocolbuffers/protobuf/commit/94a2a448518403341b8aa71335ab1123fbdcccd8 + * + * @deprecated This method is deprecated, and slated for removal in the next Java breaking + * change (5.x). Users should update gencode to >= 4.26.x which no longer overrides this method. + */ + @Deprecated + @Override + public final T getExtension(final GeneratedExtension extension) { + return getExtension((ExtensionLite) extension); + } + + /* Removed from GeneratedMessage.ExtendableMessage in + * https://github.com/protocolbuffers/protobuf/commit/94a2a448518403341b8aa71335ab1123fbdcccd8 + * + * @deprecated This method is deprecated, and slated for removal in the next Java breaking + * change (5.x). Users should update gencode to >= 4.26.x which no longer overrides this method. + */ + @Deprecated + @Override + public final T getExtension( + final GeneratedExtension> extension, final int index) { + return getExtension((ExtensionLite>) extension, index); + } + + /* Overrides abstract GeneratedMessage.ExtendableMessage.internalGetFieldAccessorTable(). + * + * @deprecated This method is deprecated, and slated for removal in the next Java breaking + * change (5.x). Users should update gencode to >= 4.26.x which overrides + * GeneratedMessage.ExtendableMessage.internalGetFieldAccessorTable() instead. + */ + @Deprecated + @Override + protected FieldAccessorTable internalGetFieldAccessorTable() { + throw new UnsupportedOperationException("Should be overridden in gencode."); + } + + /** + * Stub for GeneratedMessageV3.ExtendableMessage.ExtensionWriter wrapping + * GeneratedMessage.ExtendableMessage.ExtensionWriter for compatibility with older gencode. + * + * @deprecated This class is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * GeneratedMessage.ExtendableMessage.ExtensionWriter instead. + */ + @Deprecated + protected class ExtensionWriter extends GeneratedMessage.ExtendableMessage.ExtensionWriter { + private ExtensionWriter(final boolean messageSetWireFormat) { + super(messageSetWireFormat); + } + } + + /* Returns GeneratedMessageV3.ExtendableMessage.ExtensionWriter instead of + * GeneratedMessage.ExtendableMessage.ExtensionWriter. + * + * @deprecated This method is deprecated, and slated for removal in the next Java breaking + * change (5.x). Users should update gencode to >= 4.26.x which uses + * GeneratedMessage.ExtendableMessage.newExtensionWriter() instead. + */ + @Deprecated + @Override + protected ExtensionWriter newExtensionWriter() { + return new ExtensionWriter(false); + } + + /* Returns GeneratedMessageV3.ExtendableMessage.ExtensionWriter instead of + * GeneratedMessage.ExtendableMessage.ExtensionWriter. + * + * @deprecated This method is deprecated, and slated for removal in the next Java breaking + * change (5.x). Users should update gencode to >= 4.26.x which uses + * GeneratedMessage.ExtendableMessage.newMessageSetExtensionWriter() instead. + */ + @Deprecated + @Override + protected ExtensionWriter newMessageSetExtensionWriter() { + return new ExtensionWriter(true); + } + } + /** * Stub for GeneratedMessageV3.ExtendableBuilder wrapping GeneratedMessage.ExtendableBuilder for * compatibility with older gencode. * * @deprecated This class is deprecated, and slated for removal in the next Java breaking change - * (5.x in 2025 Q1). Users should update gencode to >= 4.26.x which uses - * GeneratedMessage.ExtendableBuilder instead of GeneratedMessageV3.ExtendableBuilder. + * (5.x). Users should update gencode to >= 4.26.x which uses + * GeneratedMessage.ExtendableBuilder instead. */ @Deprecated public abstract static class ExtendableBuilder< MessageT extends ExtendableMessage, BuilderT extends ExtendableBuilder> - extends GeneratedMessage.ExtendableBuilder - implements GeneratedMessage.ExtendableMessageOrBuilder { + extends Builder // Extends GeneratedMessage.ExtendableBuilder via Builder + implements ExtendableMessageOrBuilder { + + @Deprecated protected ExtendableBuilder() { super(); } + @Deprecated protected ExtendableBuilder(BuilderParent parent) { super(parent); } - // Support old gencode override method removed in - // https://github.com/protocolbuffers/protobuf/commit/7bff169d32710b143951ec6ce2c4ea9a56e2ad24 + /* Removed from GeneratedMessage.ExtendableBuilder in + * https://github.com/protocolbuffers/protobuf/commit/94a2a448518403341b8aa71335ab1123fbdcccd8 + * + * @deprecated This method is deprecated, and slated for removal in the next Java breaking + * change (5.x). Users should update gencode to >= 4.26.x which no longer overrides this method. + */ + @Deprecated + @Override + public final boolean hasExtension(final GeneratedExtension extension) { + return hasExtension((ExtensionLite) extension); + } + + /* Removed from GeneratedMessage.ExtendableBuilder in + * https://github.com/protocolbuffers/protobuf/commit/94a2a448518403341b8aa71335ab1123fbdcccd8 + * + * @deprecated This method is deprecated, and slated for removal in the next Java breaking + * change (5.x). Users should update gencode to >= 4.26.x which no longer overrides this method. + */ + @Deprecated + @Override + public final int getExtensionCount(final GeneratedExtension> extension) { + return getExtensionCount((ExtensionLite>) extension); + } + + /* Removed from GeneratedMessage.ExtendableBuilder in + * https://github.com/protocolbuffers/protobuf/commit/94a2a448518403341b8aa71335ab1123fbdcccd8 + * + * @deprecated This method is deprecated, and slated for removal in the next Java breaking + * change (5.x). Users should update gencode to >= 4.26.x which no longer overrides this method. + */ + @Deprecated + @Override + public final T getExtension(final GeneratedExtension extension) { + return getExtension((ExtensionLite) extension); + } + + /* Removed from GeneratedMessage.ExtendableBuilder in + * https://github.com/protocolbuffers/protobuf/commit/94a2a448518403341b8aa71335ab1123fbdcccd8 + * + * @deprecated This method is deprecated, and slated for removal in the next Java breaking + * change (5.x). Users should update gencode to >= 4.26.x which no longer overrides this method. + */ + @Deprecated + @Override + public final T getExtension( + final GeneratedExtension> extension, final int index) { + return getExtension((ExtensionLite>) extension, index); + } + + /* Removed from GeneratedMessage.ExtendableBuilder in + * https://github.com/protocolbuffers/protobuf/commit/94a2a448518403341b8aa71335ab1123fbdcccd8 + * + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which no longer overrides this method. + */ + @Deprecated public BuilderT setExtension( final GeneratedMessage.GeneratedExtension extension, final T value) { return setExtension((ExtensionLite) extension, value); } - // Support old gencode override method removed in - // https://github.com/protocolbuffers/protobuf/commit/7bff169d32710b143951ec6ce2c4ea9a56e2ad24 + /* Removed from GeneratedMessage.ExtendableBuilder in + * https://github.com/protocolbuffers/protobuf/commit/94a2a448518403341b8aa71335ab1123fbdcccd8 + * + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which no longer overrides this method. + */ + @Deprecated public BuilderT setExtension( final GeneratedMessage.GeneratedExtension> extension, final int index, @@ -66,18 +546,141 @@ public abstract class GeneratedMessageV3 extends GeneratedMessage { return setExtension((ExtensionLite>) extension, index, value); } - // Support old gencode override method removed in - // https://github.com/protocolbuffers/protobuf/commit/7bff169d32710b143951ec6ce2c4ea9a56e2ad24 + /* Removed from GeneratedMessage.ExtendableBuilder in + * https://github.com/protocolbuffers/protobuf/commit/94a2a448518403341b8aa71335ab1123fbdcccd8 + * + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which no longer overrides this method. + */ + @Deprecated public BuilderT addExtension( final GeneratedMessage.GeneratedExtension> extension, final T value) { return addExtension((ExtensionLite>) extension, value); } - // Support old gencode override method removed in - // https://github.com/protocolbuffers/protobuf/commit/7bff169d32710b143951ec6ce2c4ea9a56e2ad24 + /* Removed from GeneratedMessage.ExtendableBuilder in + * https://github.com/protocolbuffers/protobuf/commit/94a2a448518403341b8aa71335ab1123fbdcccd8 + * + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which no longer overrides this method. + */ + @Deprecated public BuilderT clearExtension( final GeneratedMessage.GeneratedExtension extension) { return clearExtension((ExtensionLite) extension); } + + /* Stub for method overridden from old generated code removed in + * https://github.com/protocolbuffers/protobuf/commit/7bff169d32710b143951ec6ce2c4ea9a56e2ad24 + * + * @deprecated This class is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which overrides + * GeneratedMessage.ExtendableBuilder.setField() instead. */ + @Deprecated + @Override + public BuilderT setField(final FieldDescriptor field, final Object value) { + return super.setField(field, value); + } + + /* Stub for method overridden from old generated code removed in + * https://github.com/protocolbuffers/protobuf/commit/7bff169d32710b143951ec6ce2c4ea9a56e2ad24 + * + * @deprecated This class is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which overrides + * GeneratedMessage.ExtendableBuilder.clearField() instead. */ + @Deprecated + @Override + public BuilderT clearField(final FieldDescriptor field) { + return super.clearField(field); + } + + /* Stub for method overridden from old generated code removed in + * https://github.com/protocolbuffers/protobuf/commit/7bff169d32710b143951ec6ce2c4ea9a56e2ad24 + * + * @deprecated This class is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which overrides + * GeneratedMessage.ExtendableBuilder.clearOneof() instead. */ + @Deprecated + @Override + public BuilderT clearOneof(final OneofDescriptor oneof) { + return super.clearOneof(oneof); + } + + /* Stub for method overridden from old generated code removed in + * https://github.com/protocolbuffers/protobuf/commit/7bff169d32710b143951ec6ce2c4ea9a56e2ad24 + * + * @deprecated This class is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which overrides + * GeneratedMessage.ExtendableBuilder.setRepeatedField() instead. */ + @Deprecated + @Override + public BuilderT setRepeatedField( + final FieldDescriptor field, final int index, final Object value) { + return super.setRepeatedField(field, index, value); + } + + /* Stub for method overridden from old generated code removed in + * https://github.com/protocolbuffers/protobuf/commit/7bff169d32710b143951ec6ce2c4ea9a56e2ad24 + * + * @deprecated This class is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which overrides + * GeneratedMessage.ExtendableBuilder.addRepeatedField() instead. */ + @Deprecated + @Override + public BuilderT addRepeatedField(final FieldDescriptor field, final Object value) { + return super.addRepeatedField(field, value); + } + + /* Stub for method called from old generated code. + * @Override not allowed despite inheriting from + * GeneratedMessage.ExtendableBuilder.mergeExtensionFields(). + * + * @deprecated This class is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * GeneratedMessage.ExtendableBuilder.mergeExtensionFields() instead. */ + @Deprecated + protected final void mergeExtensionFields(final ExtendableMessage other) { + super.mergeExtensionFields(other); + } + } + + /** + * Stub for GeneratedMessageV3.FieldAccessorTable wrapping GeneratedMessage.FieldAccessorTable for + * compatibility with older gencode. + * + * @deprecated This class is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * GeneratedMessage.FieldAccessorTable instead. + */ + @Deprecated + public static final class FieldAccessorTable extends GeneratedMessage.FieldAccessorTable { + + @Deprecated + public FieldAccessorTable( + final Descriptor descriptor, + final String[] camelCaseNames, + final Class messageClass, + final Class> builderClass) { + super(descriptor, camelCaseNames, messageClass, builderClass); + } + + @Deprecated + public FieldAccessorTable(final Descriptor descriptor, final String[] camelCaseNames) { + super(descriptor, camelCaseNames); + } + + /* Returns GeneratedMessageV3.FieldAccessorTable instead of GeneratedMessage.FieldAccessorTable. + * + * @deprecated This method is deprecated, and slated for removal in the next Java breaking + * change (5.x). Users should update gencode to >= 4.26.x which uses + * GeneratedMessage.ensureFieldAccessorsInitialized() instead. + */ + @Deprecated + @Override + public FieldAccessorTable ensureFieldAccessorsInitialized( + Class messageClass, + Class> builderClass) { + return (FieldAccessorTable) super.ensureFieldAccessorsInitialized(messageClass, builderClass); + } } } diff --git a/java/core/src/main/java/com/google/protobuf/RepeatedFieldBuilderV3.java b/java/core/src/main/java/com/google/protobuf/RepeatedFieldBuilderV3.java index 14a47a5d46..dc3326190a 100644 --- a/java/core/src/main/java/com/google/protobuf/RepeatedFieldBuilderV3.java +++ b/java/core/src/main/java/com/google/protobuf/RepeatedFieldBuilderV3.java @@ -7,28 +7,579 @@ package com.google.protobuf; +import static com.google.protobuf.Internal.checkNotNull; + +import java.util.AbstractList; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; import java.util.List; +import java.util.RandomAccess; /** - * Stub for RepeatedFieldBuilderV3 wrapping RepeatedFieldBuilder for compatibility with older - * gencode. + * Stub for RepeatedFieldBuilderV3 matching RepeatedFieldBuilder for compatibility with older + * gencode. This cannot wrap RepeatedFieldBuilder directly due to RepeatedFieldBuilder having more + * restrictive extends GeneratedMessage for MType and BType. * * @deprecated This class is deprecated, and slated for removal in the next breaking change. Users * should update gencode to >= 4.26.x which replaces RepeatedFieldBuilderV3 with * RepeatedFieldBuilder. */ -@Deprecated public class RepeatedFieldBuilderV3< - MType extends GeneratedMessage, - BType extends GeneratedMessage.Builder, + MType extends AbstractMessage, + BType extends AbstractMessage.Builder, IType extends MessageOrBuilder> - extends RepeatedFieldBuilder { + implements AbstractMessage.BuilderParent { + + private AbstractMessage.BuilderParent parent; + + private List messages; + + private boolean isMessagesListMutable; + + private List> builders; + + private boolean isClean; + private MessageExternalList externalMessageList; + + private BuilderExternalList externalBuilderList; + + private MessageOrBuilderExternalList externalMessageOrBuilderList; + + @Deprecated public RepeatedFieldBuilderV3( List messages, boolean isMessagesListMutable, - GeneratedMessage.BuilderParent parent, + AbstractMessage.BuilderParent parent, boolean isClean) { - super(messages, isMessagesListMutable, parent, isClean); + this.messages = messages; + this.isMessagesListMutable = isMessagesListMutable; + this.parent = parent; + this.isClean = isClean; + } + + /* + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * RepeatedFieldBuilder.dispose() instead. + */ + @Deprecated + public void dispose() { + parent = null; + } + + private void ensureMutableMessageList() { + if (!isMessagesListMutable) { + messages = new ArrayList(messages); + isMessagesListMutable = true; + } + } + + private void ensureBuilders() { + if (this.builders == null) { + this.builders = new ArrayList>(messages.size()); + for (int i = 0; i < messages.size(); i++) { + builders.add(null); + } + } + } + + /* + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * RepeatedFieldBuilder.getCount() instead. + */ + @Deprecated + public int getCount() { + return messages.size(); + } + + /* + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * RepeatedFieldBuilder.isEmpty() instead. + */ + @Deprecated + public boolean isEmpty() { + return messages.isEmpty(); + } + + /* + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * RepeatedFieldBuilder.getMessage() instead. + */ + @Deprecated + public MType getMessage(int index) { + return getMessage(index, false); + } + + private MType getMessage(int index, boolean forBuild) { + if (this.builders == null) { + return messages.get(index); + } + + SingleFieldBuilderV3 builder = builders.get(index); + if (builder == null) { + return messages.get(index); + + } else { + return forBuild ? builder.build() : builder.getMessage(); + } + } + + /* + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * RepeatedFieldBuilder.getBuilder() instead. + */ + @Deprecated + public BType getBuilder(int index) { + ensureBuilders(); + SingleFieldBuilderV3 builder = builders.get(index); + if (builder == null) { + MType message = messages.get(index); + builder = new SingleFieldBuilderV3(message, this, isClean); + builders.set(index, builder); + } + return builder.getBuilder(); + } + + /* + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * RepeatedFieldBuilder.getMessageOrBuilder() instead. + */ + @Deprecated + @SuppressWarnings("unchecked") + public IType getMessageOrBuilder(int index) { + if (this.builders == null) { + return (IType) messages.get(index); + } + + SingleFieldBuilderV3 builder = builders.get(index); + if (builder == null) { + return (IType) messages.get(index); + + } else { + return builder.getMessageOrBuilder(); + } + } + + /* + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * RepeatedFieldBuilder.setMessage() instead. + */ + @Deprecated + @CanIgnoreReturnValue + public RepeatedFieldBuilderV3 setMessage(int index, MType message) { + checkNotNull(message); + ensureMutableMessageList(); + messages.set(index, message); + if (builders != null) { + SingleFieldBuilderV3 entry = builders.set(index, null); + if (entry != null) { + entry.dispose(); + } + } + onChanged(); + incrementModCounts(); + return this; + } + + /* + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * RepeatedFieldBuilder.addMessage() instead. + */ + @Deprecated + @CanIgnoreReturnValue + public RepeatedFieldBuilderV3 addMessage(MType message) { + checkNotNull(message); + ensureMutableMessageList(); + messages.add(message); + if (builders != null) { + builders.add(null); + } + onChanged(); + incrementModCounts(); + return this; + } + + /* + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * RepeatedFieldBuilder.addMessage() instead. + */ + @Deprecated + @CanIgnoreReturnValue + public RepeatedFieldBuilderV3 addMessage(int index, MType message) { + checkNotNull(message); + ensureMutableMessageList(); + messages.add(index, message); + if (builders != null) { + builders.add(index, null); + } + onChanged(); + incrementModCounts(); + return this; + } + + /* + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * RepeatedFieldBuilder.addAllMessages() instead. + */ + @Deprecated + @CanIgnoreReturnValue + public RepeatedFieldBuilderV3 addAllMessages( + Iterable values) { + for (final MType value : values) { + checkNotNull(value); + } + + int size = -1; + if (values instanceof Collection) { + final Collection collection = (Collection) values; + if (collection.isEmpty()) { + return this; + } + size = collection.size(); + } + ensureMutableMessageList(); + + if (size >= 0 && messages instanceof ArrayList) { + ((ArrayList) messages).ensureCapacity(messages.size() + size); + } + + for (MType value : values) { + addMessage(value); + } + + onChanged(); + incrementModCounts(); + return this; + } + + /* + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * RepeatedFieldBuilder.addBuilder() instead. + */ + @Deprecated + public BType addBuilder(MType message) { + ensureMutableMessageList(); + ensureBuilders(); + SingleFieldBuilderV3 builder = + new SingleFieldBuilderV3(message, this, isClean); + messages.add(null); + builders.add(builder); + onChanged(); + incrementModCounts(); + return builder.getBuilder(); + } + + /* + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * RepeatedFieldBuilder.addBuilder() instead. + */ + @Deprecated + public BType addBuilder(int index, MType message) { + ensureMutableMessageList(); + ensureBuilders(); + SingleFieldBuilderV3 builder = + new SingleFieldBuilderV3(message, this, isClean); + messages.add(index, null); + builders.add(index, builder); + onChanged(); + incrementModCounts(); + return builder.getBuilder(); + } + + /* + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * RepeatedFieldBuilder.remove() instead. + */ + @Deprecated + public void remove(int index) { + ensureMutableMessageList(); + messages.remove(index); + if (builders != null) { + SingleFieldBuilderV3 entry = builders.remove(index); + if (entry != null) { + entry.dispose(); + } + } + onChanged(); + incrementModCounts(); + } + + /* + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * RepeatedFieldBuilder.clear() instead. + */ + @Deprecated + public void clear() { + messages = Collections.emptyList(); + isMessagesListMutable = false; + if (builders != null) { + for (SingleFieldBuilderV3 entry : builders) { + if (entry != null) { + entry.dispose(); + } + } + builders = null; + } + onChanged(); + incrementModCounts(); + } + + /* + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * RepeatedFieldBuilder.build() instead. + */ + @Deprecated + public List build() { + isClean = true; + + if (!isMessagesListMutable && builders == null) { + return messages; + } + + boolean allMessagesInSync = true; + if (!isMessagesListMutable) { + for (int i = 0; i < messages.size(); i++) { + Message message = messages.get(i); + SingleFieldBuilderV3 builder = builders.get(i); + if (builder != null) { + if (builder.build() != message) { + allMessagesInSync = false; + break; + } + } + } + if (allMessagesInSync) { + return messages; + } + } + ensureMutableMessageList(); + for (int i = 0; i < messages.size(); i++) { + messages.set(i, getMessage(i, true)); + } + messages = Collections.unmodifiableList(messages); + isMessagesListMutable = false; + return messages; + } + + /* + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * RepeatedFieldBuilder.getMessageList() instead. + */ + @Deprecated + public List getMessageList() { + if (externalMessageList == null) { + externalMessageList = new MessageExternalList(this); + } + return externalMessageList; + } + + /* + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * RepeatedFieldBuilder.getBuilderList() instead. + */ + @Deprecated + public List getBuilderList() { + if (externalBuilderList == null) { + externalBuilderList = new BuilderExternalList(this); + } + return externalBuilderList; + } + + /* + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * RepeatedFieldBuilder.getMessageOrBuilderList() instead. + */ + @Deprecated + public List getMessageOrBuilderList() { + if (externalMessageOrBuilderList == null) { + externalMessageOrBuilderList = new MessageOrBuilderExternalList(this); + } + return externalMessageOrBuilderList; + } + + private void onChanged() { + if (isClean && parent != null) { + parent.markDirty(); + isClean = false; + } + } + + /* + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * RepeatedFieldBuilder.markDirty() instead. + */ + @Deprecated + @Override + public void markDirty() { + onChanged(); + } + + private void incrementModCounts() { + if (externalMessageList != null) { + externalMessageList.incrementModCount(); + } + if (externalBuilderList != null) { + externalBuilderList.incrementModCount(); + } + if (externalMessageOrBuilderList != null) { + externalMessageOrBuilderList.incrementModCount(); + } + } + + private static class MessageExternalList< + MType extends AbstractMessage, + BType extends AbstractMessage.Builder, + IType extends MessageOrBuilder> + extends AbstractList implements List, RandomAccess { + + RepeatedFieldBuilderV3 builder; + + @Deprecated + MessageExternalList(RepeatedFieldBuilderV3 builder) { + this.builder = builder; + } + + /* + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * RepeatedFieldBuilder.MessageExternalList.size() instead. + */ + @Deprecated + @Override + public int size() { + return this.builder.getCount(); + } + + /* + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * RepeatedFieldBuilder.MessageExternalList.get() instead. + */ + @Deprecated + @Override + public MType get(int index) { + return builder.getMessage(index); + } + + /* + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * RepeatedFieldBuilder.MessageExternalList.incrementModCount() instead. + */ + @Deprecated + void incrementModCount() { + modCount++; + } + } + + private static class BuilderExternalList< + MType extends AbstractMessage, + BType extends AbstractMessage.Builder, + IType extends MessageOrBuilder> + extends AbstractList implements List, RandomAccess { + + RepeatedFieldBuilderV3 builder; + + @Deprecated + BuilderExternalList(RepeatedFieldBuilderV3 builder) { + this.builder = builder; + } + + /* + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * RepeatedFieldBuilder.BuilderExternalList.size() instead. + */ + @Deprecated + @Override + public int size() { + return this.builder.getCount(); + } + + /* + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * RepeatedFieldBuilder.BuilderExternalList.get() instead. + */ + @Deprecated + @Override + public BType get(int index) { + return builder.getBuilder(index); + } + + /* + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * RepeatedFieldBuilder.BuilderExternalList.incrementModCount() instead. + */ + @Deprecated + void incrementModCount() { + modCount++; + } + } + + private static class MessageOrBuilderExternalList< + MType extends AbstractMessage, + BType extends AbstractMessage.Builder, + IType extends MessageOrBuilder> + extends AbstractList implements List, RandomAccess { + + RepeatedFieldBuilderV3 builder; + + MessageOrBuilderExternalList(RepeatedFieldBuilderV3 builder) { + this.builder = builder; + } + + /* + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * RepeatedFieldBuilder.MessageOrBuilderExternalList.size() instead. + */ + @Deprecated + @Override + public int size() { + return this.builder.getCount(); + } + + /* + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * RepeatedFieldBuilder.MessageOrBuilderExternalList.get() instead. + */ + @Deprecated + @Override + public IType get(int index) { + return builder.getMessageOrBuilder(index); + } + + /* + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * RepeatedFieldBuilder.MessageOrBuilderExternalList.incrementModCount() instead. + */ + @Deprecated + void incrementModCount() { + modCount++; + } } } diff --git a/java/core/src/main/java/com/google/protobuf/SingleFieldBuilderV3.java b/java/core/src/main/java/com/google/protobuf/SingleFieldBuilderV3.java index 3c6b151de2..3b074aece7 100644 --- a/java/core/src/main/java/com/google/protobuf/SingleFieldBuilderV3.java +++ b/java/core/src/main/java/com/google/protobuf/SingleFieldBuilderV3.java @@ -7,8 +7,12 @@ package com.google.protobuf; +import static com.google.protobuf.Internal.checkNotNull; + /** - * Stub for SingleFieldBuilderV3 wrapping SingleFieldBuilder for compatibility with older gencode. + * Stub for SingleFieldBuilderV3 matching SingleFieldBuilder for compatibility with older gencode. + * This cannot wrap SingleFieldBuilder directly due to SingleFieldBuilder having more restrictive + * extends GeneratedMessage for MType and BType. * * @deprecated This class is deprecated, and slated for removal in the next breaking change. Users * should update gencode to >= 4.26.x which replaces SingleFieldBuilderV3 with @@ -16,13 +20,175 @@ package com.google.protobuf; */ @Deprecated public class SingleFieldBuilderV3< - MType extends GeneratedMessage, - BType extends GeneratedMessage.Builder, + MType extends AbstractMessage, + BType extends AbstractMessage.Builder, IType extends MessageOrBuilder> - extends SingleFieldBuilder { + implements AbstractMessage.BuilderParent { + + private AbstractMessage.BuilderParent parent; + + private BType builder; + + private MType message; + private boolean isClean; + + @Deprecated public SingleFieldBuilderV3( - MType message, GeneratedMessage.BuilderParent parent, boolean isClean) { - super(message, parent, isClean); + MType message, AbstractMessage.BuilderParent parent, boolean isClean) { + this.message = checkNotNull(message); + this.parent = parent; + this.isClean = isClean; + } + + /* + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * SingleFieldBuilder.dispose() instead. + */ + @Deprecated + public void dispose() { + parent = null; + } + + /* + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * SingleFieldBuilder.getMessage() instead. + */ + @Deprecated + @SuppressWarnings("unchecked") + public MType getMessage() { + if (message == null) { + message = (MType) builder.buildPartial(); + } + return message; + } + + /* + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * SingleFieldBuilder.build() instead. + */ + @Deprecated + public MType build() { + isClean = true; + return getMessage(); + } + + /* + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * SingleFieldBuilder.getbuilder() instead. + */ + @Deprecated + @SuppressWarnings("unchecked") + public BType getBuilder() { + if (builder == null) { + builder = (BType) message.newBuilderForType(this); + builder.mergeFrom(message); + builder.markClean(); + } + return builder; + } + + /* + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * SingleFieldBuilder.getMessageOrBuilder() instead. + */ + @Deprecated + @SuppressWarnings("unchecked") + public IType getMessageOrBuilder() { + if (builder != null) { + return (IType) builder; + } else { + return (IType) message; + } + } + + /* + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * SingleFieldBuilder.setMessage() instead. + */ + @Deprecated + @CanIgnoreReturnValue + public SingleFieldBuilderV3 setMessage(MType message) { + this.message = checkNotNull(message); + if (builder != null) { + builder.dispose(); + builder = null; + } + onChanged(); + return this; + } + + /* + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * SingleFieldBuilder.mergeFrom() instead. + */ + @Deprecated + @CanIgnoreReturnValue + public SingleFieldBuilderV3 mergeFrom(MType value) { + if (builder == null && message == message.getDefaultInstanceForType()) { + message = value; + } else { + getBuilder().mergeFrom(value); + } + onChanged(); + return this; + } + + /* + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * SingleFieldBuilder.clear() instead. + */ + @Deprecated + @SuppressWarnings("unchecked") + @CanIgnoreReturnValue + public SingleFieldBuilderV3 clear() { + message = + (MType) + (message != null + ? message.getDefaultInstanceForType() + : builder.getDefaultInstanceForType()); + if (builder != null) { + builder.dispose(); + builder = null; + } + onChanged(); + isClean = true; + return this; + } + + /* + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * SingleFieldBuilder.onChanged() instead. + */ + @Deprecated + private void onChanged() { + if (builder != null) { + message = null; + } + if (isClean && parent != null) { + parent.markDirty(); + + isClean = false; + } + } + + /* + * @deprecated This method is deprecated, and slated for removal in the next Java breaking change + * (5.x). Users should update gencode to >= 4.26.x which uses + * SingleFieldBuilder.markDirty() instead. + */ + @Deprecated + @Override + public void markDirty() { + onChanged(); } } diff --git a/java/internal/testing.bzl b/java/internal/testing.bzl index 3939ccadcf..e55f7b5a2a 100644 --- a/java/internal/testing.bzl +++ b/java/internal/testing.bzl @@ -45,6 +45,7 @@ def junit_tests(name, srcs, data = [], deps = [], package_name = "com.google.pro deps = deps, resources = data, data = data, + testonly = True, ) test_names = [] prefix = name.replace("-", "_") + "TestSuite"