File protobuf-763c3588740b97e8e80b1b1a1a2dc4f417647133.patch of Package protobuf.26315
From 763c3588740b97e8e80b1b1a1a2dc4f417647133 Mon Sep 17 00:00:00 2001
From: Paul Yang <TeBoring@users.noreply.github.com>
Date: Thu, 12 Sep 2019 11:03:27 -0700
Subject: [PATCH] Down Integrate Internal Changes (#6634)
* Down integrate to GitHub
* Remove unintentional double assign
* Change ByteCountInt64 to int64_t
* Revert changes in binary_conformance_test.cc
* Revert unnecessary changes
---
java/core/src/main/java/com/google/protobuf/AbstractMessage.java | 2 -
java/core/src/main/java/com/google/protobuf/ByteString.java | 17 ++++++++
java/core/src/main/java/com/google/protobuf/FieldSet.java | 4 +-
java/core/src/main/java/com/google/protobuf/GeneratedMessageLite.java | 4 +-
java/core/src/main/java/com/google/protobuf/GeneratedMessageV3.java | 10 ++---
java/core/src/main/java/com/google/protobuf/MapEntry.java | 2 -
java/core/src/main/java/com/google/protobuf/MapFieldLite.java | 2 -
java/core/src/test/java/com/google/protobuf/ByteStringTest.java | 19 ++++++++++
java/util/src/main/java/com/google/protobuf/util/JsonFormat.java | 8 ++++
java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java | 16 ++++++++
10 files changed, 71 insertions(+), 13 deletions(-)
--- a/java/core/src/main/java/com/google/protobuf/AbstractMessage.java
+++ b/java/core/src/main/java/com/google/protobuf/AbstractMessage.java
@@ -193,7 +193,7 @@ public abstract class AbstractMessage
if (list.isEmpty()) {
return Collections.emptyMap();
}
- Map result = new HashMap();
+ Map result = new HashMap<>();
Iterator iterator = list.iterator();
Message entry = (Message) iterator.next();
Descriptors.Descriptor descriptor = entry.getDescriptorForType();
--- a/java/core/src/main/java/com/google/protobuf/ByteString.java
+++ b/java/core/src/main/java/com/google/protobuf/ByteString.java
@@ -30,6 +30,10 @@
package com.google.protobuf;
+import static com.google.protobuf.TextFormatEscaper.escapeBytes;
+import static java.lang.Integer.toHexString;
+import static java.lang.System.identityHashCode;
+
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@@ -49,6 +53,7 @@ import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
+import java.util.Locale;
import java.util.NoSuchElementException;
/**
@@ -1268,7 +1273,17 @@ public abstract class ByteString impleme
@Override
public final String toString() {
return String.format(
- "<ByteString@%s size=%d>", Integer.toHexString(System.identityHashCode(this)), size());
+ Locale.ROOT,
+ "<ByteString@%s size=%d contents=\"%s\">",
+ toHexString(identityHashCode(this)),
+ size(),
+ truncateAndEscapeForDisplay());
+ }
+
+ private String truncateAndEscapeForDisplay() {
+ final int limit = 50;
+
+ return size() <= limit ? escapeBytes(this) : escapeBytes(substring(0, limit - 3)) + "...";
}
/**
--- a/java/core/src/main/java/com/google/protobuf/FieldSet.java
+++ b/java/core/src/main/java/com/google/protobuf/FieldSet.java
@@ -526,7 +526,7 @@ final class FieldSet<T extends FieldSet.
if (descriptor.isRepeated()) {
Object value = getField(descriptor);
if (value == null) {
- value = new ArrayList();
+ value = new ArrayList<>();
}
for (Object element : (List) otherValue) {
((List) value).add(cloneIfMutable(element));
@@ -1266,7 +1266,7 @@ final class FieldSet<T extends FieldSet.
if (descriptor.isRepeated()) {
Object value = getField(descriptor);
if (value == null) {
- value = new ArrayList();
+ value = new ArrayList<>();
}
for (Object element : (List) otherValue) {
((List) value).add(FieldSet.cloneIfMutable(element));
--- a/java/core/src/main/java/com/google/protobuf/GeneratedMessageLite.java
+++ b/java/core/src/main/java/com/google/protobuf/GeneratedMessageLite.java
@@ -1238,7 +1238,7 @@ public abstract class GeneratedMessageLi
Object fromFieldSetType(final Object value) {
if (descriptor.isRepeated()) {
if (descriptor.getLiteJavaType() == WireFormat.JavaType.ENUM) {
- final List result = new ArrayList();
+ final List result = new ArrayList<>();
for (final Object element : (List) value) {
result.add(singularFromFieldSetType(element));
}
@@ -1263,7 +1263,7 @@ public abstract class GeneratedMessageLi
Object toFieldSetType(final Object value) {
if (descriptor.isRepeated()) {
if (descriptor.getLiteJavaType() == WireFormat.JavaType.ENUM) {
- final List result = new ArrayList();
+ final List result = new ArrayList<>();
for (final Object element : (List) value) {
result.add(singularToFieldSetType(element));
}
--- a/java/core/src/main/java/com/google/protobuf/GeneratedMessageV3.java
+++ b/java/core/src/main/java/com/google/protobuf/GeneratedMessageV3.java
@@ -158,7 +158,7 @@ public abstract class GeneratedMessageV3
/**
* Internal helper to return a modifiable map containing all the fields.
- * The returned Map is modifialbe so that the caller can add additional
+ * The returned Map is modifiable so that the caller can add additional
* extension fields to implement {@link #getAllFields()}.
*
* @param getBytesForString whether to generate ByteString for string fields
@@ -2853,7 +2853,7 @@ public abstract class GeneratedMessageV3
@Override
@SuppressWarnings("unchecked")
public Object get(GeneratedMessageV3 message) {
- List result = new ArrayList();
+ List result = new ArrayList<>();
for (int i = 0; i < getRepeatedCount(message); i++) {
result.add(getRepeated(message, i));
}
@@ -2863,7 +2863,7 @@ public abstract class GeneratedMessageV3
@Override
@SuppressWarnings("unchecked")
public Object get(Builder builder) {
- List result = new ArrayList();
+ List result = new ArrayList<>();
for (int i = 0; i < getRepeatedCount(builder); i++) {
result.add(getRepeated(builder, i));
}
@@ -3068,7 +3068,7 @@ public abstract class GeneratedMessageV3
@Override
@SuppressWarnings("unchecked")
public Object get(final GeneratedMessageV3 message) {
- final List newList = new ArrayList();
+ final List newList = new ArrayList<>();
final int size = getRepeatedCount(message);
for (int i = 0; i < size; i++) {
newList.add(getRepeated(message, i));
@@ -3079,7 +3079,7 @@ public abstract class GeneratedMessageV3
@Override
@SuppressWarnings("unchecked")
public Object get(final GeneratedMessageV3.Builder builder) {
- final List newList = new ArrayList();
+ final List newList = new ArrayList<>();
final int size = getRepeatedCount(builder);
for (int i = 0; i < size; i++) {
newList.add(getRepeated(builder, i));
--- a/java/core/src/main/java/com/google/protobuf/MapEntry.java
+++ b/java/core/src/main/java/com/google/protobuf/MapEntry.java
@@ -439,7 +439,7 @@ public final class MapEntry<K, V> extend
@Override
@SuppressWarnings("unchecked")
public Builder<K, V> clone() {
- return new Builder(metadata, key, value, hasKey, hasValue);
+ return new Builder<>(metadata, key, value, hasKey, hasValue);
}
}
--- a/java/core/src/main/java/com/google/protobuf/MapFieldLite.java
+++ b/java/core/src/main/java/com/google/protobuf/MapFieldLite.java
@@ -58,7 +58,7 @@ public final class MapFieldLite<K, V> ex
}
@SuppressWarnings({"rawtypes", "unchecked"})
- private static final MapFieldLite EMPTY_MAP_FIELD = new MapFieldLite();
+ private static final MapFieldLite EMPTY_MAP_FIELD = new MapFieldLite<>();
static {
EMPTY_MAP_FIELD.makeImmutable();
--- a/java/core/src/test/java/com/google/protobuf/ByteStringTest.java
+++ b/java/core/src/test/java/com/google/protobuf/ByteStringTest.java
@@ -486,6 +486,25 @@ public class ByteStringTest extends Test
"copyToStringUtf8 must respect the charset", testString, byteString.toStringUtf8());
}
+ public void testToString() {
+ String toString =
+ ByteString.copyFrom("Here are some bytes: \t\u00a1".getBytes(Internal.UTF_8)).toString();
+ assertTrue(toString, toString.contains("size=24"));
+ assertTrue(toString, toString.contains("contents=\"Here are some bytes: \\t\\302\\241\""));
+ }
+
+ public void testToString_long() {
+ String toString =
+ ByteString.copyFrom(
+ "123456789012345678901234567890123456789012345678901234567890"
+ .getBytes(Internal.UTF_8))
+ .toString();
+ assertTrue(toString, toString.contains("size=60"));
+ assertTrue(
+ toString,
+ toString.contains("contents=\"12345678901234567890123456789012345678901234567...\""));
+ }
+
public void testNewOutput_InitialCapacity() throws IOException {
byte[] bytes = getTestBytes();
ByteString.Output output = ByteString.newOutput(bytes.length + 100);
--- a/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java
+++ b/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java
@@ -1945,6 +1945,14 @@ public class JsonFormat {
return field.getEnumType().findValueByNumber(0);
}
return null;
+ } else if (json instanceof JsonObject) {
+ if (field.getType() != FieldDescriptor.Type.MESSAGE
+ && field.getType() != FieldDescriptor.Type.GROUP) {
+ // If the field type is primitive, but the json type is JsonObject rather than
+ // JsonElement, throw a type mismatch error.
+ throw new InvalidProtocolBufferException(
+ String.format("Invalid value: %s for expected type: %s", json, field.getType()));
+ }
}
switch (field.getType()) {
case INT32:
--- a/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java
+++ b/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java
@@ -1723,6 +1723,22 @@ public class JsonFormatTest extends Test
}
}
+ // Test that an error is thrown if a nested JsonObject is parsed as a primitive field.
+ public void testJsonObjectForPrimitiveField() throws Exception {
+ TestAllTypes.Builder builder = TestAllTypes.newBuilder();
+ try {
+ mergeFromJson(
+ "{\n"
+ + " \"optionalString\": {\n"
+ + " \"invalidNestedString\": \"Hello world\"\n"
+ + " }\n"
+ + "}\n",
+ builder);
+ } catch (InvalidProtocolBufferException e) {
+ // Expected.
+ }
+ }
+
public void testSortedMapKeys() throws Exception {
TestMap.Builder mapBuilder = TestMap.newBuilder();
mapBuilder.putStringToInt32Map("\ud834\udd20", 3); // utf-8 F0 9D 84 A0