From 6fa6350d0a0a09c1aefd4895dcc5bdcf4c2d32e4 Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Wed, 29 May 2024 14:16:17 -0700 Subject: [PATCH] Automated rollback of commit 05e5107d98452f3c1db2737a3c72c8e54f7a4ba0. PiperOrigin-RevId: 638409590 --- .../com/google/protobuf/GeneratedMessage.java | 33 +++---------------- .../protobuf/compiler/java/full/message.cc | 10 +++--- 2 files changed, 10 insertions(+), 33 deletions(-) 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 1e4019a848767..fe532dca0cc21 100644 --- a/java/core/src/main/java/com/google/protobuf/GeneratedMessage.java +++ b/java/core/src/main/java/com/google/protobuf/GeneratedMessage.java @@ -1028,23 +1028,7 @@ public boolean isInitialized() { * numbers, but we must write them in canonical (sorted by field number) order. ExtensionWriter * helps us write individual ranges of extensions at once. */ - protected interface ExtensionWriter { - public void writeUntil(final int end, final CodedOutputStream output) throws IOException; - } - - // Singleton instance so we can avoid allocating a new one for each message serialization. - private static final NoOpExtensionWriter NO_OP_EXTENSION_WRITER = new NoOpExtensionWriter(); - - /** No-op implementation that writes nothing, for messages with no extensions. */ - private static final class NoOpExtensionWriter implements ExtensionWriter { - @Override - public void writeUntil(final int end, final CodedOutputStream output) { - // no-op - } - } - - /** Implementation that writes extensions from the FieldSet, for messages with extensions. */ - private final class FieldSetExtensionWriter implements ExtensionWriter { + protected class ExtensionWriter { // Imagine how much simpler this code would be if Java iterators had // a way to get the next element without advancing the iterator. @@ -1052,14 +1036,13 @@ private final class FieldSetExtensionWriter implements ExtensionWriter { private Map.Entry next; private final boolean messageSetWireFormat; - private FieldSetExtensionWriter(final boolean messageSetWireFormat) { + private ExtensionWriter(final boolean messageSetWireFormat) { if (iter.hasNext()) { next = iter.next(); } this.messageSetWireFormat = messageSetWireFormat; } - @Override public void writeUntil(final int end, final CodedOutputStream output) throws IOException { while (next != null && next.getKey().getNumber() < end) { FieldDescriptor descriptor = next.getKey(); @@ -1093,19 +1076,11 @@ public void writeUntil(final int end, final CodedOutputStream output) throws IOE } protected ExtensionWriter newExtensionWriter() { - // Avoid allocation in the common case of no extensions. - if (extensions.isEmpty()) { - return NO_OP_EXTENSION_WRITER; - } - return new FieldSetExtensionWriter(false); + return new ExtensionWriter(false); } protected ExtensionWriter newMessageSetExtensionWriter() { - // Avoid allocation in the common case of no extensions. - if (extensions.isEmpty()) { - return NO_OP_EXTENSION_WRITER; - } - return new FieldSetExtensionWriter(true); + return new ExtensionWriter(true); } /** Called by subclasses to compute the size of extensions. */ diff --git a/src/google/protobuf/compiler/java/full/message.cc b/src/google/protobuf/compiler/java/full/message.cc index 3456a98b763d4..10abbb90e876f 100644 --- a/src/google/protobuf/compiler/java/full/message.cc +++ b/src/google/protobuf/compiler/java/full/message.cc @@ -587,13 +587,15 @@ void ImmutableMessageGenerator::GenerateMessageSerializationMethods( if (descriptor_->options().message_set_wire_format()) { printer->Print( "com.google.protobuf.GeneratedMessage\n" - " .ExtendableMessage.ExtensionWriter\n" - " extensionWriter = newMessageSetExtensionWriter();\n"); + " .ExtendableMessage<$classname$>.ExtensionWriter\n" + " extensionWriter = newMessageSetExtensionWriter();\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_)); } else { printer->Print( "com.google.protobuf.GeneratedMessage\n" - " .ExtendableMessage.ExtensionWriter\n" - " extensionWriter = newExtensionWriter();\n"); + " .ExtendableMessage<$classname$>.ExtensionWriter\n" + " extensionWriter = newExtensionWriter();\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_)); } }