Merge alpha branch 'review/v3.0.0-alpha-2'

changes/13/217213/1
Jisi Liu 10 years ago
commit 581be24606
  1. 65
      CHANGES.txt
  2. 42
      Makefile.am
  3. 4
      configure.ac
  4. 4
      java/pom.xml
  5. 50
      java/src/main/java/com/google/protobuf/GeneratedMessage.java
  6. 14
      java/src/test/java/com/google/protobuf/GeneratedMessageTest.java
  7. 171
      java/src/test/java/com/google/protobuf/MapTest.java
  8. 22
      javanano/README.txt
  9. 4
      javanano/pom.xml
  10. 2
      post_process_dist.sh
  11. 2
      python/setup.py
  12. 48
      ruby/README.md
  13. 2
      ruby/google-protobuf.gemspec
  14. 6
      src/google/protobuf/compiler/command_line_interface_unittest.cc
  15. 1
      src/google/protobuf/compiler/java/java_message_field.cc
  16. 6
      src/google/protobuf/generated_message_reflection.cc
  17. 24
      src/google/protobuf/message.cc
  18. 6
      src/google/protobuf/stubs/singleton.h
  19. 4
      vsprojects/extract_includes.bat
  20. 1054
      vsprojects/libprotoc.vcproj

@ -1,3 +1,68 @@
2015-02-22 version 3.0.0-alpha-2 (Ruby/JavaNano):
General
* Introduced two new language implementations (Ruby and JavaNano) to proto3.
* Various bug fixes since 3.0.0-alpha-1
Ruby:
We have added proto3 support for Ruby via a native C extension.
The Ruby extension itself is included in the ruby/ directory, and details on
building and installing the extension are in ruby/README.md. The extension
will also be published as a Ruby gem. Code generator support is included as
part of `protoc` with the `--ruby_out` flag.
The Ruby extension implements a user-friendly DSL to define message types
(also generated by the code generator from `.proto` files). Once a message
type is defined, the user may create instances of the message that behave in
ways idiomatic to Ruby. For example:
- Message fields are present as ordinary Ruby properties (getter method
`foo` and setter method `foo=`).
- Repeated field elements are stored in a container that acts like a native
Ruby array, and map elements are stored in a container that acts like a
native Ruby hashmap.
- The usual well-known methods, such as `#to_s`, `#dup`, and the like, are
present.
Unlike several existing third-party Ruby extensions for protobuf, this
extension is built on a "strongly-typed" philosophy: message fields and
array/map containers will throw exceptions eagerly when values of the
incorrect type are inserted.
See ruby/README.md for details.
JavaNano:
JavaNano is a special code generator and runtime library designed especially
for resource-restricted systems, like Android. It is very resource-friendly
in both the amount of code and the runtime overhead. Here is an an overview
of JavaNano features compared with the official Java protobuf:
- No descriptors or message builders.
- All messages are mutable; fields are public Java fields.
- For optional fields only, encapsulation behind setter/getter/hazzer/
clearer functions is opt-in, which provide proper 'has' state support.
- For proto2, if not opted in, has state (field presence) is not available.
Serialization outputs all fields not equal to their defaults.
The behavior is consistent with proto3 semantics.
- Required fields (proto2 only) are always serialized.
- Enum constants are integers; protection against invalid values only
when parsing from the wire.
- Enum constants can be generated into container interfaces bearing
the enum's name (so the referencing code is in Java style).
- CodedInputByteBufferNano can only take byte[] (not InputStream).
- Similarly CodedOutputByteBufferNano can only write to byte[].
- Repeated fields are in arrays, not ArrayList or Vector. Null array
elements are allowed and silently ignored.
- Full support for serializing/deserializing repeated packed fields.
- Support extensions (in proto2).
- Unset messages/groups are null, not an immutable empty default
instance.
- toByteArray(...) and mergeFrom(...) are now static functions of
MessageNano.
- The 'bytes' type translates to the Java type byte[].
See javanano/README.txt for details.
2014-12-01 version 3.0.0-alpha-1 (C++/Java): 2014-12-01 version 3.0.0-alpha-1 (C++/Java):
General General

@ -154,6 +154,46 @@ java_EXTRA_DIST= \
java/pom.xml \ java/pom.xml \
java/README.txt java/README.txt
javanano_EXTRA_DIST= \
javanano/src/main/java/com/google/protobuf/nano/CodedOutputByteBufferNano.java \
javanano/src/main/java/com/google/protobuf/nano/FieldData.java \
javanano/src/main/java/com/google/protobuf/nano/FieldArray.java \
javanano/src/main/java/com/google/protobuf/nano/WireFormatNano.java \
javanano/src/main/java/com/google/protobuf/nano/Extension.java \
javanano/src/main/java/com/google/protobuf/nano/CodedInputByteBufferNano.java \
javanano/src/main/java/com/google/protobuf/nano/UnknownFieldData.java \
javanano/src/main/java/com/google/protobuf/nano/MessageNano.java \
javanano/src/main/java/com/google/protobuf/nano/InternalNano.java \
javanano/src/main/java/com/google/protobuf/nano/InvalidProtocolBufferNanoException.java \
javanano/src/main/java/com/google/protobuf/nano/MapFactories.java \
javanano/src/main/java/com/google/protobuf/nano/ExtendableMessageNano.java \
javanano/src/main/java/com/google/protobuf/nano/MessageNanoPrinter.java \
javanano/src/test/java/com/google/protobuf/nano/unittest_accessors_nano.proto \
javanano/src/test/java/com/google/protobuf/nano/unittest_enum_class_nano.proto \
javanano/src/test/java/com/google/protobuf/nano/unittest_reference_types_nano.proto \
javanano/src/test/java/com/google/protobuf/nano/unittest_extension_repeated_nano.proto \
javanano/src/test/java/com/google/protobuf/nano/unittest_has_nano.proto \
javanano/src/test/java/com/google/protobuf/nano/unittest_nano.proto \
javanano/src/test/java/com/google/protobuf/nano/unittest_multiple_nameclash_nano.proto \
javanano/src/test/java/com/google/protobuf/nano/unittest_single_nano.proto \
javanano/src/test/java/com/google/protobuf/nano/NanoTest.java \
javanano/src/test/java/com/google/protobuf/nano/unittest_simple_nano.proto \
javanano/src/test/java/com/google/protobuf/nano/unittest_import_nano.proto \
javanano/src/test/java/com/google/protobuf/nano/unittest_repeated_merge_nano.proto \
javanano/src/test/java/com/google/protobuf/nano/unittest_extension_nano.proto \
javanano/src/test/java/com/google/protobuf/nano/unittest_repeated_packables_nano.proto \
javanano/src/test/java/com/google/protobuf/nano/unittest_extension_singular_nano.proto \
javanano/src/test/java/com/google/protobuf/nano/unittest_recursive_nano.proto \
javanano/src/test/java/com/google/protobuf/nano/unittest_extension_packed_nano.proto \
javanano/src/test/java/com/google/protobuf/nano/unittest_enum_validity_nano.proto \
javanano/src/test/java/com/google/protobuf/nano/unittest_stringutf8_nano.proto \
javanano/src/test/java/com/google/protobuf/nano/unittest_multiple_nano.proto \
javanano/src/test/java/com/google/protobuf/nano/unittest_enum_class_multiple_nano.proto \
javanano/src/test/java/com/google/protobuf/nano/map_test.proto \
javanano/README.txt \
javanano/pom.xml
python_EXTRA_DIST= \ python_EXTRA_DIST= \
python/google/protobuf/internal/api_implementation.cc \ python/google/protobuf/internal/api_implementation.cc \
python/google/protobuf/internal/api_implementation.py \ python/google/protobuf/internal/api_implementation.py \
@ -260,7 +300,7 @@ ruby_EXTRA_DIST= \
ruby/tests/generated_code.rb \ ruby/tests/generated_code.rb \
ruby/tests/generated_code_test.rb ruby/tests/generated_code_test.rb
all_EXTRA_DIST=$(java_EXTRA_DIST) $(python_EXTRA_DIST) $(ruby_EXTRA_DIST) all_EXTRA_DIST=$(java_EXTRA_DIST) $(javanano_EXTRA_DIST) $(python_EXTRA_DIST) $(ruby_EXTRA_DIST)
EXTRA_DIST = $(@DIST_LANG@_EXTRA_DIST) \ EXTRA_DIST = $(@DIST_LANG@_EXTRA_DIST) \
autogen.sh \ autogen.sh \

@ -12,7 +12,7 @@ AC_PREREQ(2.59)
# In the SVN trunk, the version should always be the next anticipated release # In the SVN trunk, the version should always be the next anticipated release
# version with the "-pre" suffix. (We used to use "-SNAPSHOT" but this pushed # version with the "-pre" suffix. (We used to use "-SNAPSHOT" but this pushed
# the size of one file name in the dist tarfile over the 99-char limit.) # the size of one file name in the dist tarfile over the 99-char limit.)
AC_INIT([Protocol Buffers],[3.0.0-pre],[protobuf@googlegroups.com],[protobuf]) AC_INIT([Protocol Buffers],[3.0.0-alpha-2],[protobuf@googlegroups.com],[protobuf])
AM_MAINTAINER_MODE([enable]) AM_MAINTAINER_MODE([enable])
@ -37,7 +37,7 @@ AS_IF([test "x${ac_cv_env_CXXFLAGS_set}" = "x"],
AC_CANONICAL_TARGET AC_CANONICAL_TARGET
AM_INIT_AUTOMAKE([subdir-objects]) AM_INIT_AUTOMAKE([1.9 tar-ustar subdir-objects])
AC_ARG_WITH([zlib], AC_ARG_WITH([zlib],
[AS_HELP_STRING([--with-zlib], [AS_HELP_STRING([--with-zlib],

@ -10,7 +10,7 @@
</parent> </parent>
<groupId>com.google.protobuf</groupId> <groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId> <artifactId>protobuf-java</artifactId>
<version>3.0.0-pre</version> <version>3.0.0-alpha-2</version>
<packaging>bundle</packaging> <packaging>bundle</packaging>
<name>Protocol Buffer Java API</name> <name>Protocol Buffer Java API</name>
<description> <description>
@ -152,7 +152,7 @@
<instructions> <instructions>
<Bundle-DocURL>https://developers.google.com/protocol-buffers/</Bundle-DocURL> <Bundle-DocURL>https://developers.google.com/protocol-buffers/</Bundle-DocURL>
<Bundle-SymbolicName>com.google.protobuf</Bundle-SymbolicName> <Bundle-SymbolicName>com.google.protobuf</Bundle-SymbolicName>
<Export-Package>com.google.protobuf;version=3.0.0-pre</Export-Package> <Export-Package>com.google.protobuf;version=3.0.0-alpha-2</Export-Package>
</instructions> </instructions>
</configuration> </configuration>
</plugin> </plugin>

@ -73,7 +73,7 @@ public abstract class GeneratedMessage extends AbstractMessage
/** For use by generated code only. */ /** For use by generated code only. */
protected UnknownFieldSet unknownFields; protected UnknownFieldSet unknownFields;
protected GeneratedMessage() { protected GeneratedMessage() {
unknownFields = UnknownFieldSet.getDefaultInstance(); unknownFields = UnknownFieldSet.getDefaultInstance();
} }
@ -549,12 +549,12 @@ public abstract class GeneratedMessage extends AbstractMessage
* Gets the map field with the given field number. This method should be * Gets the map field with the given field number. This method should be
* overridden in the generated message class if the message contains map * overridden in the generated message class if the message contains map
* fields. * fields.
* *
* Unlike other field types, reflection support for map fields can't be * Unlike other field types, reflection support for map fields can't be
* implemented based on generated public API because we need to access a * implemented based on generated public API because we need to access a
* map field as a list in reflection API but the generated API only allows * map field as a list in reflection API but the generated API only allows
* us to access it as a map. This method returns the underlying map field * us to access it as a map. This method returns the underlying map field
* directly and thus enables us to access the map field as a list. * directly and thus enables us to access the map field as a list.
*/ */
@SuppressWarnings({"unused", "rawtypes"}) @SuppressWarnings({"unused", "rawtypes"})
protected MapField internalGetMapField(int fieldNumber) { protected MapField internalGetMapField(int fieldNumber) {
@ -683,7 +683,7 @@ public abstract class GeneratedMessage extends AbstractMessage
public final <Type> Type getExtension( public final <Type> Type getExtension(
final ExtensionLite<MessageType, Type> extensionLite) { final ExtensionLite<MessageType, Type> extensionLite) {
Extension<MessageType, Type> extension = checkNotLite(extensionLite); Extension<MessageType, Type> extension = checkNotLite(extensionLite);
verifyExtensionContainingType(extension); verifyExtensionContainingType(extension);
FieldDescriptor descriptor = extension.getDescriptor(); FieldDescriptor descriptor = extension.getDescriptor();
final Object value = extensions.getField(descriptor); final Object value = extensions.getField(descriptor);
@ -1313,7 +1313,7 @@ public abstract class GeneratedMessage extends AbstractMessage
implements ExtensionDescriptorRetriever { implements ExtensionDescriptorRetriever {
private volatile FieldDescriptor descriptor; private volatile FieldDescriptor descriptor;
protected abstract FieldDescriptor loadDescriptor(); protected abstract FieldDescriptor loadDescriptor();
public FieldDescriptor getDescriptor() { public FieldDescriptor getDescriptor() {
if (descriptor == null) { if (descriptor == null) {
synchronized (this) { synchronized (this) {
@ -1651,17 +1651,17 @@ public abstract class GeneratedMessage extends AbstractMessage
} }
} }
} }
/** /**
* Gets the map field with the given field number. This method should be * Gets the map field with the given field number. This method should be
* overridden in the generated message class if the message contains map * overridden in the generated message class if the message contains map
* fields. * fields.
* *
* Unlike other field types, reflection support for map fields can't be * Unlike other field types, reflection support for map fields can't be
* implemented based on generated public API because we need to access a * implemented based on generated public API because we need to access a
* map field as a list in reflection API but the generated API only allows * map field as a list in reflection API but the generated API only allows
* us to access it as a map. This method returns the underlying map field * us to access it as a map. This method returns the underlying map field
* directly and thus enables us to access the map field as a list. * directly and thus enables us to access the map field as a list.
*/ */
@SuppressWarnings({"rawtypes", "unused"}) @SuppressWarnings({"rawtypes", "unused"})
protected MapField internalGetMapField(int fieldNumber) { protected MapField internalGetMapField(int fieldNumber) {
@ -1709,7 +1709,7 @@ public abstract class GeneratedMessage extends AbstractMessage
oneofs = new OneofAccessor[descriptor.getOneofs().size()]; oneofs = new OneofAccessor[descriptor.getOneofs().size()];
initialized = false; initialized = false;
} }
private boolean isMapFieldEnabled(FieldDescriptor field) { private boolean isMapFieldEnabled(FieldDescriptor field) {
boolean result = true; boolean result = true;
return result; return result;
@ -1934,11 +1934,11 @@ public abstract class GeneratedMessage extends AbstractMessage
protected final FieldDescriptor field; protected final FieldDescriptor field;
protected final boolean isOneofField; protected final boolean isOneofField;
protected final boolean hasHasMethod; protected final boolean hasHasMethod;
private int getOneofFieldNumber(final GeneratedMessage message) { private int getOneofFieldNumber(final GeneratedMessage message) {
return ((Internal.EnumLite) invokeOrDie(caseMethod, message)).getNumber(); return ((Internal.EnumLite) invokeOrDie(caseMethod, message)).getNumber();
} }
private int getOneofFieldNumber(final GeneratedMessage.Builder builder) { private int getOneofFieldNumber(final GeneratedMessage.Builder builder) {
return ((Internal.EnumLite) invokeOrDie(caseMethodBuilder, builder)).getNumber(); return ((Internal.EnumLite) invokeOrDie(caseMethodBuilder, builder)).getNumber();
} }
@ -2130,15 +2130,15 @@ public abstract class GeneratedMessage extends AbstractMessage
private final FieldDescriptor field; private final FieldDescriptor field;
private final Message mapEntryMessageDefaultInstance; private final Message mapEntryMessageDefaultInstance;
private MapField<?, ?> getMapField(GeneratedMessage message) { private MapField<?, ?> getMapField(GeneratedMessage message) {
return (MapField<?, ?>) message.internalGetMapField(field.getNumber()); return (MapField<?, ?>) message.internalGetMapField(field.getNumber());
} }
private MapField<?, ?> getMapField(GeneratedMessage.Builder builder) { private MapField<?, ?> getMapField(GeneratedMessage.Builder builder) {
return (MapField<?, ?>) builder.internalGetMapField(field.getNumber()); return (MapField<?, ?>) builder.internalGetMapField(field.getNumber());
} }
public Object get(GeneratedMessage message) { public Object get(GeneratedMessage message) {
List result = new ArrayList(); List result = new ArrayList();
for (int i = 0; i < getRepeatedCount(message); i++) { for (int i = 0; i < getRepeatedCount(message); i++) {
@ -2171,10 +2171,12 @@ public abstract class GeneratedMessage extends AbstractMessage
} }
public void setRepeated(Builder builder, int index, Object value) { public void setRepeated(Builder builder, int index, Object value) {
builder.onChanged();
getMapField(builder).getMutableList().set(index, (Message) value); getMapField(builder).getMutableList().set(index, (Message) value);
} }
public void addRepeated(Builder builder, Object value) { public void addRepeated(Builder builder, Object value) {
builder.onChanged();
getMapField(builder).getMutableList().add((Message) value); getMapField(builder).getMutableList().add((Message) value);
} }
@ -2197,6 +2199,7 @@ public abstract class GeneratedMessage extends AbstractMessage
} }
public void clear(Builder builder) { public void clear(Builder builder) {
builder.onChanged();
getMapField(builder).getMutableList().clear(); getMapField(builder).getMutableList().clear();
} }
@ -2208,7 +2211,7 @@ public abstract class GeneratedMessage extends AbstractMessage
throw new UnsupportedOperationException( throw new UnsupportedOperationException(
"Nested builder not supported for map fields."); "Nested builder not supported for map fields.");
} }
public com.google.protobuf.Message.Builder getRepeatedBuilder( public com.google.protobuf.Message.Builder getRepeatedBuilder(
Builder builder, int index) { Builder builder, int index) {
throw new UnsupportedOperationException( throw new UnsupportedOperationException(
@ -2226,7 +2229,7 @@ public abstract class GeneratedMessage extends AbstractMessage
final Class<? extends Builder> builderClass, final Class<? extends Builder> builderClass,
final String containingOneofCamelCaseName) { final String containingOneofCamelCaseName) {
super(descriptor, camelCaseName, messageClass, builderClass, containingOneofCamelCaseName); super(descriptor, camelCaseName, messageClass, builderClass, containingOneofCamelCaseName);
enumDescriptor = descriptor.getEnumType(); enumDescriptor = descriptor.getEnumType();
valueOfMethod = getMethodOrDie(type, "valueOf", valueOfMethod = getMethodOrDie(type, "valueOf",
@ -2244,12 +2247,12 @@ public abstract class GeneratedMessage extends AbstractMessage
getMethodOrDie(builderClass, "set" + camelCaseName + "Value", int.class); getMethodOrDie(builderClass, "set" + camelCaseName + "Value", int.class);
} }
} }
private EnumDescriptor enumDescriptor; private EnumDescriptor enumDescriptor;
private Method valueOfMethod; private Method valueOfMethod;
private Method getValueDescriptorMethod; private Method getValueDescriptorMethod;
private boolean supportUnknownEnumValue; private boolean supportUnknownEnumValue;
private Method getValueMethod; private Method getValueMethod;
private Method getValueMethodBuilder; private Method getValueMethodBuilder;
@ -2291,7 +2294,7 @@ public abstract class GeneratedMessage extends AbstractMessage
final Class<? extends GeneratedMessage> messageClass, final Class<? extends GeneratedMessage> messageClass,
final Class<? extends Builder> builderClass) { final Class<? extends Builder> builderClass) {
super(descriptor, camelCaseName, messageClass, builderClass); super(descriptor, camelCaseName, messageClass, builderClass);
enumDescriptor = descriptor.getEnumType(); enumDescriptor = descriptor.getEnumType();
valueOfMethod = getMethodOrDie(type, "valueOf", valueOfMethod = getMethodOrDie(type, "valueOf",
@ -2315,7 +2318,7 @@ public abstract class GeneratedMessage extends AbstractMessage
private final Method valueOfMethod; private final Method valueOfMethod;
private final Method getValueDescriptorMethod; private final Method getValueDescriptorMethod;
private boolean supportUnknownEnumValue; private boolean supportUnknownEnumValue;
private Method getRepeatedValueMethod; private Method getRepeatedValueMethod;
private Method getRepeatedValueMethodBuilder; private Method getRepeatedValueMethodBuilder;
@ -2395,7 +2398,8 @@ public abstract class GeneratedMessage extends AbstractMessage
final Class<? extends GeneratedMessage> messageClass, final Class<? extends GeneratedMessage> messageClass,
final Class<? extends Builder> builderClass, final Class<? extends Builder> builderClass,
final String containingOneofCamelCaseName) { final String containingOneofCamelCaseName) {
super(descriptor, camelCaseName, messageClass, builderClass, containingOneofCamelCaseName); super(descriptor, camelCaseName, messageClass, builderClass,
containingOneofCamelCaseName);
newBuilderMethod = getMethodOrDie(type, "newBuilder"); newBuilderMethod = getMethodOrDie(type, "newBuilder");
getBuilderMethodBuilder = getBuilderMethodBuilder =
@ -2492,7 +2496,7 @@ public abstract class GeneratedMessage extends AbstractMessage
protected Object writeReplace() throws ObjectStreamException { protected Object writeReplace() throws ObjectStreamException {
return new GeneratedMessageLite.SerializedForm(this); return new GeneratedMessageLite.SerializedForm(this);
} }
/** /**
* Checks that the {@link Extension} is non-Lite and returns it as a * Checks that the {@link Extension} is non-Lite and returns it as a
* {@link GeneratedExtension}. * {@link GeneratedExtension}.
@ -2503,7 +2507,7 @@ public abstract class GeneratedMessage extends AbstractMessage
if (extension.isLite()) { if (extension.isLite()) {
throw new IllegalArgumentException("Expected non-lite extension."); throw new IllegalArgumentException("Expected non-lite extension.");
} }
return (Extension<MessageType, T>) extension; return (Extension<MessageType, T>) extension;
} }
} }

@ -56,9 +56,10 @@ import protobuf_unittest.UnittestProto;
import protobuf_unittest.UnittestProto.ForeignEnum; import protobuf_unittest.UnittestProto.ForeignEnum;
import protobuf_unittest.UnittestProto.ForeignMessage; import protobuf_unittest.UnittestProto.ForeignMessage;
import protobuf_unittest.UnittestProto.ForeignMessageOrBuilder; import protobuf_unittest.UnittestProto.ForeignMessageOrBuilder;
import protobuf_unittest.UnittestProto.NestedTestAllTypes;
import protobuf_unittest.UnittestProto.TestAllExtensions; import protobuf_unittest.UnittestProto.TestAllExtensions;
import protobuf_unittest.UnittestProto.TestAllTypes;
import protobuf_unittest.UnittestProto.TestAllTypes.NestedMessage; import protobuf_unittest.UnittestProto.TestAllTypes.NestedMessage;
import protobuf_unittest.UnittestProto.TestAllTypes;
import protobuf_unittest.UnittestProto.TestAllTypesOrBuilder; import protobuf_unittest.UnittestProto.TestAllTypesOrBuilder;
import protobuf_unittest.UnittestProto.TestExtremeDefaultValues; import protobuf_unittest.UnittestProto.TestExtremeDefaultValues;
import protobuf_unittest.UnittestProto.TestOneof2; import protobuf_unittest.UnittestProto.TestOneof2;
@ -1510,6 +1511,17 @@ public class GeneratedMessageTest extends TestCase {
} }
} }
public void testOneofNestedBuilderOnChangePropagation() {
NestedTestAllTypes.Builder parentBuilder = NestedTestAllTypes.newBuilder();
TestAllTypes.Builder builder = parentBuilder.getPayloadBuilder();
builder.getOneofNestedMessageBuilder();
assertTrue(builder.hasOneofNestedMessage());
assertTrue(parentBuilder.hasPayload());
NestedTestAllTypes message = parentBuilder.build();
assertTrue(message.hasPayload());
assertTrue(message.getPayload().hasOneofNestedMessage());
}
public void testGetRepeatedFieldBuilder() { public void testGetRepeatedFieldBuilder() {
Descriptor descriptor = TestAllTypes.getDescriptor(); Descriptor descriptor = TestAllTypes.getDescriptor();

@ -57,22 +57,22 @@ public class MapTest extends TestCase {
builder.getMutableInt32ToStringField().put(1, "11"); builder.getMutableInt32ToStringField().put(1, "11");
builder.getMutableInt32ToStringField().put(2, "22"); builder.getMutableInt32ToStringField().put(2, "22");
builder.getMutableInt32ToStringField().put(3, "33"); builder.getMutableInt32ToStringField().put(3, "33");
builder.getMutableInt32ToBytesField().put(1, TestUtil.toBytes("11")); builder.getMutableInt32ToBytesField().put(1, TestUtil.toBytes("11"));
builder.getMutableInt32ToBytesField().put(2, TestUtil.toBytes("22")); builder.getMutableInt32ToBytesField().put(2, TestUtil.toBytes("22"));
builder.getMutableInt32ToBytesField().put(3, TestUtil.toBytes("33")); builder.getMutableInt32ToBytesField().put(3, TestUtil.toBytes("33"));
builder.getMutableInt32ToEnumField().put(1, TestMap.EnumValue.FOO); builder.getMutableInt32ToEnumField().put(1, TestMap.EnumValue.FOO);
builder.getMutableInt32ToEnumField().put(2, TestMap.EnumValue.BAR); builder.getMutableInt32ToEnumField().put(2, TestMap.EnumValue.BAR);
builder.getMutableInt32ToEnumField().put(3, TestMap.EnumValue.BAZ); builder.getMutableInt32ToEnumField().put(3, TestMap.EnumValue.BAZ);
builder.getMutableInt32ToMessageField().put( builder.getMutableInt32ToMessageField().put(
1, MessageValue.newBuilder().setValue(11).build()); 1, MessageValue.newBuilder().setValue(11).build());
builder.getMutableInt32ToMessageField().put( builder.getMutableInt32ToMessageField().put(
2, MessageValue.newBuilder().setValue(22).build()); 2, MessageValue.newBuilder().setValue(22).build());
builder.getMutableInt32ToMessageField().put( builder.getMutableInt32ToMessageField().put(
3, MessageValue.newBuilder().setValue(33).build()); 3, MessageValue.newBuilder().setValue(33).build());
builder.getMutableStringToInt32Field().put("1", 11); builder.getMutableStringToInt32Field().put("1", 11);
builder.getMutableStringToInt32Field().put("2", 22); builder.getMutableStringToInt32Field().put("2", 22);
builder.getMutableStringToInt32Field().put("3", 33); builder.getMutableStringToInt32Field().put("3", 33);
@ -88,22 +88,22 @@ public class MapTest extends TestCase {
assertEquals("11", message.getInt32ToStringField().get(1)); assertEquals("11", message.getInt32ToStringField().get(1));
assertEquals("22", message.getInt32ToStringField().get(2)); assertEquals("22", message.getInt32ToStringField().get(2));
assertEquals("33", message.getInt32ToStringField().get(3)); assertEquals("33", message.getInt32ToStringField().get(3));
assertEquals(3, message.getInt32ToBytesField().size()); assertEquals(3, message.getInt32ToBytesField().size());
assertEquals(TestUtil.toBytes("11"), message.getInt32ToBytesField().get(1)); assertEquals(TestUtil.toBytes("11"), message.getInt32ToBytesField().get(1));
assertEquals(TestUtil.toBytes("22"), message.getInt32ToBytesField().get(2)); assertEquals(TestUtil.toBytes("22"), message.getInt32ToBytesField().get(2));
assertEquals(TestUtil.toBytes("33"), message.getInt32ToBytesField().get(3)); assertEquals(TestUtil.toBytes("33"), message.getInt32ToBytesField().get(3));
assertEquals(3, message.getInt32ToEnumField().size()); assertEquals(3, message.getInt32ToEnumField().size());
assertEquals(TestMap.EnumValue.FOO, message.getInt32ToEnumField().get(1)); assertEquals(TestMap.EnumValue.FOO, message.getInt32ToEnumField().get(1));
assertEquals(TestMap.EnumValue.BAR, message.getInt32ToEnumField().get(2)); assertEquals(TestMap.EnumValue.BAR, message.getInt32ToEnumField().get(2));
assertEquals(TestMap.EnumValue.BAZ, message.getInt32ToEnumField().get(3)); assertEquals(TestMap.EnumValue.BAZ, message.getInt32ToEnumField().get(3));
assertEquals(3, message.getInt32ToMessageField().size()); assertEquals(3, message.getInt32ToMessageField().size());
assertEquals(11, message.getInt32ToMessageField().get(1).getValue()); assertEquals(11, message.getInt32ToMessageField().get(1).getValue());
assertEquals(22, message.getInt32ToMessageField().get(2).getValue()); assertEquals(22, message.getInt32ToMessageField().get(2).getValue());
assertEquals(33, message.getInt32ToMessageField().get(3).getValue()); assertEquals(33, message.getInt32ToMessageField().get(3).getValue());
assertEquals(3, message.getStringToInt32Field().size()); assertEquals(3, message.getStringToInt32Field().size());
assertEquals(11, message.getStringToInt32Field().get("1").intValue()); assertEquals(11, message.getStringToInt32Field().get("1").intValue());
assertEquals(22, message.getStringToInt32Field().get("2").intValue()); assertEquals(22, message.getStringToInt32Field().get("2").intValue());
@ -118,21 +118,21 @@ public class MapTest extends TestCase {
builder.getMutableInt32ToStringField().put(1, "111"); builder.getMutableInt32ToStringField().put(1, "111");
builder.getMutableInt32ToStringField().remove(2); builder.getMutableInt32ToStringField().remove(2);
builder.getMutableInt32ToStringField().put(4, "44"); builder.getMutableInt32ToStringField().put(4, "44");
builder.getMutableInt32ToBytesField().put(1, TestUtil.toBytes("111")); builder.getMutableInt32ToBytesField().put(1, TestUtil.toBytes("111"));
builder.getMutableInt32ToBytesField().remove(2); builder.getMutableInt32ToBytesField().remove(2);
builder.getMutableInt32ToBytesField().put(4, TestUtil.toBytes("44")); builder.getMutableInt32ToBytesField().put(4, TestUtil.toBytes("44"));
builder.getMutableInt32ToEnumField().put(1, TestMap.EnumValue.BAR); builder.getMutableInt32ToEnumField().put(1, TestMap.EnumValue.BAR);
builder.getMutableInt32ToEnumField().remove(2); builder.getMutableInt32ToEnumField().remove(2);
builder.getMutableInt32ToEnumField().put(4, TestMap.EnumValue.QUX); builder.getMutableInt32ToEnumField().put(4, TestMap.EnumValue.QUX);
builder.getMutableInt32ToMessageField().put( builder.getMutableInt32ToMessageField().put(
1, MessageValue.newBuilder().setValue(111).build()); 1, MessageValue.newBuilder().setValue(111).build());
builder.getMutableInt32ToMessageField().remove(2); builder.getMutableInt32ToMessageField().remove(2);
builder.getMutableInt32ToMessageField().put( builder.getMutableInt32ToMessageField().put(
4, MessageValue.newBuilder().setValue(44).build()); 4, MessageValue.newBuilder().setValue(44).build());
builder.getMutableStringToInt32Field().put("1", 111); builder.getMutableStringToInt32Field().put("1", 111);
builder.getMutableStringToInt32Field().remove("2"); builder.getMutableStringToInt32Field().remove("2");
builder.getMutableStringToInt32Field().put("4", 44); builder.getMutableStringToInt32Field().put("4", 44);
@ -148,22 +148,22 @@ public class MapTest extends TestCase {
assertEquals("111", message.getInt32ToStringField().get(1)); assertEquals("111", message.getInt32ToStringField().get(1));
assertEquals("33", message.getInt32ToStringField().get(3)); assertEquals("33", message.getInt32ToStringField().get(3));
assertEquals("44", message.getInt32ToStringField().get(4)); assertEquals("44", message.getInt32ToStringField().get(4));
assertEquals(3, message.getInt32ToBytesField().size()); assertEquals(3, message.getInt32ToBytesField().size());
assertEquals(TestUtil.toBytes("111"), message.getInt32ToBytesField().get(1)); assertEquals(TestUtil.toBytes("111"), message.getInt32ToBytesField().get(1));
assertEquals(TestUtil.toBytes("33"), message.getInt32ToBytesField().get(3)); assertEquals(TestUtil.toBytes("33"), message.getInt32ToBytesField().get(3));
assertEquals(TestUtil.toBytes("44"), message.getInt32ToBytesField().get(4)); assertEquals(TestUtil.toBytes("44"), message.getInt32ToBytesField().get(4));
assertEquals(3, message.getInt32ToEnumField().size()); assertEquals(3, message.getInt32ToEnumField().size());
assertEquals(TestMap.EnumValue.BAR, message.getInt32ToEnumField().get(1)); assertEquals(TestMap.EnumValue.BAR, message.getInt32ToEnumField().get(1));
assertEquals(TestMap.EnumValue.BAZ, message.getInt32ToEnumField().get(3)); assertEquals(TestMap.EnumValue.BAZ, message.getInt32ToEnumField().get(3));
assertEquals(TestMap.EnumValue.QUX, message.getInt32ToEnumField().get(4)); assertEquals(TestMap.EnumValue.QUX, message.getInt32ToEnumField().get(4));
assertEquals(3, message.getInt32ToMessageField().size()); assertEquals(3, message.getInt32ToMessageField().size());
assertEquals(111, message.getInt32ToMessageField().get(1).getValue()); assertEquals(111, message.getInt32ToMessageField().get(1).getValue());
assertEquals(33, message.getInt32ToMessageField().get(3).getValue()); assertEquals(33, message.getInt32ToMessageField().get(3).getValue());
assertEquals(44, message.getInt32ToMessageField().get(4).getValue()); assertEquals(44, message.getInt32ToMessageField().get(4).getValue());
assertEquals(3, message.getStringToInt32Field().size()); assertEquals(3, message.getStringToInt32Field().size());
assertEquals(111, message.getStringToInt32Field().get("1").intValue()); assertEquals(111, message.getStringToInt32Field().get("1").intValue());
assertEquals(33, message.getStringToInt32Field().get("3").intValue()); assertEquals(33, message.getStringToInt32Field().get("3").intValue());
@ -183,17 +183,17 @@ public class MapTest extends TestCase {
TestMap.Builder builder = TestMap.newBuilder(); TestMap.Builder builder = TestMap.newBuilder();
TestMap message = builder.build(); TestMap message = builder.build();
assertMapValuesCleared(message); assertMapValuesCleared(message);
builder = message.toBuilder(); builder = message.toBuilder();
setMapValues(builder); setMapValues(builder);
message = builder.build(); message = builder.build();
assertMapValuesSet(message); assertMapValuesSet(message);
builder = message.toBuilder(); builder = message.toBuilder();
updateMapValues(builder); updateMapValues(builder);
message = builder.build(); message = builder.build();
assertMapValuesUpdated(message); assertMapValuesUpdated(message);
builder = message.toBuilder(); builder = message.toBuilder();
builder.clear(); builder.clear();
message = builder.build(); message = builder.build();
@ -207,14 +207,14 @@ public class MapTest extends TestCase {
assertEquals(message.getSerializedSize(), message.toByteString().size()); assertEquals(message.getSerializedSize(), message.toByteString().size());
message = TestMap.PARSER.parseFrom(message.toByteString()); message = TestMap.PARSER.parseFrom(message.toByteString());
assertMapValuesSet(message); assertMapValuesSet(message);
builder = message.toBuilder(); builder = message.toBuilder();
updateMapValues(builder); updateMapValues(builder);
message = builder.build(); message = builder.build();
assertEquals(message.getSerializedSize(), message.toByteString().size()); assertEquals(message.getSerializedSize(), message.toByteString().size());
message = TestMap.PARSER.parseFrom(message.toByteString()); message = TestMap.PARSER.parseFrom(message.toByteString());
assertMapValuesUpdated(message); assertMapValuesUpdated(message);
builder = message.toBuilder(); builder = message.toBuilder();
builder.clear(); builder.clear();
message = builder.build(); message = builder.build();
@ -222,12 +222,12 @@ public class MapTest extends TestCase {
message = TestMap.PARSER.parseFrom(message.toByteString()); message = TestMap.PARSER.parseFrom(message.toByteString());
assertMapValuesCleared(message); assertMapValuesCleared(message);
} }
public void testMergeFrom() throws Exception { public void testMergeFrom() throws Exception {
TestMap.Builder builder = TestMap.newBuilder(); TestMap.Builder builder = TestMap.newBuilder();
setMapValues(builder); setMapValues(builder);
TestMap message = builder.build(); TestMap message = builder.build();
TestMap.Builder other = TestMap.newBuilder(); TestMap.Builder other = TestMap.newBuilder();
other.mergeFrom(message); other.mergeFrom(message);
assertMapValuesSet(other.build()); assertMapValuesSet(other.build());
@ -236,7 +236,7 @@ public class MapTest extends TestCase {
public void testEqualsAndHashCode() throws Exception { public void testEqualsAndHashCode() throws Exception {
// Test that generated equals() and hashCode() will disregard the order // Test that generated equals() and hashCode() will disregard the order
// of map entries when comparing/hashing map fields. // of map entries when comparing/hashing map fields.
// We can't control the order of elements in a HashMap. The best we can do // We can't control the order of elements in a HashMap. The best we can do
// here is to add elements in different order. // here is to add elements in different order.
TestMap.Builder b1 = TestMap.newBuilder(); TestMap.Builder b1 = TestMap.newBuilder();
@ -244,23 +244,23 @@ public class MapTest extends TestCase {
b1.getMutableInt32ToInt32Field().put(3, 4); b1.getMutableInt32ToInt32Field().put(3, 4);
b1.getMutableInt32ToInt32Field().put(5, 6); b1.getMutableInt32ToInt32Field().put(5, 6);
TestMap m1 = b1.build(); TestMap m1 = b1.build();
TestMap.Builder b2 = TestMap.newBuilder(); TestMap.Builder b2 = TestMap.newBuilder();
b2.getMutableInt32ToInt32Field().put(5, 6); b2.getMutableInt32ToInt32Field().put(5, 6);
b2.getMutableInt32ToInt32Field().put(1, 2); b2.getMutableInt32ToInt32Field().put(1, 2);
b2.getMutableInt32ToInt32Field().put(3, 4); b2.getMutableInt32ToInt32Field().put(3, 4);
TestMap m2 = b2.build(); TestMap m2 = b2.build();
assertEquals(m1, m2); assertEquals(m1, m2);
assertEquals(m1.hashCode(), m2.hashCode()); assertEquals(m1.hashCode(), m2.hashCode());
// Make sure we did compare map fields. // Make sure we did compare map fields.
b2.getMutableInt32ToInt32Field().put(1, 0); b2.getMutableInt32ToInt32Field().put(1, 0);
m2 = b2.build(); m2 = b2.build();
assertFalse(m1.equals(m2)); assertFalse(m1.equals(m2));
// Don't check m1.hashCode() != m2.hashCode() because it's not guaranteed // Don't check m1.hashCode() != m2.hashCode() because it's not guaranteed
// to be different. // to be different.
// Regression test for b/18549190: if a map is a subset of the other map, // Regression test for b/18549190: if a map is a subset of the other map,
// equals() should return false. // equals() should return false.
b2.getMutableInt32ToInt32Field().remove(1); b2.getMutableInt32ToInt32Field().remove(1);
@ -268,57 +268,96 @@ public class MapTest extends TestCase {
assertFalse(m1.equals(m2)); assertFalse(m1.equals(m2));
assertFalse(m2.equals(m1)); assertFalse(m2.equals(m1));
} }
public void testNestedBuilderOnChangeEventPropagation() { public void testNestedBuilderOnChangeEventPropagation() {
TestOnChangeEventPropagation.Builder parent = TestOnChangeEventPropagation.Builder parent =
TestOnChangeEventPropagation.newBuilder(); TestOnChangeEventPropagation.newBuilder();
parent.getOptionalMessageBuilder().getMutableInt32ToInt32Field().put(1, 2); parent.getOptionalMessageBuilder().getMutableInt32ToInt32Field().put(1, 2);
TestOnChangeEventPropagation message = parent.build(); TestOnChangeEventPropagation message = parent.build();
assertEquals(2, message.getOptionalMessage().getInt32ToInt32Field().get(1).intValue()); assertEquals(2, message.getOptionalMessage().getInt32ToInt32Field().get(1).intValue());
// Make a change using nested builder. // Make a change using nested builder.
parent.getOptionalMessageBuilder().getMutableInt32ToInt32Field().put(1, 3); parent.getOptionalMessageBuilder().getMutableInt32ToInt32Field().put(1, 3);
// Should be able to observe the change. // Should be able to observe the change.
message = parent.build(); message = parent.build();
assertEquals(3, message.getOptionalMessage().getInt32ToInt32Field().get(1).intValue()); assertEquals(3, message.getOptionalMessage().getInt32ToInt32Field().get(1).intValue());
// Make another change using mergeFrom() // Make another change using mergeFrom()
TestMap.Builder other = TestMap.newBuilder(); TestMap.Builder other = TestMap.newBuilder();
other.getMutableInt32ToInt32Field().put(1, 4); other.getMutableInt32ToInt32Field().put(1, 4);
parent.getOptionalMessageBuilder().mergeFrom(other.build()); parent.getOptionalMessageBuilder().mergeFrom(other.build());
// Should be able to observe the change. // Should be able to observe the change.
message = parent.build(); message = parent.build();
assertEquals(4, message.getOptionalMessage().getInt32ToInt32Field().get(1).intValue()); assertEquals(4, message.getOptionalMessage().getInt32ToInt32Field().get(1).intValue());
// Make yet another change by clearing the nested builder. // Make yet another change by clearing the nested builder.
parent.getOptionalMessageBuilder().clear(); parent.getOptionalMessageBuilder().clear();
// Should be able to observe the change. // Should be able to observe the change.
message = parent.build(); message = parent.build();
assertEquals(0, message.getOptionalMessage().getInt32ToInt32Field().size()); assertEquals(0, message.getOptionalMessage().getInt32ToInt32Field().size());
} }
public void testNestedBuilderOnChangeEventPropagationReflection() {
FieldDescriptor intMapField = f("int32_to_int32_field");
// Create an outer message builder with nested builder.
TestOnChangeEventPropagation.Builder parentBuilder =
TestOnChangeEventPropagation.newBuilder();
TestMap.Builder testMapBuilder = parentBuilder.getOptionalMessageBuilder();
// Create a map entry message.
TestMap.Builder entryBuilder = TestMap.newBuilder();
entryBuilder.getMutableInt32ToInt32Field().put(1, 1);
// Put the entry into the nested builder.
testMapBuilder.addRepeatedField(
intMapField, entryBuilder.getRepeatedField(intMapField, 0));
// Should be able to observe the change.
TestOnChangeEventPropagation message = parentBuilder.build();
assertEquals(1, message.getOptionalMessage().getInt32ToInt32Field().size());
// Change the entry value.
entryBuilder.getMutableInt32ToInt32Field().put(1, 4);
testMapBuilder = parentBuilder.getOptionalMessageBuilder();
testMapBuilder.setRepeatedField(
intMapField, 0, entryBuilder.getRepeatedField(intMapField, 0));
// Should be able to observe the change.
message = parentBuilder.build();
assertEquals(4,
message.getOptionalMessage().getInt32ToInt32Field().get(1).intValue());
// Clear the nested builder.
testMapBuilder = parentBuilder.getOptionalMessageBuilder();
testMapBuilder.clearField(intMapField);
// Should be able to observe the change.
message = parentBuilder.build();
assertEquals(0, message.getOptionalMessage().getInt32ToInt32Field().size());
}
// The following methods are used to test reflection API. // The following methods are used to test reflection API.
private static FieldDescriptor f(String name) { private static FieldDescriptor f(String name) {
return TestMap.getDescriptor().findFieldByName(name); return TestMap.getDescriptor().findFieldByName(name);
} }
private static Object getFieldValue(Message mapEntry, String name) { private static Object getFieldValue(Message mapEntry, String name) {
FieldDescriptor field = mapEntry.getDescriptorForType().findFieldByName(name); FieldDescriptor field = mapEntry.getDescriptorForType().findFieldByName(name);
return mapEntry.getField(field); return mapEntry.getField(field);
} }
private static Message.Builder setFieldValue( private static Message.Builder setFieldValue(
Message.Builder mapEntry, String name, Object value) { Message.Builder mapEntry, String name, Object value) {
FieldDescriptor field = mapEntry.getDescriptorForType().findFieldByName(name); FieldDescriptor field = mapEntry.getDescriptorForType().findFieldByName(name);
mapEntry.setField(field, value); mapEntry.setField(field, value);
return mapEntry; return mapEntry;
} }
private static void assertHasMapValues(Message message, String name, Map<?, ?> values) { private static void assertHasMapValues(Message message, String name, Map<?, ?> values) {
FieldDescriptor field = f(name); FieldDescriptor field = f(name);
for (Object entry : (List<?>) message.getField(field)) { for (Object entry : (List<?>) message.getField(field)) {
@ -337,7 +376,7 @@ public class MapTest extends TestCase {
assertEquals(value, values.get(key)); assertEquals(value, values.get(key));
} }
} }
private static <KeyType, ValueType> private static <KeyType, ValueType>
Message newMapEntry(Message.Builder builder, String name, KeyType key, ValueType value) { Message newMapEntry(Message.Builder builder, String name, KeyType key, ValueType value) {
FieldDescriptor field = builder.getDescriptorForType().findFieldByName(name); FieldDescriptor field = builder.getDescriptorForType().findFieldByName(name);
@ -348,7 +387,7 @@ public class MapTest extends TestCase {
entryBuilder.setField(valueField, value); entryBuilder.setField(valueField, value);
return entryBuilder.build(); return entryBuilder.build();
} }
private static void setMapValues(Message.Builder builder, String name, Map<?, ?> values) { private static void setMapValues(Message.Builder builder, String name, Map<?, ?> values) {
List<Message> entryList = new ArrayList<Message>(); List<Message> entryList = new ArrayList<Message>();
for (Map.Entry<?, ?> entry : values.entrySet()) { for (Map.Entry<?, ?> entry : values.entrySet()) {
@ -357,7 +396,7 @@ public class MapTest extends TestCase {
FieldDescriptor field = builder.getDescriptorForType().findFieldByName(name); FieldDescriptor field = builder.getDescriptorForType().findFieldByName(name);
builder.setField(field, entryList); builder.setField(field, entryList);
} }
private static <KeyType, ValueType> private static <KeyType, ValueType>
Map<KeyType, ValueType> mapForValues( Map<KeyType, ValueType> mapForValues(
KeyType key1, ValueType value1, KeyType key2, ValueType value2) { KeyType key1, ValueType value1, KeyType key2, ValueType value2) {
@ -385,14 +424,14 @@ public class MapTest extends TestCase {
mapForValues( mapForValues(
11, MessageValue.newBuilder().setValue(22).build(), 11, MessageValue.newBuilder().setValue(22).build(),
33, MessageValue.newBuilder().setValue(44).build())); 33, MessageValue.newBuilder().setValue(44).build()));
// Test clearField() // Test clearField()
builder.clearField(f("int32_to_int32_field")); builder.clearField(f("int32_to_int32_field"));
builder.clearField(f("int32_to_message_field")); builder.clearField(f("int32_to_message_field"));
message = builder.build(); message = builder.build();
assertEquals(0, message.getInt32ToInt32Field().size()); assertEquals(0, message.getInt32ToInt32Field().size());
assertEquals(0, message.getInt32ToMessageField().size()); assertEquals(0, message.getInt32ToMessageField().size());
// Test setField() // Test setField()
setMapValues(builder, "int32_to_int32_field", setMapValues(builder, "int32_to_int32_field",
mapForValues(11, 22, 33, 44)); mapForValues(11, 22, 33, 44));
@ -405,7 +444,7 @@ public class MapTest extends TestCase {
assertEquals(44, message.getInt32ToInt32Field().get(33).intValue()); assertEquals(44, message.getInt32ToInt32Field().get(33).intValue());
assertEquals(222, message.getInt32ToMessageField().get(111).getValue()); assertEquals(222, message.getInt32ToMessageField().get(111).getValue());
assertEquals(444, message.getInt32ToMessageField().get(333).getValue()); assertEquals(444, message.getInt32ToMessageField().get(333).getValue());
// Test addRepeatedField // Test addRepeatedField
builder.addRepeatedField(f("int32_to_int32_field"), builder.addRepeatedField(f("int32_to_int32_field"),
newMapEntry(builder, "int32_to_int32_field", 55, 66)); newMapEntry(builder, "int32_to_int32_field", 55, 66));
@ -425,7 +464,7 @@ public class MapTest extends TestCase {
message = builder.build(); message = builder.build();
assertEquals(55, message.getInt32ToInt32Field().get(55).intValue()); assertEquals(55, message.getInt32ToInt32Field().get(55).intValue());
assertEquals(555, message.getInt32ToMessageField().get(555).getValue()); assertEquals(555, message.getInt32ToMessageField().get(555).getValue());
// Test setRepeatedField // Test setRepeatedField
for (int i = 0; i < builder.getRepeatedFieldCount(f("int32_to_int32_field")); i++) { for (int i = 0; i < builder.getRepeatedFieldCount(f("int32_to_int32_field")); i++) {
Message mapEntry = (Message) builder.getRepeatedField(f("int32_to_int32_field"), i); Message mapEntry = (Message) builder.getRepeatedField(f("int32_to_int32_field"), i);
@ -442,35 +481,35 @@ public class MapTest extends TestCase {
assertEquals(33, message.getInt32ToInt32Field().get(44).intValue()); assertEquals(33, message.getInt32ToInt32Field().get(44).intValue());
assertEquals(55, message.getInt32ToInt32Field().get(55).intValue()); assertEquals(55, message.getInt32ToInt32Field().get(55).intValue());
} }
public void testTextFormat() throws Exception { public void testTextFormat() throws Exception {
TestMap.Builder builder = TestMap.newBuilder(); TestMap.Builder builder = TestMap.newBuilder();
setMapValues(builder); setMapValues(builder);
TestMap message = builder.build(); TestMap message = builder.build();
String textData = TextFormat.printToString(message); String textData = TextFormat.printToString(message);
builder = TestMap.newBuilder(); builder = TestMap.newBuilder();
TextFormat.merge(textData, builder); TextFormat.merge(textData, builder);
message = builder.build(); message = builder.build();
assertMapValuesSet(message); assertMapValuesSet(message);
} }
public void testDynamicMessage() throws Exception { public void testDynamicMessage() throws Exception {
TestMap.Builder builder = TestMap.newBuilder(); TestMap.Builder builder = TestMap.newBuilder();
setMapValues(builder); setMapValues(builder);
TestMap message = builder.build(); TestMap message = builder.build();
Message dynamicDefaultInstance = Message dynamicDefaultInstance =
DynamicMessage.getDefaultInstance(TestMap.getDescriptor()); DynamicMessage.getDefaultInstance(TestMap.getDescriptor());
Message dynamicMessage = dynamicDefaultInstance Message dynamicMessage = dynamicDefaultInstance
.newBuilderForType().mergeFrom(message.toByteString()).build(); .newBuilderForType().mergeFrom(message.toByteString()).build();
assertEquals(message, dynamicMessage); assertEquals(message, dynamicMessage);
assertEquals(message.hashCode(), dynamicMessage.hashCode()); assertEquals(message.hashCode(), dynamicMessage.hashCode());
} }
public void testReflectionEqualsAndHashCode() throws Exception { public void testReflectionEqualsAndHashCode() throws Exception {
// Test that generated equals() and hashCode() will disregard the order // Test that generated equals() and hashCode() will disregard the order
// of map entries when comparing/hashing map fields. // of map entries when comparing/hashing map fields.
@ -479,22 +518,22 @@ public class MapTest extends TestCase {
Message dynamicDefaultInstance = Message dynamicDefaultInstance =
DynamicMessage.getDefaultInstance(TestMap.getDescriptor()); DynamicMessage.getDefaultInstance(TestMap.getDescriptor());
FieldDescriptor field = f("int32_to_int32_field"); FieldDescriptor field = f("int32_to_int32_field");
Message.Builder b1 = dynamicDefaultInstance.newBuilderForType(); Message.Builder b1 = dynamicDefaultInstance.newBuilderForType();
b1.addRepeatedField(field, newMapEntry(b1, "int32_to_int32_field", 1, 2)); b1.addRepeatedField(field, newMapEntry(b1, "int32_to_int32_field", 1, 2));
b1.addRepeatedField(field, newMapEntry(b1, "int32_to_int32_field", 3, 4)); b1.addRepeatedField(field, newMapEntry(b1, "int32_to_int32_field", 3, 4));
b1.addRepeatedField(field, newMapEntry(b1, "int32_to_int32_field", 5, 6)); b1.addRepeatedField(field, newMapEntry(b1, "int32_to_int32_field", 5, 6));
Message m1 = b1.build(); Message m1 = b1.build();
Message.Builder b2 = dynamicDefaultInstance.newBuilderForType(); Message.Builder b2 = dynamicDefaultInstance.newBuilderForType();
b2.addRepeatedField(field, newMapEntry(b2, "int32_to_int32_field", 5, 6)); b2.addRepeatedField(field, newMapEntry(b2, "int32_to_int32_field", 5, 6));
b2.addRepeatedField(field, newMapEntry(b2, "int32_to_int32_field", 1, 2)); b2.addRepeatedField(field, newMapEntry(b2, "int32_to_int32_field", 1, 2));
b2.addRepeatedField(field, newMapEntry(b2, "int32_to_int32_field", 3, 4)); b2.addRepeatedField(field, newMapEntry(b2, "int32_to_int32_field", 3, 4));
Message m2 = b2.build(); Message m2 = b2.build();
assertEquals(m1, m2); assertEquals(m1, m2);
assertEquals(m1.hashCode(), m2.hashCode()); assertEquals(m1.hashCode(), m2.hashCode());
// Make sure we did compare map fields. // Make sure we did compare map fields.
b2.setRepeatedField(field, 0, newMapEntry(b1, "int32_to_int32_field", 0, 0)); b2.setRepeatedField(field, 0, newMapEntry(b1, "int32_to_int32_field", 0, 0));
m2 = b2.build(); m2 = b2.build();
@ -502,7 +541,7 @@ public class MapTest extends TestCase {
// Don't check m1.hashCode() != m2.hashCode() because it's not guaranteed // Don't check m1.hashCode() != m2.hashCode() because it's not guaranteed
// to be different. // to be different.
} }
public void testUnknownEnumValues() throws Exception { public void testUnknownEnumValues() throws Exception {
TestMap.Builder builder = TestMap.newBuilder(); TestMap.Builder builder = TestMap.newBuilder();
builder.getMutableInt32ToEnumFieldValue().put(0, 0); builder.getMutableInt32ToEnumFieldValue().put(0, 0);
@ -517,7 +556,7 @@ public class MapTest extends TestCase {
assertEquals(TestMap.EnumValue.UNRECOGNIZED, assertEquals(TestMap.EnumValue.UNRECOGNIZED,
message.getInt32ToEnumField().get(2)); message.getInt32ToEnumField().get(2));
assertEquals(1000, message.getInt32ToEnumFieldValue().get(2).intValue()); assertEquals(1000, message.getInt32ToEnumFieldValue().get(2).intValue());
// Unknown enum values should be preserved after: // Unknown enum values should be preserved after:
// 1. Serialization and parsing. // 1. Serialization and parsing.
// 2. toBuild(). // 2. toBuild().
@ -528,7 +567,7 @@ public class MapTest extends TestCase {
assertEquals(1000, builder.getInt32ToEnumFieldValue().get(2).intValue()); assertEquals(1000, builder.getInt32ToEnumFieldValue().get(2).intValue());
builder = TestMap.newBuilder().mergeFrom(message); builder = TestMap.newBuilder().mergeFrom(message);
assertEquals(1000, builder.getInt32ToEnumFieldValue().get(2).intValue()); assertEquals(1000, builder.getInt32ToEnumFieldValue().get(2).intValue());
// hashCode()/equals() should take unknown enum values into account. // hashCode()/equals() should take unknown enum values into account.
builder.getMutableInt32ToEnumFieldValue().put(2, 1001); builder.getMutableInt32ToEnumFieldValue().put(2, 1001);
TestMap message2 = builder.build(); TestMap message2 = builder.build();
@ -538,17 +577,17 @@ public class MapTest extends TestCase {
// should be the same. // should be the same.
assertTrue(message.getInt32ToEnumField().equals(message2.getInt32ToEnumField())); assertTrue(message.getInt32ToEnumField().equals(message2.getInt32ToEnumField()));
} }
public void testUnknownEnumValuesInReflectionApi() throws Exception { public void testUnknownEnumValuesInReflectionApi() throws Exception {
Descriptor descriptor = TestMap.getDescriptor(); Descriptor descriptor = TestMap.getDescriptor();
EnumDescriptor enumDescriptor = TestMap.EnumValue.getDescriptor(); EnumDescriptor enumDescriptor = TestMap.EnumValue.getDescriptor();
FieldDescriptor field = descriptor.findFieldByName("int32_to_enum_field"); FieldDescriptor field = descriptor.findFieldByName("int32_to_enum_field");
Map<Integer, Integer> data = new HashMap<Integer, Integer>(); Map<Integer, Integer> data = new HashMap<Integer, Integer>();
data.put(0, 0); data.put(0, 0);
data.put(1, 1); data.put(1, 1);
data.put(2, 1000); // unknown value. data.put(2, 1000); // unknown value.
TestMap.Builder builder = TestMap.newBuilder(); TestMap.Builder builder = TestMap.newBuilder();
for (Map.Entry<Integer, Integer> entry : data.entrySet()) { for (Map.Entry<Integer, Integer> entry : data.entrySet()) {
builder.getMutableInt32ToEnumFieldValue().put(entry.getKey(), entry.getValue()); builder.getMutableInt32ToEnumFieldValue().put(entry.getKey(), entry.getValue());

@ -68,18 +68,20 @@ running unit tests.
Nano version Nano version
============================ ============================
Nano is a special code generator and runtime library designed specially JavaNano is a special code generator and runtime library designed specially for
for Android, and is very resource-friendly in both the amount of code resource-restricted systems, like Android. It is very resource-friendly in both
and the runtime overhead. An overview of Nano features: the amount of code and the runtime overhead. Here is an overview of JavaNano
features compared with the official Java protobuf:
- No descriptors or message builders. - No descriptors or message builders.
- All messages are mutable; fields are public Java fields. - All messages are mutable; fields are public Java fields.
- For optional fields only, encapsulation behind setter/getter/hazzer/ - For optional fields only, encapsulation behind setter/getter/hazzer/
clearer functions is opt-in, which provide proper 'has' state support. clearer functions is opt-in, which provide proper 'has' state support.
- If not opted in, has state is not available. Serialization outputs - For proto2, if not opted in, has state (field presence) is not available.
all fields not equal to their defaults (see important implications Serialization outputs all fields not equal to their defaults
below). (see important implications below).
- Required fields are always serialized. The behavior is consistent with proto3 semantics.
- Required fields (proto2 only) are always serialized.
- Enum constants are integers; protection against invalid values only - Enum constants are integers; protection against invalid values only
when parsing from the wire. when parsing from the wire.
- Enum constants can be generated into container interfaces bearing - Enum constants can be generated into container interfaces bearing
@ -88,8 +90,8 @@ and the runtime overhead. An overview of Nano features:
- Similarly CodedOutputByteBufferNano can only write to byte[]. - Similarly CodedOutputByteBufferNano can only write to byte[].
- Repeated fields are in arrays, not ArrayList or Vector. Null array - Repeated fields are in arrays, not ArrayList or Vector. Null array
elements are allowed and silently ignored. elements are allowed and silently ignored.
- Full support of serializing/deserializing repeated packed fields. - Full support for serializing/deserializing repeated packed fields.
- Support of extensions. - Support extensions (in proto2).
- Unset messages/groups are null, not an immutable empty default - Unset messages/groups are null, not an immutable empty default
instance. instance.
- toByteArray(...) and mergeFrom(...) are now static functions of - toByteArray(...) and mergeFrom(...) are now static functions of
@ -200,7 +202,7 @@ optional_field_style={default,accessors,reftypes} (default: default)
In the default style, optional fields translate into public mutable In the default style, optional fields translate into public mutable
Java fields, and the serialization process is as discussed in the Java fields, and the serialization process is as discussed in the
"IMPORTANT" section above. "IMPORTANT" section above.
* accessors * * accessors *

@ -10,7 +10,7 @@
</parent> </parent>
<groupId>com.google.protobuf.nano</groupId> <groupId>com.google.protobuf.nano</groupId>
<artifactId>protobuf-javanano</artifactId> <artifactId>protobuf-javanano</artifactId>
<version>2.6.2-pre</version> <version>3.0.0-alpha-2</version>
<packaging>bundle</packaging> <packaging>bundle</packaging>
<name>Protocol Buffer JavaNano API</name> <name>Protocol Buffer JavaNano API</name>
<description> <description>
@ -156,7 +156,7 @@
<instructions> <instructions>
<Bundle-DocURL>https://developers.google.com/protocol-buffers/</Bundle-DocURL> <Bundle-DocURL>https://developers.google.com/protocol-buffers/</Bundle-DocURL>
<Bundle-SymbolicName>com.google.protobuf</Bundle-SymbolicName> <Bundle-SymbolicName>com.google.protobuf</Bundle-SymbolicName>
<Export-Package>com.google.protobuf;version=2.6.2-pre</Export-Package> <Export-Package>com.google.protobuf;version=3.0.0-alpha-2</Export-Package>
</instructions> </instructions>
</configuration> </configuration>
</plugin> </plugin>

@ -27,7 +27,7 @@ fi
set -ex set -ex
LANGUAGES="cpp java python" LANGUAGES="cpp java javanano python ruby"
BASENAME=`basename $1 .tar.gz` BASENAME=`basename $1 .tar.gz`
VERSION=${BASENAME:9} VERSION=${BASENAME:9}

@ -163,7 +163,7 @@ if __name__ == '__main__':
)) ))
setup(name = 'protobuf', setup(name = 'protobuf',
version = '3.0.0-pre', version = '3.0.0-alpha-2',
packages = [ 'google' ], packages = [ 'google' ],
namespace_packages = [ 'google' ], namespace_packages = [ 'google' ],
test_suite = 'setup.MakeTestSuite', test_suite = 'setup.MakeTestSuite',

@ -7,8 +7,51 @@ we recommend using protoc's Ruby generation support with .proto files. The
build process in this directory only installs the extension; you need to build process in this directory only installs the extension; you need to
install protoc as well to have Ruby code generation functionality. install protoc as well to have Ruby code generation functionality.
Installation Installation from Gem
------------ ---------------------
When we release a version of Protocol Buffers, we will upload a Gem to
[RubyGems](https://www.rubygems.org/). To use this pre-packaged gem, simply
install it as you would any other gem:
$ gem install [--prerelease] google-protobuf
The `--pre` flag is necessary if we have not yet made a non-alpha/beta release
of the Ruby extension; it allows `gem` to consider these "pre-release"
alpha/beta versions.
Once the gem is installed, you may or may not need `protoc`. If you write your
message type descriptions directly in the Ruby DSL, you do not need it.
However, if you wish to generate the Ruby DSL from a `.proto` file, you will
also want to install Protocol Buffers itself, as described in this repository's
main `README` file. The version of `protoc` included in the latest release
supports the `--ruby_out` option to generate Ruby code.
A simple example of using the Ruby extension follows. More extensive
documentation may be found in the RubyDoc comments (`call-seq` tags) in the
source, and we plan to release separate, more detailed, documentation at a
later date.
require 'google/protobuf'
# generated from my_proto_types.proto with protoc:
# $ protoc --ruby_out=. my_proto_types.proto
require 'my_proto_types'
mymessage = MyTestMessage.new(:field1 => 42, :field2 => ["a", "b", "c"])
mymessage.field1 = 43
mymessage.field2.push("d")
mymessage.field3 = SubMessage.new(:foo => 100)
encoded_data = MyTestMessage.encode(mymessage)
decoded = MyTestMessage.decode(encoded_data)
assert decoded == mymessage
puts "JSON:"
puts MyTestMessage.encode_json(mymessage)
Installation from Source (Building Gem)
---------------------------------------
To build this Ruby extension, you will need: To build this Ruby extension, you will need:
@ -16,7 +59,6 @@ To build this Ruby extension, you will need:
* Bundler * Bundler
* Ruby development headers * Ruby development headers
* a C compiler * a C compiler
* the upb submodule
First, install the required Ruby gems: First, install the required Ruby gems:

@ -7,7 +7,7 @@ end
Gem::Specification.new do |s| Gem::Specification.new do |s|
s.name = "google-protobuf" s.name = "google-protobuf"
s.version = "3.0.0.alpha.2" s.version = "3.0.0.alpha.2.0"
s.licenses = ["BSD"] s.licenses = ["BSD"]
s.summary = "Protocol Buffers" s.summary = "Protocol Buffers"
s.description = "Protocol Buffers are Google's data interchange format." s.description = "Protocol Buffers are Google's data interchange format."

@ -64,6 +64,10 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
// Disable the whole test when we use tcmalloc for "draconian" heap checks, in
// which case tcmalloc will print warnings that fail the plugin tests.
#if !GOOGLE_PROTOBUF_HEAP_CHECK_DRACONIAN
namespace google { namespace google {
namespace protobuf { namespace protobuf {
namespace compiler { namespace compiler {
@ -1663,3 +1667,5 @@ TEST_F(EncodeDecodeTest, ProtoParseError) {
} // namespace compiler } // namespace compiler
} // namespace protobuf } // namespace protobuf
} // namespace google } // namespace google
#endif // !GOOGLE_PROTOBUF_HEAP_CHECK_DRACONIAN

@ -718,6 +718,7 @@ GenerateBuilderMembers(io::Printer* printer) const {
" $oneof_name$_ = null;\n" " $oneof_name$_ = null;\n"
" }\n" " }\n"
" $set_oneof_case_message$;\n" " $set_oneof_case_message$;\n"
" $on_changed$;\n"
" return $name$Builder_;\n" " return $name$Builder_;\n"
"}\n"); "}\n");
} }

@ -247,8 +247,14 @@ namespace {
UnknownFieldSet* empty_unknown_field_set_ = NULL; UnknownFieldSet* empty_unknown_field_set_ = NULL;
GOOGLE_PROTOBUF_DECLARE_ONCE(empty_unknown_field_set_once_); GOOGLE_PROTOBUF_DECLARE_ONCE(empty_unknown_field_set_once_);
void DeleteEmptyUnknownFieldSet() {
delete empty_unknown_field_set_;
empty_unknown_field_set_ = NULL;
}
void InitEmptyUnknownFieldSet() { void InitEmptyUnknownFieldSet() {
empty_unknown_field_set_ = new UnknownFieldSet; empty_unknown_field_set_ = new UnknownFieldSet;
internal::OnShutdown(&DeleteEmptyUnknownFieldSet);
} }
const UnknownFieldSet& GetEmptyUnknownFieldSet() { const UnknownFieldSet& GetEmptyUnknownFieldSet() {

@ -440,6 +440,30 @@ const internal::RepeatedFieldAccessor* Reflection::RepeatedFieldAccessor(
return NULL; return NULL;
} }
namespace internal {
namespace {
void ShutdownRepeatedFieldAccessor() {
Singleton<internal::RepeatedFieldPrimitiveAccessor<int32> >::ShutDown();
Singleton<internal::RepeatedFieldPrimitiveAccessor<uint32> >::ShutDown();
Singleton<internal::RepeatedFieldPrimitiveAccessor<int64> >::ShutDown();
Singleton<internal::RepeatedFieldPrimitiveAccessor<uint64> >::ShutDown();
Singleton<internal::RepeatedFieldPrimitiveAccessor<float> >::ShutDown();
Singleton<internal::RepeatedFieldPrimitiveAccessor<double> >::ShutDown();
Singleton<internal::RepeatedFieldPrimitiveAccessor<bool> >::ShutDown();
Singleton<internal::RepeatedPtrFieldStringAccessor>::ShutDown();
Singleton<internal::RepeatedPtrFieldMessageAccessor>::ShutDown();
Singleton<internal::MapFieldAccessor>::ShutDown();
};
struct ShutdownRepeatedFieldRegister {
ShutdownRepeatedFieldRegister() {
OnShutdown(&ShutdownRepeatedFieldAccessor);
}
} shutdown_;
} // namesapce
} // namespace internal
namespace internal { namespace internal {
// Macro defined in repeated_field.h. We can only define the Message-specific // Macro defined in repeated_field.h. We can only define the Message-specific
// GenericTypeHandler specializations here because we depend on Message, which // GenericTypeHandler specializations here because we depend on Message, which

@ -44,6 +44,10 @@ class Singleton {
GoogleOnceInit(&once_, &Singleton<T>::Init); GoogleOnceInit(&once_, &Singleton<T>::Init);
return instance_; return instance_;
} }
static void ShutDown() {
delete instance_;
instance_ = NULL;
}
private: private:
static void Init() { static void Init() {
instance_ = new T(); instance_ = new T();
@ -56,7 +60,7 @@ template<typename T>
ProtobufOnceType Singleton<T>::once_; ProtobufOnceType Singleton<T>::once_;
template<typename T> template<typename T>
T* Singleton<T>::instance_; T* Singleton<T>::instance_ = NULL;
} // namespace internal } // namespace internal
} // namespace protobuf } // namespace protobuf
} // namespace google } // namespace google

@ -6,7 +6,9 @@ md include\google\protobuf\io
md include\google\protobuf\compiler md include\google\protobuf\compiler
md include\google\protobuf\compiler\cpp md include\google\protobuf\compiler\cpp
md include\google\protobuf\compiler\java md include\google\protobuf\compiler\java
md include\google\protobuf\compiler\javanano
md include\google\protobuf\compiler\python md include\google\protobuf\compiler\python
md include\google\protobuf\compiler\ruby
copy ..\src\google\protobuf\arena.h include\google\protobuf\arena.h copy ..\src\google\protobuf\arena.h include\google\protobuf\arena.h
copy ..\src\google\protobuf\arenastring.h include\google\protobuf\arenastring.h copy ..\src\google\protobuf\arenastring.h include\google\protobuf\arenastring.h
copy ..\src\google\protobuf\compiler\code_generator.h include\google\protobuf\compiler\code_generator.h copy ..\src\google\protobuf\compiler\code_generator.h include\google\protobuf\compiler\code_generator.h
@ -14,10 +16,12 @@ copy ..\src\google\protobuf\compiler\command_line_interface.h include\google\pro
copy ..\src\google\protobuf\compiler\cpp\cpp_generator.h include\google\protobuf\compiler\cpp\cpp_generator.h copy ..\src\google\protobuf\compiler\cpp\cpp_generator.h include\google\protobuf\compiler\cpp\cpp_generator.h
copy ..\src\google\protobuf\compiler\importer.h include\google\protobuf\compiler\importer.h copy ..\src\google\protobuf\compiler\importer.h include\google\protobuf\compiler\importer.h
copy ..\src\google\protobuf\compiler\java\java_generator.h include\google\protobuf\compiler\java\java_generator.h copy ..\src\google\protobuf\compiler\java\java_generator.h include\google\protobuf\compiler\java\java_generator.h
copy ..\src\google\protobuf\compiler\javanano\javanano_generator.h include\google\protobuf\compiler\javanano\javanano_generator.h
copy ..\src\google\protobuf\compiler\parser.h include\google\protobuf\compiler\parser.h copy ..\src\google\protobuf\compiler\parser.h include\google\protobuf\compiler\parser.h
copy ..\src\google\protobuf\compiler\plugin.h include\google\protobuf\compiler\plugin.h copy ..\src\google\protobuf\compiler\plugin.h include\google\protobuf\compiler\plugin.h
copy ..\src\google\protobuf\compiler\plugin.pb.h include\google\protobuf\compiler\plugin.pb.h copy ..\src\google\protobuf\compiler\plugin.pb.h include\google\protobuf\compiler\plugin.pb.h
copy ..\src\google\protobuf\compiler\python\python_generator.h include\google\protobuf\compiler\python\python_generator.h copy ..\src\google\protobuf\compiler\python\python_generator.h include\google\protobuf\compiler\python\python_generator.h
copy ..\src\google\protobuf\compiler\ruby\ruby_generator.h include\google\protobuf\compiler\ruby\ruby_generator.h
copy ..\src\google\protobuf\descriptor_database.h include\google\protobuf\descriptor_database.h copy ..\src\google\protobuf\descriptor_database.h include\google\protobuf\descriptor_database.h
copy ..\src\google\protobuf\descriptor.h include\google\protobuf\descriptor.h copy ..\src\google\protobuf\descriptor.h include\google\protobuf\descriptor.h
copy ..\src\google\protobuf\descriptor.pb.h include\google\protobuf\descriptor.pb.h copy ..\src\google\protobuf\descriptor.pb.h include\google\protobuf\descriptor.pb.h

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save