diff options
Diffstat (limited to 'java')
-rw-r--r-- | java/core/pom.xml | 2 | ||||
-rw-r--r-- | java/core/src/main/java/com/google/protobuf/CodedOutputStream.java | 38 | ||||
-rw-r--r-- | java/core/src/main/java/com/google/protobuf/DynamicMessage.java | 8 | ||||
-rw-r--r-- | java/core/src/main/java/com/google/protobuf/GeneratedMessage.java | 8 | ||||
-rw-r--r-- | java/core/src/test/java/com/google/protobuf/FieldPresenceTest.java | 50 | ||||
-rw-r--r-- | java/core/src/test/java/com/google/protobuf/LazyStringArrayListTest.java | 3 | ||||
-rw-r--r-- | java/lite/pom.xml | 2 | ||||
-rw-r--r-- | java/pom.xml | 4 | ||||
-rw-r--r-- | java/util/pom.xml | 2 | ||||
-rw-r--r-- | java/util/src/main/java/com/google/protobuf/util/JsonFormat.java | 40 | ||||
-rw-r--r-- | java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java | 22 |
11 files changed, 152 insertions, 27 deletions
diff --git a/java/core/pom.xml b/java/core/pom.xml index 0d4c5c75..422bde82 100644 --- a/java/core/pom.xml +++ b/java/core/pom.xml @@ -6,7 +6,7 @@ <parent> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-parent</artifactId> - <version>3.0.0-beta-3</version> + <version>3.0.0-beta-4</version> </parent> <artifactId>protobuf-java</artifactId> diff --git a/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java b/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java index 576a350f..e5515285 100644 --- a/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java +++ b/java/core/src/main/java/com/google/protobuf/CodedOutputStream.java @@ -145,6 +145,44 @@ public abstract class CodedOutputStream extends ByteOutput { } /** + * Configures serialization to be deterministic. + * + * <p>The deterministic serialization guarantees that for a given binary, equal (defined by the + * {@code equals()} methods in protos) messages will always be serialized to the same bytes. This + * implies: + * + * <ul> + * <li>repeated serialization of a message will return the same bytes + * <li>different processes of the same binary (which may be executing on different machines) will + * serialize equal messages to the same bytes. + * </ul> + * + * <p>Note the deterministic serialization is NOT canonical across languages; it is also unstable + * across different builds with schema changes due to unknown fields. Users who need canonical + * serialization, e.g. persistent storage in a canonical form, fingerprinting, etc, should define + * their own canonicalization specification and implement the serializer using reflection APIs + * rather than relying on this API. + * + * <p> Once set, the serializer will: (Note this is an implementation detail and may subject to + * change in the future) + * + * <ul> + * <li> sort map entries by keys in lexicographical order or numerical order. Note: For string + * keys, the order is based on comparing the Unicode value of each character in the strings. + * The order may be different from the deterministic serialization in other languages where + * maps are sorted on the lexicographical order of the UTF8 encoded keys. + * </ul> + */ + void useDeterministicSerialization() { + serializationDeterministic = true; + } + + boolean isSerializationDeterministic() { + return serializationDeterministic; + } + private boolean serializationDeterministic; + + /** * Create a new {@code CodedOutputStream} that writes to the given {@link ByteBuffer}. * * @deprecated the size parameter is no longer used since use of an internal buffer is useless diff --git a/java/core/src/main/java/com/google/protobuf/DynamicMessage.java b/java/core/src/main/java/com/google/protobuf/DynamicMessage.java index 859a9e8f..c54da67f 100644 --- a/java/core/src/main/java/com/google/protobuf/DynamicMessage.java +++ b/java/core/src/main/java/com/google/protobuf/DynamicMessage.java @@ -526,6 +526,14 @@ public final class DynamicMessage extends AbstractMessage { fields.clearField(oldField); } oneofCases[index] = field; + } else if (field.getFile().getSyntax() == Descriptors.FileDescriptor.Syntax.PROTO3) { + if (!field.isRepeated() + && field.getJavaType() != FieldDescriptor.JavaType.MESSAGE + && value.equals(field.getDefaultValue())) { + // In proto3, setting a field to its default value is equivalent to clearing the field. + fields.clearField(field); + return this; + } } fields.setField(field, value); return this; 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 2c87302b..cea05794 100644 --- a/java/core/src/main/java/com/google/protobuf/GeneratedMessage.java +++ b/java/core/src/main/java/com/google/protobuf/GeneratedMessage.java @@ -1396,7 +1396,7 @@ public abstract class GeneratedMessage extends AbstractMessage return setExtension((ExtensionLite<MessageType, Type>) extension, value); } /** Set the value of an extension. */ - public final <Type> BuilderType setExtension( + public <Type> BuilderType setExtension( final GeneratedExtension<MessageType, Type> extension, final Type value) { return setExtension((ExtensionLite<MessageType, Type>) extension, value); } @@ -1407,7 +1407,7 @@ public abstract class GeneratedMessage extends AbstractMessage return setExtension((ExtensionLite<MessageType, List<Type>>) extension, index, value); } /** Set the value of one element of a repeated extension. */ - public final <Type> BuilderType setExtension( + public <Type> BuilderType setExtension( final GeneratedExtension<MessageType, List<Type>> extension, final int index, final Type value) { return setExtension((ExtensionLite<MessageType, List<Type>>) extension, index, value); @@ -1418,7 +1418,7 @@ public abstract class GeneratedMessage extends AbstractMessage return addExtension((ExtensionLite<MessageType, List<Type>>) extension, value); } /** Append a value to a repeated extension. */ - public final <Type> BuilderType addExtension( + public <Type> BuilderType addExtension( final GeneratedExtension<MessageType, List<Type>> extension, final Type value) { return addExtension((ExtensionLite<MessageType, List<Type>>) extension, value); } @@ -1428,7 +1428,7 @@ public abstract class GeneratedMessage extends AbstractMessage return clearExtension((ExtensionLite<MessageType, ?>) extension); } /** Clear an extension. */ - public final <Type> BuilderType clearExtension( + public <Type> BuilderType clearExtension( final GeneratedExtension<MessageType, ?> extension) { return clearExtension((ExtensionLite<MessageType, ?>) extension); } diff --git a/java/core/src/test/java/com/google/protobuf/FieldPresenceTest.java b/java/core/src/test/java/com/google/protobuf/FieldPresenceTest.java index 82f4216b..4a42c897 100644 --- a/java/core/src/test/java/com/google/protobuf/FieldPresenceTest.java +++ b/java/core/src/test/java/com/google/protobuf/FieldPresenceTest.java @@ -31,6 +31,8 @@ package com.google.protobuf; import com.google.protobuf.Descriptors.Descriptor; +import com.google.protobuf.Descriptors.EnumDescriptor; +import com.google.protobuf.Descriptors.EnumValueDescriptor; import com.google.protobuf.Descriptors.FieldDescriptor; import com.google.protobuf.FieldPresenceTestProto.TestAllTypes; import com.google.protobuf.FieldPresenceTestProto.TestOptionalFieldsOnly; @@ -253,6 +255,54 @@ public class FieldPresenceTest extends TestCase { assertEquals(4, message.getAllFields().size()); } + public void testFieldPresenceDynamicMessage() { + Descriptor descriptor = TestAllTypes.getDescriptor(); + FieldDescriptor optionalInt32Field = descriptor.findFieldByName("optional_int32"); + FieldDescriptor optionalStringField = descriptor.findFieldByName("optional_string"); + FieldDescriptor optionalBytesField = descriptor.findFieldByName("optional_bytes"); + FieldDescriptor optionalNestedEnumField = descriptor.findFieldByName("optional_nested_enum"); + EnumDescriptor enumDescriptor = optionalNestedEnumField.getEnumType(); + EnumValueDescriptor defaultEnumValueDescriptor = enumDescriptor.getValues().get(0); + EnumValueDescriptor nonDefaultEnumValueDescriptor = enumDescriptor.getValues().get(1); + + DynamicMessage defaultInstance = DynamicMessage.getDefaultInstance(descriptor); + // Field not present. + DynamicMessage message = defaultInstance.newBuilderForType().build(); + assertFalse(message.hasField(optionalInt32Field)); + assertFalse(message.hasField(optionalStringField)); + assertFalse(message.hasField(optionalBytesField)); + assertFalse(message.hasField(optionalNestedEnumField)); + assertEquals(0, message.getAllFields().size()); + + // Field set to non-default value is seen as present. + message = + defaultInstance + .newBuilderForType() + .setField(optionalInt32Field, 1) + .setField(optionalStringField, "x") + .setField(optionalBytesField, ByteString.copyFromUtf8("y")) + .setField(optionalNestedEnumField, nonDefaultEnumValueDescriptor) + .build(); + assertTrue(message.hasField(optionalInt32Field)); + assertTrue(message.hasField(optionalStringField)); + assertTrue(message.hasField(optionalBytesField)); + assertTrue(message.hasField(optionalNestedEnumField)); + assertEquals(4, message.getAllFields().size()); + + // Field set to default value is seen as not present. + message = message.toBuilder() + .setField(optionalInt32Field, 0) + .setField(optionalStringField, "") + .setField(optionalBytesField, ByteString.EMPTY) + .setField(optionalNestedEnumField, defaultEnumValueDescriptor) + .build(); + assertFalse(message.hasField(optionalInt32Field)); + assertFalse(message.hasField(optionalStringField)); + assertFalse(message.hasField(optionalBytesField)); + assertFalse(message.hasField(optionalNestedEnumField)); + assertEquals(0, message.getAllFields().size()); + } + public void testMessageField() { TestAllTypes.Builder builder = TestAllTypes.newBuilder(); assertFalse(builder.hasOptionalNestedMessage()); diff --git a/java/core/src/test/java/com/google/protobuf/LazyStringArrayListTest.java b/java/core/src/test/java/com/google/protobuf/LazyStringArrayListTest.java index 0f42ac50..497c4df2 100644 --- a/java/core/src/test/java/com/google/protobuf/LazyStringArrayListTest.java +++ b/java/core/src/test/java/com/google/protobuf/LazyStringArrayListTest.java @@ -35,6 +35,7 @@ import static java.util.Arrays.asList; import junit.framework.TestCase; import java.util.ArrayList; +import java.util.Collections; import java.util.ConcurrentModificationException; import java.util.Iterator; import java.util.List; @@ -233,7 +234,7 @@ public class LazyStringArrayListTest extends TestCase { } try { - list.addAllByteArray(asList(BYTE_STRING_A.toByteArray())); + list.addAllByteArray(Collections.singletonList(BYTE_STRING_A.toByteArray())); fail(); } catch (UnsupportedOperationException e) { // expected diff --git a/java/lite/pom.xml b/java/lite/pom.xml index c403dc09..87a5c44d 100644 --- a/java/lite/pom.xml +++ b/java/lite/pom.xml @@ -6,7 +6,7 @@ <parent> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-parent</artifactId> - <version>3.0.0-beta-3</version> + <version>3.0.0-beta-4</version> </parent> <artifactId>protobuf-lite</artifactId> diff --git a/java/pom.xml b/java/pom.xml index 7a1a91f8..2d7161b2 100644 --- a/java/pom.xml +++ b/java/pom.xml @@ -11,7 +11,7 @@ <groupId>com.google.protobuf</groupId> <artifactId>protobuf-parent</artifactId> - <version>3.0.0-beta-3</version> + <version>3.0.0-beta-4</version> <packaging>pom</packaging> <name>Protocol Buffers [Parent]</name> @@ -182,7 +182,7 @@ <modules> <module>core</module> - <module>lite</module> + <!-- <module>lite</module> --> <module>util</module> </modules> diff --git a/java/util/pom.xml b/java/util/pom.xml index 9236f907..d8f64eb1 100644 --- a/java/util/pom.xml +++ b/java/util/pom.xml @@ -6,7 +6,7 @@ <parent> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-parent</artifactId> - <version>3.0.0-beta-3</version> + <version>3.0.0-beta-4</version> </parent> <artifactId>protobuf-java-util</artifactId> diff --git a/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java b/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java index 297545e5..ad50cc0e 100644 --- a/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java +++ b/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java @@ -116,7 +116,8 @@ public class JsonFormat { private Printer( TypeRegistry registry, boolean includingDefaultValueFields, - boolean preservingProtoFieldNames, boolean omittingInsignificantWhitespace) { + boolean preservingProtoFieldNames, + boolean omittingInsignificantWhitespace) { this.registry = registry; this.includingDefaultValueFields = includingDefaultValueFields; this.preservingProtoFieldNames = preservingProtoFieldNames; @@ -133,7 +134,11 @@ public class JsonFormat { if (this.registry != TypeRegistry.getEmptyTypeRegistry()) { throw new IllegalArgumentException("Only one registry is allowed."); } - return new Printer(registry, includingDefaultValueFields, preservingProtoFieldNames, omittingInsignificantWhitespace); + return new Printer( + registry, + includingDefaultValueFields, + preservingProtoFieldNames, + omittingInsignificantWhitespace); } /** @@ -143,7 +148,8 @@ public class JsonFormat { * {@link Printer}. */ public Printer includingDefaultValueFields() { - return new Printer(registry, true, preservingProtoFieldNames, omittingInsignificantWhitespace); + return new Printer( + registry, true, preservingProtoFieldNames, omittingInsignificantWhitespace); } /** @@ -153,7 +159,8 @@ public class JsonFormat { * current {@link Printer}. */ public Printer preservingProtoFieldNames() { - return new Printer(registry, includingDefaultValueFields, true, omittingInsignificantWhitespace); + return new Printer( + registry, includingDefaultValueFields, true, omittingInsignificantWhitespace); } @@ -172,7 +179,7 @@ public class JsonFormat { * See <a href="https://tools.ietf.org/html/rfc7159">https://tools.ietf.org/html/rfc7159</a> * current {@link Printer}. */ - public Printer omittingInsignificantWhitespace(){ + public Printer omittingInsignificantWhitespace() { return new Printer(registry, includingDefaultValueFields, preservingProtoFieldNames, true); } @@ -186,7 +193,12 @@ public class JsonFormat { public void appendTo(MessageOrBuilder message, Appendable output) throws IOException { // TODO(xiaofeng): Investigate the allocation overhead and optimize for // mobile. - new PrinterImpl(registry, includingDefaultValueFields, preservingProtoFieldNames, output, omittingInsignificantWhitespace) + new PrinterImpl( + registry, + includingDefaultValueFields, + preservingProtoFieldNames, + output, + omittingInsignificantWhitespace) .print(message); } @@ -379,18 +391,18 @@ public class JsonFormat { */ interface TextGenerator { void indent(); + void outdent(); + void print(final CharSequence text) throws IOException; } - /** * Format the json without indentation */ - private static final class CompactTextGenerator implements TextGenerator{ + private static final class CompactTextGenerator implements TextGenerator { private final Appendable output; - private CompactTextGenerator(final Appendable output) { this.output = output; } @@ -411,12 +423,11 @@ public class JsonFormat { public void print(final CharSequence text) throws IOException { output.append(text); } - } /** * A TextGenerator adds indentation when writing formatted text. */ - private static final class PrettyTextGenerator implements TextGenerator{ + private static final class PrettyTextGenerator implements TextGenerator { private final Appendable output; private final StringBuilder indent = new StringBuilder(); private boolean atStartOfLine = true; @@ -496,7 +507,8 @@ public class JsonFormat { TypeRegistry registry, boolean includingDefaultValueFields, boolean preservingProtoFieldNames, - Appendable jsonOutput, boolean omittingInsignificantWhitespace) { + Appendable jsonOutput, + boolean omittingInsignificantWhitespace) { this.registry = registry; this.includingDefaultValueFields = includingDefaultValueFields; this.preservingProtoFieldNames = preservingProtoFieldNames; @@ -734,9 +746,7 @@ public class JsonFormat { } /** Prints a regular message with an optional type URL. */ - - private void print(MessageOrBuilder message, String typeUrl) - throws IOException { + private void print(MessageOrBuilder message, String typeUrl) throws IOException { generator.print("{" + blankOrNewLine); generator.indent(); diff --git a/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java b/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java index e68c7be1..6fc784ef 100644 --- a/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java +++ b/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java @@ -140,7 +140,7 @@ public class JsonFormatTest extends TestCase { private String toJsonString(Message message) throws IOException { return JsonFormat.printer().print(message); } - private String toCompactJsonString(Message message) throws IOException{ + private String toCompactJsonString(Message message) throws IOException { return JsonFormat.printer().omittingInsignificantWhitespace().print(message); } @@ -1172,7 +1172,9 @@ public class JsonFormatTest extends TestCase { public void testOmittingInsignificantWhiteSpace() throws Exception { TestAllTypes message = TestAllTypes.newBuilder().setOptionalInt32(12345).build(); - assertEquals("{" + "\"optionalInt32\":12345" + "}", JsonFormat.printer().omittingInsignificantWhitespace().print(message)); + assertEquals( + "{" + "\"optionalInt32\":12345" + "}", + JsonFormat.printer().omittingInsignificantWhitespace().print(message)); TestAllTypes message1 = TestAllTypes.getDefaultInstance(); assertEquals("{}", JsonFormat.printer().omittingInsignificantWhitespace().print(message1)); TestAllTypes.Builder builder = TestAllTypes.newBuilder(); @@ -1224,4 +1226,20 @@ public class JsonFormatTest extends TestCase { toCompactJsonString(message2)); } + // Regression test for b/29892357 + public void testEmptyWrapperTypesInAny() throws Exception { + JsonFormat.TypeRegistry registry = + JsonFormat.TypeRegistry.newBuilder().add(TestAllTypes.getDescriptor()).build(); + JsonFormat.Parser parser = JsonFormat.parser().usingTypeRegistry(registry); + + Any.Builder builder = Any.newBuilder(); + parser.merge( + "{\n" + + " \"@type\": \"type.googleapis.com/google.protobuf.BoolValue\",\n" + + " \"value\": false\n" + + "}\n", + builder); + Any any = builder.build(); + assertEquals(0, any.getValue().size()); + } } |