Skip to content

Commit e729bc6

Browse files
habermancopybara-github
authored andcommitted
Removed dependency CodedOutputStream -> LazyFieldLite.
This is in line with the larger goal of making `CodedOutputStream` not depend on specific message representations, which `LazyFieldLite` is. PiperOrigin-RevId: 793672999
1 parent eac8c06 commit e729bc6

File tree

4 files changed

+46
-19
lines changed

4 files changed

+46
-19
lines changed

java/core/src/main/java/com/google/protobuf/CodedOutputStream.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -616,11 +616,13 @@ public static int computeByteBufferSize(final int fieldNumber, final ByteBuffer
616616
}
617617

618618
/**
619-
* Compute the number of bytes that would be needed to encode an embedded message in lazy field,
620-
* including tag.
619+
* Compute the number of bytes that would be needed to encode an embedded message in lazy
620+
* field, including tag.
621621
*/
622+
@Deprecated
623+
@InlineMe(replacement = "value.computeSize(fieldNumber)")
622624
public static int computeLazyFieldSize(final int fieldNumber, final LazyFieldLite value) {
623-
return computeTagSize(fieldNumber) + computeLazyFieldSizeNoTag(value);
625+
return value.computeSize(fieldNumber);
624626
}
625627

626628
/**
@@ -653,14 +655,15 @@ public static int computeRawMessageSetExtensionSize(
653655
}
654656

655657
/**
656-
* Compute the number of bytes that would be needed to encode a lazily parsed MessageSet extension
657-
* field to the stream. For historical reasons, the wire format differs from normal fields.
658+
* Compute the number of bytes that would be needed to encode a lazily parsed MessageSet
659+
* extension field to the stream. For historical reasons, the wire format differs from normal
660+
* fields.
658661
*/
662+
@Deprecated
663+
@InlineMe(replacement = "value.computeMessageSetExtensionSize(fieldNumber)")
659664
public static int computeLazyFieldMessageSetExtensionSize(
660665
final int fieldNumber, final LazyFieldLite value) {
661-
return computeTagSize(WireFormat.MESSAGE_SET_ITEM) * 2
662-
+ computeUInt32Size(WireFormat.MESSAGE_SET_TYPE_ID, fieldNumber)
663-
+ computeLazyFieldSize(WireFormat.MESSAGE_SET_MESSAGE, value);
666+
return value.computeMessageSetExtensionSize(fieldNumber);
664667
}
665668

666669
// -----------------------------------------------------------------
@@ -814,12 +817,11 @@ public static int computeStringSizeNoTag(final String value) {
814817
return computeLengthDelimitedFieldSize(length);
815818
}
816819

817-
/**
818-
* Compute the number of bytes that would be needed to encode an embedded message stored in lazy
819-
* field.
820-
*/
820+
/** Compute the number of bytes that would be needed to encode a {@code bytes} field. */
821+
@Deprecated
822+
@InlineMe(replacement = "value.computeSizeNoTag()")
821823
public static int computeLazyFieldSizeNoTag(final LazyFieldLite value) {
822-
return computeLengthDelimitedFieldSize(value.getSerializedSize());
824+
return value.computeSizeNoTag();
823825
}
824826

825827
/** Compute the number of bytes that would be needed to encode a {@code bytes} field. */

java/core/src/main/java/com/google/protobuf/FieldSet.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -837,8 +837,7 @@ private int getMessageSetSerializedSize(final Map.Entry<T, Object> entry) {
837837
&& !descriptor.isRepeated()
838838
&& !descriptor.isPacked()) {
839839
if (value instanceof LazyField) {
840-
return CodedOutputStream.computeLazyFieldMessageSetExtensionSize(
841-
entry.getKey().getNumber(), (LazyField) value);
840+
return ((LazyField) value).computeMessageSetExtensionSize(entry.getKey().getNumber());
842841
} else {
843842
return CodedOutputStream.computeMessageSetExtensionSize(
844843
entry.getKey().getNumber(), (MessageLite) value);
@@ -921,7 +920,7 @@ static int computeElementSizeNoTag(final WireFormat.FieldType type, final Object
921920

922921
case MESSAGE:
923922
if (value instanceof LazyField) {
924-
return CodedOutputStream.computeLazyFieldSizeNoTag((LazyField) value);
923+
return ((LazyField) value).computeSizeNoTag();
925924
} else {
926925
return CodedOutputStream.computeMessageSizeNoTag((MessageLite) value);
927926
}

java/core/src/main/java/com/google/protobuf/LazyFieldLite.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,32 @@ public ByteString toByteString() {
366366
}
367367
}
368368

369+
/**
370+
* Compute the number of bytes that would be needed to encode an embedded message stored in lazy
371+
* field.
372+
*/
373+
public int computeSizeNoTag() {
374+
return CodedOutputStream.computeLengthDelimitedFieldSize(getSerializedSize());
375+
}
376+
377+
/**
378+
* Compute the number of bytes that would be needed to encode an embedded message in lazy field,
379+
* including tag.
380+
*/
381+
public int computeSize(final int fieldNumber) {
382+
return CodedOutputStream.computeTagSize(fieldNumber) + computeSizeNoTag();
383+
}
384+
385+
/**
386+
* Compute the number of bytes that would be needed to encode a lazily parsed MessageSet extension
387+
* field to the stream. For historical reasons, the wire format differs from normal fields.
388+
*/
389+
public int computeMessageSetExtensionSize(final int fieldNumber) {
390+
return CodedOutputStream.computeTagSize(WireFormat.MESSAGE_SET_ITEM) * 2
391+
+ CodedOutputStream.computeUInt32Size(WireFormat.MESSAGE_SET_TYPE_ID, fieldNumber)
392+
+ computeSize(WireFormat.MESSAGE_SET_MESSAGE);
393+
}
394+
369395
/** Writes this lazy field into a {@link Writer}. */
370396
void writeTo(Writer writer, int fieldNumber) throws IOException {
371397
if (memoizedBytes != null) {

java/core/src/main/java/com/google/protobuf/SchemaUtil.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ static int computeSizeStringList(int fieldNumber, List<?> list) {
646646

647647
static int computeSizeMessage(int fieldNumber, Object value, Schema<?> schema) {
648648
if (value instanceof LazyFieldLite) {
649-
return CodedOutputStream.computeLazyFieldSize(fieldNumber, (LazyFieldLite) value);
649+
return ((LazyFieldLite) value).computeSize(fieldNumber);
650650
} else {
651651
return computeMessageSize(fieldNumber, (AbstractMessageLite) value, schema);
652652
}
@@ -678,7 +678,7 @@ static int computeSizeMessageList(int fieldNumber, List<?> list) {
678678
for (int i = 0; i < length; i++) {
679679
Object value = list.get(i);
680680
if (value instanceof LazyFieldLite) {
681-
size += CodedOutputStream.computeLazyFieldSizeNoTag((LazyFieldLite) value);
681+
size += ((LazyFieldLite) value).computeSizeNoTag();
682682
} else {
683683
size += CodedOutputStream.computeMessageSizeNoTag((MessageLite) value);
684684
}
@@ -695,7 +695,7 @@ static int computeSizeMessageList(int fieldNumber, List<?> list, Schema<?> schem
695695
for (int i = 0; i < length; i++) {
696696
Object value = list.get(i);
697697
if (value instanceof LazyFieldLite) {
698-
size += CodedOutputStream.computeLazyFieldSizeNoTag((LazyFieldLite) value);
698+
size += ((LazyFieldLite) value).computeSizeNoTag();
699699
} else {
700700
size += computeMessageSizeNoTag((AbstractMessageLite) value, schema);
701701
}

0 commit comments

Comments
 (0)