Avoid an iterator allocation in FieldSet.Builder.setField

Fix some rawtypes warnings too.

PiperOrigin-RevId: 650804398
pull/17360/head
Mark Hansen 5 months ago committed by Copybara-Service
parent cc5eccfb28
commit 2f3b4603a1
  1. 9
      java/core/src/main/java/com/google/protobuf/FieldSet.java

@ -1122,7 +1122,8 @@ final class FieldSet<T extends FieldSet.FieldDescriptorLite<T>> {
* Useful for implementing {@link Message.Builder#setField(Descriptors.FieldDescriptor, * Useful for implementing {@link Message.Builder#setField(Descriptors.FieldDescriptor,
* Object)}. * Object)}.
*/ */
@SuppressWarnings({"unchecked", "rawtypes"}) // Avoid iterator allocation.
@SuppressWarnings({"unchecked", "ForeachList", "ForeachListWithUserVar"})
public void setField(final T descriptor, Object value) { public void setField(final T descriptor, Object value) {
ensureIsMutable(); ensureIsMutable();
if (descriptor.isRepeated()) { if (descriptor.isRepeated()) {
@ -1133,8 +1134,10 @@ final class FieldSet<T extends FieldSet.FieldDescriptorLite<T>> {
// Wrap the contents in a new list so that the caller cannot change // Wrap the contents in a new list so that the caller cannot change
// the list's contents after setting it. // the list's contents after setting it.
final List newList = new ArrayList((List) value); final List<Object> newList = new ArrayList<>((List<Object>) value);
for (final Object element : newList) { int newListSize = newList.size();
for (int i = 0; i < newListSize; i++) {
Object element = newList.get(i);
verifyType(descriptor, element); verifyType(descriptor, element);
hasNestedBuilders = hasNestedBuilders || element instanceof MessageLite.Builder; hasNestedBuilders = hasNestedBuilders || element instanceof MessageLite.Builder;
} }

Loading…
Cancel
Save