aboutsummaryrefslogtreecommitdiff
path: root/java
diff options
context:
space:
mode:
Diffstat (limited to 'java')
-rw-r--r--java/pom.xml62
-rw-r--r--java/src/main/java/com/google/protobuf/AbstractMessage.java6
-rw-r--r--java/src/main/java/com/google/protobuf/BoundedByteString.java2
-rw-r--r--java/src/main/java/com/google/protobuf/ByteString.java7
-rw-r--r--java/src/main/java/com/google/protobuf/Descriptors.java2
-rw-r--r--java/src/main/java/com/google/protobuf/DynamicMessage.java11
-rw-r--r--java/src/main/java/com/google/protobuf/FieldSet.java56
-rw-r--r--java/src/main/java/com/google/protobuf/GeneratedMessage.java256
-rw-r--r--java/src/main/java/com/google/protobuf/GeneratedMessageLite.java145
-rw-r--r--java/src/main/java/com/google/protobuf/LazyFieldLite.java249
-rw-r--r--java/src/main/java/com/google/protobuf/LiteralByteString.java6
-rw-r--r--java/src/main/java/com/google/protobuf/Message.java4
-rw-r--r--java/src/main/java/com/google/protobuf/MessageReflection.java60
-rw-r--r--java/src/main/java/com/google/protobuf/Parser.java4
-rw-r--r--java/src/main/java/com/google/protobuf/RopeByteString.java8
-rw-r--r--java/src/main/java/com/google/protobuf/TextFormat.java119
-rw-r--r--java/src/main/java/com/google/protobuf/WireFormat.java82
-rw-r--r--java/src/test/java/com/google/protobuf/GeneratedMessageTest.java12
-rw-r--r--java/src/test/java/com/google/protobuf/LazyFieldLiteTest.java139
-rw-r--r--java/src/test/java/com/google/protobuf/LazyMessageLiteTest.java19
-rw-r--r--java/src/test/java/com/google/protobuf/LiteralByteStringTest.java24
-rw-r--r--java/src/test/java/com/google/protobuf/MapForProto2Test.java14
-rw-r--r--java/src/test/java/com/google/protobuf/MapTest.java170
-rw-r--r--java/src/test/java/com/google/protobuf/RopeByteStringTest.java28
-rw-r--r--java/src/test/java/com/google/protobuf/lazy_fields_lite.proto9
-rw-r--r--java/src/test/java/com/google/protobuf/map_for_proto2_lite_test.proto7
-rw-r--r--java/src/test/java/com/google/protobuf/map_for_proto2_test.proto7
-rw-r--r--java/src/test/java/com/google/protobuf/map_test.proto1
-rw-r--r--java/src/test/java/com/google/protobuf/test_bad_identifiers.proto1
29 files changed, 1212 insertions, 298 deletions
diff --git a/java/pom.xml b/java/pom.xml
index bbadc656..3eb7a703 100644
--- a/java/pom.xml
+++ b/java/pom.xml
@@ -10,7 +10,7 @@
</parent>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
- <version>3.0.0-pre</version>
+ <version>3.0.0-alpha-3-pre</version>
<packaging>bundle</packaging>
<name>Protocol Buffer Java API</name>
<description>
@@ -152,7 +152,7 @@
<instructions>
<Bundle-DocURL>https://developers.google.com/protocol-buffers/</Bundle-DocURL>
<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-3-pre</Export-Package>
</instructions>
</configuration>
</plugin>
@@ -160,6 +160,64 @@
</build>
<profiles>
<profile>
+ <id>release</id>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-source-plugin</artifactId>
+ <version>2.2.1</version>
+ <executions>
+ <execution>
+ <id>attach-sources</id>
+ <goals>
+ <goal>jar-no-fork</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-javadoc-plugin</artifactId>
+ <version>2.9.1</version>
+ <executions>
+ <execution>
+ <id>attach-javadocs</id>
+ <goals>
+ <goal>jar</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-gpg-plugin</artifactId>
+ <version>1.5</version>
+ <executions>
+ <execution>
+ <id>sign-artifacts</id>
+ <phase>verify</phase>
+ <goals>
+ <goal>sign</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ <plugin>
+ <groupId>org.sonatype.plugins</groupId>
+ <artifactId>nexus-staging-maven-plugin</artifactId>
+ <version>1.6.3</version>
+ <extensions>true</extensions>
+ <configuration>
+ <serverId>sonatype-nexus-staging</serverId>
+ <nexusUrl>https://oss.sonatype.org/</nexusUrl>
+ <autoReleaseAfterClose>false</autoReleaseAfterClose>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+ </profile>
+ <profile>
<id>lite</id>
<build>
<plugins>
diff --git a/java/src/main/java/com/google/protobuf/AbstractMessage.java b/java/src/main/java/com/google/protobuf/AbstractMessage.java
index 6de4cae3..cc89173a 100644
--- a/java/src/main/java/com/google/protobuf/AbstractMessage.java
+++ b/java/src/main/java/com/google/protobuf/AbstractMessage.java
@@ -83,10 +83,10 @@ public abstract class AbstractMessage extends AbstractMessageLite
}
public void writeTo(final CodedOutputStream output) throws IOException {
- MessageReflection.writeMessageTo(this, output, false);
+ MessageReflection.writeMessageTo(this, getAllFields(), output, false);
}
- private int memoizedSize = -1;
+ protected int memoizedSize = -1;
public int getSerializedSize() {
int size = memoizedSize;
@@ -94,7 +94,7 @@ public abstract class AbstractMessage extends AbstractMessageLite
return size;
}
- memoizedSize = MessageReflection.getSerializedSize(this);
+ memoizedSize = MessageReflection.getSerializedSize(this, getAllFields());
return memoizedSize;
}
diff --git a/java/src/main/java/com/google/protobuf/BoundedByteString.java b/java/src/main/java/com/google/protobuf/BoundedByteString.java
index 8cb6f463..b4c3fb1b 100644
--- a/java/src/main/java/com/google/protobuf/BoundedByteString.java
+++ b/java/src/main/java/com/google/protobuf/BoundedByteString.java
@@ -30,8 +30,8 @@
package com.google.protobuf;
-import java.io.InvalidObjectException;
import java.io.IOException;
+import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.util.NoSuchElementException;
diff --git a/java/src/main/java/com/google/protobuf/ByteString.java b/java/src/main/java/com/google/protobuf/ByteString.java
index cff1ee51..9b0a524b 100644
--- a/java/src/main/java/com/google/protobuf/ByteString.java
+++ b/java/src/main/java/com/google/protobuf/ByteString.java
@@ -76,6 +76,9 @@ public abstract class ByteString implements Iterable<Byte>, Serializable {
static final int MIN_READ_FROM_CHUNK_SIZE = 0x100; // 256b
static final int MAX_READ_FROM_CHUNK_SIZE = 0x2000; // 8k
+ // Defined by java.nio.charset.Charset
+ protected static final String UTF_8 = "UTF-8";
+
/**
* Empty {@code ByteString}.
*/
@@ -267,7 +270,7 @@ public abstract class ByteString implements Iterable<Byte>, Serializable {
*/
public static ByteString copyFromUtf8(String text) {
try {
- return new LiteralByteString(text.getBytes("UTF-8"));
+ return new LiteralByteString(text.getBytes(UTF_8));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("UTF-8 not supported?", e);
}
@@ -622,7 +625,7 @@ public abstract class ByteString implements Iterable<Byte>, Serializable {
*/
public String toStringUtf8() {
try {
- return toString("UTF-8");
+ return toString(UTF_8);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("UTF-8 not supported?", e);
}
diff --git a/java/src/main/java/com/google/protobuf/Descriptors.java b/java/src/main/java/com/google/protobuf/Descriptors.java
index d65e8b49..806f46c1 100644
--- a/java/src/main/java/com/google/protobuf/Descriptors.java
+++ b/java/src/main/java/com/google/protobuf/Descriptors.java
@@ -897,7 +897,7 @@ public final class Descriptors {
return (type == Type.STRING) && (getFile().getOptions().getJavaStringCheckUtf8());
}
- boolean isMapField() {
+ public boolean isMapField() {
return getType() == Type.MESSAGE && isRepeated()
&& getMessageType().getOptions().getMapEntry();
}
diff --git a/java/src/main/java/com/google/protobuf/DynamicMessage.java b/java/src/main/java/com/google/protobuf/DynamicMessage.java
index 9c5e6c61..3ea1b688 100644
--- a/java/src/main/java/com/google/protobuf/DynamicMessage.java
+++ b/java/src/main/java/com/google/protobuf/DynamicMessage.java
@@ -35,8 +35,8 @@ import com.google.protobuf.Descriptors.EnumValueDescriptor;
import com.google.protobuf.Descriptors.FieldDescriptor;
import com.google.protobuf.Descriptors.OneofDescriptor;
-import java.io.InputStream;
import java.io.IOException;
+import java.io.InputStream;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@@ -600,9 +600,12 @@ public final class DynamicMessage extends AbstractMessage {
}
// TODO(xiaofeng): Re-enable this check after Orgstore is fixed to not
// set incorrect EnumValueDescriptors.
- // if (field.getEnumType() != ((EnumValueDescriptor) value).getType()) {
- // throw new IllegalArgumentException(
- // "EnumValueDescriptor doesn't match Enum Field.");
+ // EnumDescriptor fieldType = field.getEnumType();
+ // EnumDescriptor fieldValueType = ((EnumValueDescriptor) value).getType();
+ // if (fieldType != fieldValueType) {
+ // throw new IllegalArgumentException(String.format(
+ // "EnumDescriptor %s of field doesn't match EnumDescriptor %s of field value",
+ // fieldType.getFullName(), fieldValueType.getFullName()));
// }
}
diff --git a/java/src/main/java/com/google/protobuf/FieldSet.java b/java/src/main/java/com/google/protobuf/FieldSet.java
index ff9b5bc0..47924b65 100644
--- a/java/src/main/java/com/google/protobuf/FieldSet.java
+++ b/java/src/main/java/com/google/protobuf/FieldSet.java
@@ -553,42 +553,13 @@ final class FieldSet<FieldDescriptorType extends
CodedInputStream input,
final WireFormat.FieldType type,
boolean checkUtf8) throws IOException {
- switch (type) {
- case DOUBLE : return input.readDouble ();
- case FLOAT : return input.readFloat ();
- case INT64 : return input.readInt64 ();
- case UINT64 : return input.readUInt64 ();
- case INT32 : return input.readInt32 ();
- case FIXED64 : return input.readFixed64 ();
- case FIXED32 : return input.readFixed32 ();
- case BOOL : return input.readBool ();
- case STRING : if (checkUtf8) {
- return input.readStringRequireUtf8();
- } else {
- return input.readString();
- }
- case BYTES : return input.readBytes ();
- case UINT32 : return input.readUInt32 ();
- case SFIXED32: return input.readSFixed32();
- case SFIXED64: return input.readSFixed64();
- case SINT32 : return input.readSInt32 ();
- case SINT64 : return input.readSInt64 ();
-
- case GROUP:
- throw new IllegalArgumentException(
- "readPrimitiveField() cannot handle nested groups.");
- case MESSAGE:
- throw new IllegalArgumentException(
- "readPrimitiveField() cannot handle embedded messages.");
- case ENUM:
- // We don't handle enums because we don't know what to do if the
- // value is not recognized.
- throw new IllegalArgumentException(
- "readPrimitiveField() cannot handle enums.");
+ if (checkUtf8) {
+ return WireFormat.readPrimitiveField(input, type,
+ WireFormat.Utf8Validation.STRICT);
+ } else {
+ return WireFormat.readPrimitiveField(input, type,
+ WireFormat.Utf8Validation.LOOSE);
}
-
- throw new RuntimeException(
- "There is no way to get here, but the compiler thinks otherwise.");
}
@@ -685,9 +656,15 @@ final class FieldSet<FieldDescriptorType extends
case FIXED64 : output.writeFixed64NoTag ((Long ) value); break;
case FIXED32 : output.writeFixed32NoTag ((Integer ) value); break;
case BOOL : output.writeBoolNoTag ((Boolean ) value); break;
- case STRING : output.writeStringNoTag ((String ) value); break;
case GROUP : output.writeGroupNoTag ((MessageLite) value); break;
case MESSAGE : output.writeMessageNoTag ((MessageLite) value); break;
+ case STRING:
+ if (value instanceof ByteString) {
+ output.writeBytesNoTag((ByteString) value);
+ } else {
+ output.writeStringNoTag((String) value);
+ }
+ break;
case BYTES:
if (value instanceof ByteString) {
output.writeBytesNoTag((ByteString) value);
@@ -843,7 +820,6 @@ final class FieldSet<FieldDescriptorType extends
case FIXED64 : return CodedOutputStream.computeFixed64SizeNoTag ((Long )value);
case FIXED32 : return CodedOutputStream.computeFixed32SizeNoTag ((Integer )value);
case BOOL : return CodedOutputStream.computeBoolSizeNoTag ((Boolean )value);
- case STRING : return CodedOutputStream.computeStringSizeNoTag ((String )value);
case GROUP : return CodedOutputStream.computeGroupSizeNoTag ((MessageLite)value);
case BYTES :
if (value instanceof ByteString) {
@@ -851,6 +827,12 @@ final class FieldSet<FieldDescriptorType extends
} else {
return CodedOutputStream.computeByteArraySizeNoTag((byte[]) value);
}
+ case STRING :
+ if (value instanceof ByteString) {
+ return CodedOutputStream.computeBytesSizeNoTag((ByteString) value);
+ } else {
+ return CodedOutputStream.computeStringSizeNoTag((String) value);
+ }
case UINT32 : return CodedOutputStream.computeUInt32SizeNoTag ((Integer )value);
case SFIXED32: return CodedOutputStream.computeSFixed32SizeNoTag((Integer )value);
case SFIXED64: return CodedOutputStream.computeSFixed64SizeNoTag((Long )value);
diff --git a/java/src/main/java/com/google/protobuf/GeneratedMessage.java b/java/src/main/java/com/google/protobuf/GeneratedMessage.java
index 156d1633..9457d999 100644
--- a/java/src/main/java/com/google/protobuf/GeneratedMessage.java
+++ b/java/src/main/java/com/google/protobuf/GeneratedMessage.java
@@ -73,7 +73,7 @@ public abstract class GeneratedMessage extends AbstractMessage
/** For use by generated code only. */
protected UnknownFieldSet unknownFields;
-
+
protected GeneratedMessage() {
unknownFields = UnknownFieldSet.getDefaultInstance();
}
@@ -109,8 +109,15 @@ public abstract class GeneratedMessage extends AbstractMessage
return internalGetFieldAccessorTable().descriptor;
}
- /** Internal helper which returns a mutable map. */
- private Map<FieldDescriptor, Object> getAllFieldsMutable() {
+ /**
+ * Internal helper to return a modifiable map containing all the fields.
+ * The returned Map is modifialbe so that the caller can add additional
+ * extension fields to implement {@link #getAllFields()}.
+ *
+ * @param getBytesForString whether to generate ByteString for string fields
+ */
+ private Map<FieldDescriptor, Object> getAllFieldsMutable(
+ boolean getBytesForString) {
final TreeMap<FieldDescriptor, Object> result =
new TreeMap<FieldDescriptor, Object>();
final Descriptor descriptor = internalGetFieldAccessorTable().descriptor;
@@ -122,7 +129,12 @@ public abstract class GeneratedMessage extends AbstractMessage
}
} else {
if (hasField(field)) {
- result.put(field, getField(field));
+ if (getBytesForString
+ && field.getJavaType() == FieldDescriptor.JavaType.STRING) {
+ result.put(field, getFieldRaw(field));
+ } else {
+ result.put(field, getField(field));
+ }
}
}
}
@@ -161,7 +173,23 @@ public abstract class GeneratedMessage extends AbstractMessage
//@Override (Java 1.6 override semantics, but we must support 1.5)
public Map<FieldDescriptor, Object> getAllFields() {
- return Collections.unmodifiableMap(getAllFieldsMutable());
+ return Collections.unmodifiableMap(
+ getAllFieldsMutable(/* getBytesForString = */ false));
+ }
+
+ /**
+ * Returns a collection of all the fields in this message which are set
+ * and their corresponding values. A singular ("required" or "optional")
+ * field is set iff hasField() returns true for that field. A "repeated"
+ * field is set iff getRepeatedFieldCount() is greater than zero. The
+ * values are exactly what would be returned by calling
+ * {@link #getFieldRaw(Descriptors.FieldDescriptor)} for each field. The map
+ * is guaranteed to be a sorted map, so iterating over it will return fields
+ * in order by field number.
+ */
+ Map<FieldDescriptor, Object> getAllFieldsRaw() {
+ return Collections.unmodifiableMap(
+ getAllFieldsMutable(/* getBytesForString = */ true));
}
//@Override (Java 1.6 override semantics, but we must support 1.5)
@@ -184,6 +212,18 @@ public abstract class GeneratedMessage extends AbstractMessage
return internalGetFieldAccessorTable().getField(field).get(this);
}
+ /**
+ * Obtains the value of the given field, or the default value if it is
+ * not set. For primitive fields, the boxed primitive value is returned.
+ * For enum fields, the EnumValueDescriptor for the value is returned. For
+ * embedded message fields, the sub-message is returned. For repeated
+ * fields, a java.util.List is returned. For present string fields, a
+ * ByteString is returned representing the bytes that the field contains.
+ */
+ Object getFieldRaw(final FieldDescriptor field) {
+ return internalGetFieldAccessorTable().getField(field).getRaw(this);
+ }
+
//@Override (Java 1.6 override semantics, but we must support 1.5)
public int getRepeatedFieldCount(final FieldDescriptor field) {
return internalGetFieldAccessorTable().getField(field)
@@ -214,6 +254,24 @@ public abstract class GeneratedMessage extends AbstractMessage
return unknownFields.mergeFieldFrom(tag, input);
}
+ @Override
+ public void writeTo(final CodedOutputStream output) throws IOException {
+ MessageReflection.writeMessageTo(this, getAllFieldsRaw(), output, false);
+ }
+
+ @Override
+ public int getSerializedSize() {
+ int size = memoizedSize;
+ if (size != -1) {
+ return size;
+ }
+
+ memoizedSize = MessageReflection.getSerializedSize(
+ this, getAllFieldsRaw());
+ return memoizedSize;
+ }
+
+
/**
* Used by parsing constructors in generated classes.
@@ -549,12 +607,12 @@ public abstract class GeneratedMessage extends AbstractMessage
* Gets the map field with the given field number. This method should be
* overridden in the generated message class if the message contains map
* fields.
- *
+ *
* Unlike other field types, reflection support for map fields can't be
* 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
* 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"})
protected MapField internalGetMapField(int fieldNumber) {
@@ -563,6 +621,15 @@ public abstract class GeneratedMessage extends AbstractMessage
throw new RuntimeException(
"No map fields found in " + getClass().getName());
}
+
+ /** Like {@link internalGetMapField} but return a mutable version. */
+ @SuppressWarnings({"unused", "rawtypes"})
+ protected MapField internalGetMutableMapField(int fieldNumber) {
+ // Note that we can't use descriptor names here because this method will
+ // be called when descriptor is being initialized.
+ throw new RuntimeException(
+ "No map fields found in " + getClass().getName());
+ }
}
// =================================================================
@@ -683,7 +750,7 @@ public abstract class GeneratedMessage extends AbstractMessage
public final <Type> Type getExtension(
final ExtensionLite<MessageType, Type> extensionLite) {
Extension<MessageType, Type> extension = checkNotLite(extensionLite);
-
+
verifyExtensionContainingType(extension);
FieldDescriptor descriptor = extension.getDescriptor();
final Object value = extensions.getField(descriptor);
@@ -825,7 +892,16 @@ public abstract class GeneratedMessage extends AbstractMessage
@Override
public Map<FieldDescriptor, Object> getAllFields() {
- final Map<FieldDescriptor, Object> result = super.getAllFieldsMutable();
+ final Map<FieldDescriptor, Object> result =
+ super.getAllFieldsMutable(/* getBytesForString = */ false);
+ result.putAll(getExtensionFields());
+ return Collections.unmodifiableMap(result);
+ }
+
+ @Override
+ public Map<FieldDescriptor, Object> getAllFieldsRaw() {
+ final Map<FieldDescriptor, Object> result =
+ super.getAllFieldsMutable(/* getBytesForString = */ false);
result.putAll(getExtensionFields());
return Collections.unmodifiableMap(result);
}
@@ -1313,7 +1389,7 @@ public abstract class GeneratedMessage extends AbstractMessage
implements ExtensionDescriptorRetriever {
private volatile FieldDescriptor descriptor;
protected abstract FieldDescriptor loadDescriptor();
-
+
public FieldDescriptor getDescriptor() {
if (descriptor == null) {
synchronized (this) {
@@ -1651,17 +1727,17 @@ public abstract class GeneratedMessage extends AbstractMessage
}
}
}
-
+
/**
* Gets the map field with the given field number. This method should be
* overridden in the generated message class if the message contains map
* fields.
- *
+ *
* Unlike other field types, reflection support for map fields can't be
* 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
* 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"})
protected MapField internalGetMapField(int fieldNumber) {
@@ -1709,7 +1785,7 @@ public abstract class GeneratedMessage extends AbstractMessage
oneofs = new OneofAccessor[descriptor.getOneofs().size()];
initialized = false;
}
-
+
private boolean isMapFieldEnabled(FieldDescriptor field) {
boolean result = true;
return result;
@@ -1761,6 +1837,10 @@ public abstract class GeneratedMessage extends AbstractMessage
fields[i] = new SingularEnumFieldAccessor(
field, camelCaseNames[i], messageClass, builderClass,
containingOneofCamelCaseName);
+ } else if (field.getJavaType() == FieldDescriptor.JavaType.STRING) {
+ fields[i] = new SingularStringFieldAccessor(
+ field, camelCaseNames[i], messageClass, builderClass,
+ containingOneofCamelCaseName);
} else {
fields[i] = new SingularFieldAccessor(
field, camelCaseNames[i], messageClass, builderClass,
@@ -1817,9 +1897,13 @@ public abstract class GeneratedMessage extends AbstractMessage
private interface FieldAccessor {
Object get(GeneratedMessage message);
Object get(GeneratedMessage.Builder builder);
+ Object getRaw(GeneratedMessage message);
+ Object getRaw(GeneratedMessage.Builder builder);
void set(Builder builder, Object value);
Object getRepeated(GeneratedMessage message, int index);
Object getRepeated(GeneratedMessage.Builder builder, int index);
+ Object getRepeatedRaw(GeneratedMessage message, int index);
+ Object getRepeatedRaw(GeneratedMessage.Builder builder, int index);
void setRepeated(Builder builder,
int index, Object value);
void addRepeated(Builder builder, Object value);
@@ -1934,11 +2018,11 @@ public abstract class GeneratedMessage extends AbstractMessage
protected final FieldDescriptor field;
protected final boolean isOneofField;
protected final boolean hasHasMethod;
-
+
private int getOneofFieldNumber(final GeneratedMessage message) {
return ((Internal.EnumLite) invokeOrDie(caseMethod, message)).getNumber();
}
-
+
private int getOneofFieldNumber(final GeneratedMessage.Builder builder) {
return ((Internal.EnumLite) invokeOrDie(caseMethodBuilder, builder)).getNumber();
}
@@ -1949,6 +2033,12 @@ public abstract class GeneratedMessage extends AbstractMessage
public Object get(GeneratedMessage.Builder builder) {
return invokeOrDie(getMethodBuilder, builder);
}
+ public Object getRaw(final GeneratedMessage message) {
+ return get(message);
+ }
+ public Object getRaw(GeneratedMessage.Builder builder) {
+ return get(builder);
+ }
public void set(final Builder builder, final Object value) {
invokeOrDie(setMethod, builder, value);
}
@@ -1957,12 +2047,22 @@ public abstract class GeneratedMessage extends AbstractMessage
throw new UnsupportedOperationException(
"getRepeatedField() called on a singular field.");
}
+ public Object getRepeatedRaw(final GeneratedMessage message,
+ final int index) {
+ throw new UnsupportedOperationException(
+ "getRepeatedFieldRaw() called on a singular field.");
+ }
public Object getRepeated(GeneratedMessage.Builder builder, int index) {
throw new UnsupportedOperationException(
"getRepeatedField() called on a singular field.");
}
- public void setRepeated(final Builder builder,
- final int index, final Object value) {
+ public Object getRepeatedRaw(GeneratedMessage.Builder builder,
+ int index) {
+ throw new UnsupportedOperationException(
+ "getRepeatedFieldRaw() called on a singular field.");
+ }
+ public void setRepeated(final Builder builder, final int index,
+ final Object value) {
throw new UnsupportedOperationException(
"setRepeatedField() called on a singular field.");
}
@@ -2058,6 +2158,12 @@ public abstract class GeneratedMessage extends AbstractMessage
public Object get(GeneratedMessage.Builder builder) {
return invokeOrDie(getMethodBuilder, builder);
}
+ public Object getRaw(final GeneratedMessage message) {
+ return get(message);
+ }
+ public Object getRaw(GeneratedMessage.Builder builder) {
+ return get(builder);
+ }
public void set(final Builder builder, final Object value) {
// Add all the elements individually. This serves two purposes:
// 1) Verifies that each element has the correct type.
@@ -2075,6 +2181,13 @@ public abstract class GeneratedMessage extends AbstractMessage
public Object getRepeated(GeneratedMessage.Builder builder, int index) {
return invokeOrDie(getRepeatedMethodBuilder, builder, index);
}
+ public Object getRepeatedRaw(GeneratedMessage message, int index) {
+ return getRepeated(message, index);
+ }
+ public Object getRepeatedRaw(GeneratedMessage.Builder builder,
+ int index) {
+ return getRepeated(builder, index);
+ }
public void setRepeated(final Builder builder,
final int index, final Object value) {
invokeOrDie(setRepeatedMethod, builder, index, value);
@@ -2130,15 +2243,21 @@ public abstract class GeneratedMessage extends AbstractMessage
private final FieldDescriptor field;
private final Message mapEntryMessageDefaultInstance;
-
+
private MapField<?, ?> getMapField(GeneratedMessage message) {
return (MapField<?, ?>) message.internalGetMapField(field.getNumber());
}
-
+
private MapField<?, ?> getMapField(GeneratedMessage.Builder builder) {
return (MapField<?, ?>) builder.internalGetMapField(field.getNumber());
}
-
+
+ private MapField<?, ?> getMutableMapField(
+ GeneratedMessage.Builder builder) {
+ return (MapField<?, ?>) builder.internalGetMutableMapField(
+ field.getNumber());
+ }
+
public Object get(GeneratedMessage message) {
List result = new ArrayList();
for (int i = 0; i < getRepeatedCount(message); i++) {
@@ -2155,6 +2274,14 @@ public abstract class GeneratedMessage extends AbstractMessage
return Collections.unmodifiableList(result);
}
+ public Object getRaw(GeneratedMessage message) {
+ return get(message);
+ }
+
+ public Object getRaw(GeneratedMessage.Builder builder) {
+ return get(builder);
+ }
+
public void set(Builder builder, Object value) {
clear(builder);
for (Object entry : (List) value) {
@@ -2170,12 +2297,20 @@ public abstract class GeneratedMessage extends AbstractMessage
return getMapField(builder).getList().get(index);
}
+ public Object getRepeatedRaw(GeneratedMessage message, int index) {
+ return getRepeated(message, index);
+ }
+
+ public Object getRepeatedRaw(Builder builder, int index) {
+ return getRepeated(builder, index);
+ }
+
public void setRepeated(Builder builder, int index, Object value) {
- getMapField(builder).getMutableList().set(index, (Message) value);
+ getMutableMapField(builder).getMutableList().set(index, (Message) value);
}
public void addRepeated(Builder builder, Object value) {
- getMapField(builder).getMutableList().add((Message) value);
+ getMutableMapField(builder).getMutableList().add((Message) value);
}
public boolean has(GeneratedMessage message) {
@@ -2197,7 +2332,7 @@ public abstract class GeneratedMessage extends AbstractMessage
}
public void clear(Builder builder) {
- getMapField(builder).getMutableList().clear();
+ getMutableMapField(builder).getMutableList().clear();
}
public com.google.protobuf.Message.Builder newBuilder() {
@@ -2208,7 +2343,7 @@ public abstract class GeneratedMessage extends AbstractMessage
throw new UnsupportedOperationException(
"Nested builder not supported for map fields.");
}
-
+
public com.google.protobuf.Message.Builder getRepeatedBuilder(
Builder builder, int index) {
throw new UnsupportedOperationException(
@@ -2226,7 +2361,7 @@ public abstract class GeneratedMessage extends AbstractMessage
final Class<? extends Builder> builderClass,
final String containingOneofCamelCaseName) {
super(descriptor, camelCaseName, messageClass, builderClass, containingOneofCamelCaseName);
-
+
enumDescriptor = descriptor.getEnumType();
valueOfMethod = getMethodOrDie(type, "valueOf",
@@ -2244,12 +2379,12 @@ public abstract class GeneratedMessage extends AbstractMessage
getMethodOrDie(builderClass, "set" + camelCaseName + "Value", int.class);
}
}
-
+
private EnumDescriptor enumDescriptor;
private Method valueOfMethod;
private Method getValueDescriptorMethod;
-
+
private boolean supportUnknownEnumValue;
private Method getValueMethod;
private Method getValueMethodBuilder;
@@ -2291,7 +2426,7 @@ public abstract class GeneratedMessage extends AbstractMessage
final Class<? extends GeneratedMessage> messageClass,
final Class<? extends Builder> builderClass) {
super(descriptor, camelCaseName, messageClass, builderClass);
-
+
enumDescriptor = descriptor.getEnumType();
valueOfMethod = getMethodOrDie(type, "valueOf",
@@ -2315,7 +2450,7 @@ public abstract class GeneratedMessage extends AbstractMessage
private final Method valueOfMethod;
private final Method getValueDescriptorMethod;
-
+
private boolean supportUnknownEnumValue;
private Method getRepeatedValueMethod;
private Method getRepeatedValueMethodBuilder;
@@ -2388,6 +2523,60 @@ public abstract class GeneratedMessage extends AbstractMessage
// ---------------------------------------------------------------
+ /**
+ * Field accessor for string fields.
+ *
+ * <p>This class makes getFooBytes() and setFooBytes() available for
+ * reflection API so that reflection based serialize/parse functions can
+ * access the raw bytes of the field to preserve non-UTF8 bytes in the
+ * string.
+ *
+ * <p>This ensures the serialize/parse round-trip safety, which is important
+ * for servers which forward messages.
+ */
+ private static final class SingularStringFieldAccessor
+ extends SingularFieldAccessor {
+ SingularStringFieldAccessor(
+ final FieldDescriptor descriptor, final String camelCaseName,
+ final Class<? extends GeneratedMessage> messageClass,
+ final Class<? extends Builder> builderClass,
+ final String containingOneofCamelCaseName) {
+ super(descriptor, camelCaseName, messageClass, builderClass,
+ containingOneofCamelCaseName);
+ getBytesMethod = getMethodOrDie(messageClass,
+ "get" + camelCaseName + "Bytes");
+ getBytesMethodBuilder = getMethodOrDie(builderClass,
+ "get" + camelCaseName + "Bytes");
+ setBytesMethodBuilder = getMethodOrDie(builderClass,
+ "set" + camelCaseName + "Bytes", ByteString.class);
+ }
+
+ private final Method getBytesMethod;
+ private final Method getBytesMethodBuilder;
+ private final Method setBytesMethodBuilder;
+
+ @Override
+ public Object getRaw(final GeneratedMessage message) {
+ return invokeOrDie(getBytesMethod, message);
+ }
+
+ @Override
+ public Object getRaw(GeneratedMessage.Builder builder) {
+ return invokeOrDie(getBytesMethodBuilder, builder);
+ }
+
+ @Override
+ public void set(GeneratedMessage.Builder builder, Object value) {
+ if (value instanceof ByteString) {
+ invokeOrDie(setBytesMethodBuilder, builder, value);
+ } else {
+ super.set(builder, value);
+ }
+ }
+ }
+
+ // ---------------------------------------------------------------
+
private static final class SingularMessageFieldAccessor
extends SingularFieldAccessor {
SingularMessageFieldAccessor(
@@ -2395,7 +2584,8 @@ public abstract class GeneratedMessage extends AbstractMessage
final Class<? extends GeneratedMessage> messageClass,
final Class<? extends Builder> builderClass,
final String containingOneofCamelCaseName) {
- super(descriptor, camelCaseName, messageClass, builderClass, containingOneofCamelCaseName);
+ super(descriptor, camelCaseName, messageClass, builderClass,
+ containingOneofCamelCaseName);
newBuilderMethod = getMethodOrDie(type, "newBuilder");
getBuilderMethodBuilder =
@@ -2492,7 +2682,7 @@ public abstract class GeneratedMessage extends AbstractMessage
protected Object writeReplace() throws ObjectStreamException {
return new GeneratedMessageLite.SerializedForm(this);
}
-
+
/**
* Checks that the {@link Extension} is non-Lite and returns it as a
* {@link GeneratedExtension}.
@@ -2503,7 +2693,7 @@ public abstract class GeneratedMessage extends AbstractMessage
if (extension.isLite()) {
throw new IllegalArgumentException("Expected non-lite extension.");
}
-
+
return (Extension<MessageType, T>) extension;
}
}
diff --git a/java/src/main/java/com/google/protobuf/GeneratedMessageLite.java b/java/src/main/java/com/google/protobuf/GeneratedMessageLite.java
index 4d25c077..6839c9dd 100644
--- a/java/src/main/java/com/google/protobuf/GeneratedMessageLite.java
+++ b/java/src/main/java/com/google/protobuf/GeneratedMessageLite.java
@@ -42,22 +42,58 @@ import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
/**
* Lite version of {@link GeneratedMessage}.
*
* @author kenton@google.com Kenton Varda
*/
-public abstract class GeneratedMessageLite extends AbstractMessageLite
- implements Serializable {
+public abstract class GeneratedMessageLite<
+ MessageType extends GeneratedMessageLite<MessageType, BuilderType>,
+ BuilderType extends GeneratedMessageLite.Builder<MessageType, BuilderType>>
+ extends AbstractMessageLite
+ implements Serializable {
+
+ /**
+ * Holds all the {@link PrototypeHolder}s for loaded classes.
+ */
+ // TODO(dweis): Consider different concurrency values.
+ // TODO(dweis): This will prevent garbage collection of the class loader.
+ // Ideally we'd use something like ClassValue but that's Java 7 only.
+ private static final Map<Class<?>, PrototypeHolder<?, ?>> PROTOTYPE_MAP =
+ new ConcurrentHashMap<Class<?>, PrototypeHolder<?, ?>>();
+
+ // For use by generated code only.
+ protected static <
+ MessageType extends GeneratedMessageLite<MessageType, BuilderType>,
+ BuilderType extends GeneratedMessageLite.Builder<
+ MessageType, BuilderType>> void onLoad(Class<MessageType> clazz,
+ PrototypeHolder<MessageType, BuilderType> protoTypeHolder) {
+ PROTOTYPE_MAP.put(clazz, protoTypeHolder);
+ }
+
private static final long serialVersionUID = 1L;
/** For use by generated code only. */
protected UnknownFieldSetLite unknownFields;
- public Parser<? extends MessageLite> getParserForType() {
- throw new UnsupportedOperationException(
- "This is supposed to be overridden by subclasses.");
+ @SuppressWarnings("unchecked") // Guaranteed by runtime.
+ public final Parser<MessageType> getParserForType() {
+ return (Parser<MessageType>) PROTOTYPE_MAP
+ .get(getClass()).getParserForType();
+ }
+
+ @SuppressWarnings("unchecked") // Guaranteed by runtime.
+ public final MessageType getDefaultInstanceForType() {
+ return (MessageType) PROTOTYPE_MAP
+ .get(getClass()).getDefaultInstanceForType();
+ }
+
+ @SuppressWarnings("unchecked") // Guaranteed by runtime.
+ public final BuilderType newBuilderForType() {
+ return (BuilderType) PROTOTYPE_MAP
+ .get(getClass()).newBuilderForType();
}
/**
@@ -73,10 +109,17 @@ public abstract class GeneratedMessageLite extends AbstractMessageLite
return unknownFields.mergeFieldFrom(tag, input);
}
+ // The default behavior. If a message has required fields in its subtree, the
+ // generated code will override.
+ public boolean isInitialized() {
+ return true;
+ }
+
@SuppressWarnings("unchecked")
- public abstract static class Builder<MessageType extends GeneratedMessageLite,
- BuilderType extends Builder>
- extends AbstractMessageLite.Builder<BuilderType> {
+ public abstract static class Builder<
+ MessageType extends GeneratedMessageLite<MessageType, BuilderType>,
+ BuilderType extends Builder<MessageType, BuilderType>>
+ extends AbstractMessageLite.Builder<BuilderType> {
private final MessageType defaultInstance;
@@ -88,6 +131,12 @@ public abstract class GeneratedMessageLite extends AbstractMessageLite
this.defaultInstance = defaultInstance;
}
+ // The default behavior. If a message has required fields in its subtree,
+ // the generated code will override.
+ public boolean isInitialized() {
+ return true;
+ }
+
//@Override (Java 1.6 override semantics, but we must support 1.5)
public BuilderType clear() {
unknownFields = UnknownFieldSetLite.getDefaultInstance();
@@ -174,7 +223,9 @@ public abstract class GeneratedMessageLite extends AbstractMessageLite
* Lite equivalent of {@link com.google.protobuf.GeneratedMessage.ExtendableMessageOrBuilder}.
*/
public interface ExtendableMessageOrBuilder<
- MessageType extends ExtendableMessage> extends MessageLiteOrBuilder {
+ MessageType extends ExtendableMessage<MessageType, BuilderType>,
+ BuilderType extends ExtendableBuilder<MessageType, BuilderType>>
+ extends MessageLiteOrBuilder {
/** Check if a singular extension is present. */
<Type> boolean hasExtension(
@@ -197,16 +248,30 @@ public abstract class GeneratedMessageLite extends AbstractMessageLite
* Lite equivalent of {@link GeneratedMessage.ExtendableMessage}.
*/
public abstract static class ExtendableMessage<
- MessageType extends ExtendableMessage<MessageType>>
- extends GeneratedMessageLite
- implements ExtendableMessageOrBuilder<MessageType> {
+ MessageType extends ExtendableMessage<MessageType, BuilderType>,
+ BuilderType extends ExtendableBuilder<MessageType, BuilderType>>
+ extends GeneratedMessageLite<MessageType, BuilderType>
+ implements ExtendableMessageOrBuilder<MessageType, BuilderType> {
/**
* Represents the set of extensions on this message. For use by generated
* code only.
*/
protected FieldSet<ExtensionDescriptor> extensions = FieldSet.newFieldSet();
-
+
+ // -1 => not memoized, 0 => false, 1 => true.
+ private byte memoizedIsInitialized = -1;
+
+ // The default behavior. If a message has required fields in its subtree,
+ // the generated code will override.
+ public boolean isInitialized() {
+ if (memoizedIsInitialized == -1) {
+ memoizedIsInitialized = (byte) (extensions.isInitialized() ? 1 : 0);
+ }
+
+ return memoizedIsInitialized == 1;
+ }
+
private void verifyExtensionContainingType(
final GeneratedExtension<MessageType, ?> extension) {
if (extension.getContainingTypeDefaultInstance() !=
@@ -349,10 +414,10 @@ public abstract class GeneratedMessageLite extends AbstractMessageLite
*/
@SuppressWarnings("unchecked")
public abstract static class ExtendableBuilder<
- MessageType extends ExtendableMessage<MessageType>,
+ MessageType extends ExtendableMessage<MessageType, BuilderType>,
BuilderType extends ExtendableBuilder<MessageType, BuilderType>>
extends Builder<MessageType, BuilderType>
- implements ExtendableMessageOrBuilder<MessageType> {
+ implements ExtendableMessageOrBuilder<MessageType, BuilderType> {
protected ExtendableBuilder(MessageType defaultInstance) {
super(defaultInstance);
}
@@ -360,6 +425,12 @@ public abstract class GeneratedMessageLite extends AbstractMessageLite
private FieldSet<ExtensionDescriptor> extensions = FieldSet.emptySet();
private boolean extensionsIsMutable;
+ // The default behavior. If a message has required fields in its subtree,
+ // the generated code will override.
+ public boolean isInitialized() {
+ return extensions.isInitialized();
+ }
+
// For immutable message conversion.
void internalSetExtensionSet(FieldSet<ExtensionDescriptor> extensions) {
this.extensions = extensions;
@@ -991,7 +1062,10 @@ public abstract class GeneratedMessageLite extends AbstractMessageLite
* Checks that the {@link Extension} is Lite and returns it as a
* {@link GeneratedExtension}.
*/
- private static <MessageType extends ExtendableMessage<MessageType>, T>
+ private static <
+ MessageType extends ExtendableMessage<MessageType, BuilderType>,
+ BuilderType extends ExtendableBuilder<MessageType, BuilderType>,
+ T>
GeneratedExtension<MessageType, T> checkIsLite(
ExtensionLite<MessageType, T> extension) {
if (!extension.isLite()) {
@@ -1000,4 +1074,43 @@ public abstract class GeneratedMessageLite extends AbstractMessageLite
return (GeneratedExtension<MessageType, T>) extension;
}
+
+ /**
+ * Represents the state needed to implement *ForType methods. Generated code
+ * must provide a static singleton instance by adding it with
+ * {@link GeneratedMessageLite#onLoad(Class, PrototypeHolder)} on class load.
+ * <ul>
+ * <li>{@link #getDefaultInstanceForType()}
+ * <li>{@link #getParserForType()}
+ * <li>{@link #newBuilderForType()}
+ * </ul>
+ * This allows us to trade three generated methods for a static Map.
+ */
+ protected static class PrototypeHolder<
+ MessageType extends GeneratedMessageLite<MessageType, BuilderType>,
+ BuilderType extends GeneratedMessageLite.Builder<
+ MessageType, BuilderType>> {
+
+ private final MessageType defaultInstance;
+ private final Parser<MessageType> parser;
+
+ public PrototypeHolder(
+ MessageType defaultInstance, Parser<MessageType> parser) {
+ this.defaultInstance = defaultInstance;
+ this.parser = parser;
+ }
+
+ public MessageType getDefaultInstanceForType() {
+ return defaultInstance;
+ }
+
+ public Parser<MessageType> getParserForType() {
+ return parser;
+ }
+
+ @SuppressWarnings("unchecked") // Guaranteed by runtime.
+ public BuilderType newBuilderForType() {
+ return (BuilderType) defaultInstance.toBuilder();
+ }
+ }
}
diff --git a/java/src/main/java/com/google/protobuf/LazyFieldLite.java b/java/src/main/java/com/google/protobuf/LazyFieldLite.java
index 1fc80e87..eea1fe3c 100644
--- a/java/src/main/java/com/google/protobuf/LazyFieldLite.java
+++ b/java/src/main/java/com/google/protobuf/LazyFieldLite.java
@@ -30,8 +30,6 @@
package com.google.protobuf;
-import java.io.IOException;
-
/**
* LazyFieldLite encapsulates the logic of lazily parsing message fields. It stores
* the message in a ByteString initially and then parse it on-demand.
@@ -44,40 +42,102 @@ import java.io.IOException;
* @author xiangl@google.com (Xiang Li)
*/
public class LazyFieldLite {
- private ByteString bytes;
+ private static final ExtensionRegistryLite EMPTY_REGISTRY =
+ ExtensionRegistryLite.getEmptyRegistry();
+
+ /**
+ * A delayed-parsed version of the bytes. When this is non-null then {@code extensionRegistry } is
+ * also non-null and {@code value} and {@code memoizedBytes} are null.
+ */
+ private ByteString delayedBytes;
+
+ /**
+ * An {@code ExtensionRegistryLite} for parsing bytes. It is non-null on a best-effort basis. It
+ * is only guaranteed to be non-null if this message was initialized using bytes and an
+ * {@code ExtensionRegistry}. If it directly had a value set then it will be null, unless it has
+ * been merged with another {@code LazyFieldLite} that had an {@code ExtensionRegistry}.
+ */
private ExtensionRegistryLite extensionRegistry;
- private volatile boolean isDirty = false;
+ /**
+ * The parsed value. When this is non-null then {@code delayedBytes} will be null.
+ */
protected volatile MessageLite value;
+ /**
+ * The memoized bytes for {@code value}. Will be null when {@code value} is null.
+ */
+ private volatile ByteString memoizedBytes;
+
+ /**
+ * Constructs a LazyFieldLite with bytes that will be parsed lazily.
+ */
public LazyFieldLite(ExtensionRegistryLite extensionRegistry, ByteString bytes) {
+ checkArguments(extensionRegistry, bytes);
this.extensionRegistry = extensionRegistry;
- this.bytes = bytes;
+ this.delayedBytes = bytes;
}
+ /**
+ * Constructs a LazyFieldLite with no contents, and no ability to parse extensions.
+ */
public LazyFieldLite() {
}
+ /**
+ * Constructs a LazyFieldLite instance with a value. The LazyFieldLite may not be able to parse
+ * the extensions in the value as it has no ExtensionRegistry.
+ */
public static LazyFieldLite fromValue(MessageLite value) {
LazyFieldLite lf = new LazyFieldLite();
lf.setValue(value);
return lf;
}
+ /**
+ * Determines whether this LazyFieldLite instance represents the default instance of this type.
+ */
public boolean containsDefaultInstance() {
- return value == null && bytes == null;
+ return memoizedBytes == ByteString.EMPTY
+ || value == null && (delayedBytes == null || delayedBytes == ByteString.EMPTY);
}
+ /**
+ * Clears the value state of this instance.
+ *
+ * <p>LazyField is not thread-safe for write access. Synchronizations are needed
+ * under read/write situations.
+ */
public void clear() {
- bytes = null;
+ // Don't clear the ExtensionRegistry. It might prove useful later on when merging in another
+ // value, but there is no guarantee that it will contain all extensions that were directly set
+ // on the values that need to be merged.
+ delayedBytes = null;
value = null;
- extensionRegistry = null;
- isDirty = true;
+ memoizedBytes = null;
+ }
+
+ /**
+ * Overrides the contents of this LazyField.
+ *
+ * <p>LazyField is not thread-safe for write access. Synchronizations are needed
+ * under read/write situations.
+ */
+ public void set(LazyFieldLite other) {
+ this.delayedBytes = other.delayedBytes;
+ this.value = other.value;
+ this.memoizedBytes = other.memoizedBytes;
+ // If the other LazyFieldLite was created by directly setting the value rather than first by
+ // parsing, then it will not have an extensionRegistry. In this case we hold on to the existing
+ // extensionRegistry, which has no guarantees that it has all the extensions that will be
+ // directly set on the value.
+ if (other.extensionRegistry != null) {
+ this.extensionRegistry = other.extensionRegistry;
+ }
}
/**
- * Returns message instance. At first time, serialized data is parsed by
- * {@code defaultInstance.getParserForType()}.
+ * Returns message instance. It may do some thread-safe delayed parsing of bytes.
*
* @param defaultInstance its message's default instance. It's also used to get parser for the
* message type.
@@ -88,38 +148,109 @@ public class LazyFieldLite {
}
/**
- * LazyField is not thread-safe for write access. Synchronizations are needed
+ * Sets the value of the instance and returns the old value without delay parsing anything.
+ *
+ * <p>LazyField is not thread-safe for write access. Synchronizations are needed
* under read/write situations.
*/
public MessageLite setValue(MessageLite value) {
MessageLite originalValue = this.value;
+ this.delayedBytes = null;
+ this.memoizedBytes = null;
this.value = value;
- bytes = null;
- isDirty = true;
return originalValue;
}
- public void merge(LazyFieldLite value) {
- if (value.containsDefaultInstance()) {
+ /**
+ * Merges another instance's contents. In some cases may drop some extensions if both fields
+ * contain data. If the other field has an {@code ExtensionRegistry} but this does not, then this
+ * field will copy over that {@code ExtensionRegistry}.
+ *
+ * <p>LazyField is not thread-safe for write access. Synchronizations are needed
+ * under read/write situations.
+ */
+ public void merge(LazyFieldLite other) {
+ if (other.containsDefaultInstance()) {
return;
}
- if (bytes == null) {
- this.bytes = value.bytes;
+ if (this.containsDefaultInstance()) {
+ set(other);
+ return;
+ }
+
+ // If the other field has an extension registry but this does not, copy over the other extension
+ // registry.
+ if (this.extensionRegistry == null) {
+ this.extensionRegistry = other.extensionRegistry;
+ }
+
+ // In the case that both of them are not parsed we simply concatenate the bytes to save time. In
+ // the (probably rare) case that they have different extension registries there is a chance that
+ // some of the extensions may be dropped, but the tradeoff of making this operation fast seems
+ // to outway the benefits of combining the extension registries, which is not normally done for
+ // lite protos anyways.
+ if (this.delayedBytes != null && other.delayedBytes != null) {
+ this.delayedBytes = this.delayedBytes.concat(other.delayedBytes);
+ return;
+ }
+
+ // At least one is parsed and both contain data. We won't drop any extensions here directly, but
+ // in the case that the extension registries are not the same then we might in the future if we
+ // need to serialze and parse a message again.
+ if (this.value == null && other.value != null) {
+ setValue(mergeValueAndBytes(other.value, this.delayedBytes, this.extensionRegistry));
+ return;
+ } else if (this.value != null && other.value == null) {
+ setValue(mergeValueAndBytes(this.value, other.delayedBytes, other.extensionRegistry));
+ return;
+ }
+
+ // At this point we have two fully parsed messages. We can't merge directly from one to the
+ // other because only generated builder code contains methods to mergeFrom another parsed
+ // message. We have to serialize one instance and then merge the bytes into the other. This may
+ // drop extensions from one of the messages if one of the values had an extension set on it
+ // directly.
+ //
+ // To mitigate this we prefer serializing a message that has an extension registry, and
+ // therefore a chance that all extensions set on it are in that registry.
+ //
+ // NOTE: The check for other.extensionRegistry not being null must come first because at this
+ // point in time if other.extensionRegistry is not null then this.extensionRegistry will not be
+ // null either.
+ if (other.extensionRegistry != null) {
+ setValue(mergeValueAndBytes(this.value, other.toByteString(), other.extensionRegistry));
+ return;
+ } else if (this.extensionRegistry != null) {
+ setValue(mergeValueAndBytes(other.value, this.toByteString(), this.extensionRegistry));
+ return;
} else {
- this.bytes.concat(value.toByteString());
+ // All extensions from the other message will be dropped because we have no registry.
+ setValue(mergeValueAndBytes(this.value, other.toByteString(), EMPTY_REGISTRY));
+ return;
}
- isDirty = false;
}
- public void setByteString(ByteString bytes, ExtensionRegistryLite extensionRegistry) {
- this.bytes = bytes;
- this.extensionRegistry = extensionRegistry;
- isDirty = false;
+ private static MessageLite mergeValueAndBytes(
+ MessageLite value, ByteString otherBytes, ExtensionRegistryLite extensionRegistry) {
+ try {
+ return value.toBuilder().mergeFrom(otherBytes, extensionRegistry).build();
+ } catch (InvalidProtocolBufferException e) {
+ // Nothing is logged and no exceptions are thrown. Clients will be unaware that a proto
+ // was invalid.
+ return value;
+ }
}
- public ExtensionRegistryLite getExtensionRegistry() {
- return extensionRegistry;
+ /**
+ * Sets this field with bytes to delay-parse.
+ */
+ public void setByteString(ByteString bytes, ExtensionRegistryLite extensionRegistry) {
+ checkArguments(extensionRegistry, bytes);
+ this.delayedBytes = bytes;
+ this.extensionRegistry = extensionRegistry;
+ this.value = null;
+ this.memoizedBytes = null;
}
/**
@@ -128,30 +259,43 @@ public class LazyFieldLite {
* parsed. Be careful when using this method.
*/
public int getSerializedSize() {
- if (isDirty) {
+ if (delayedBytes != null) {
+ return delayedBytes.size();
+ } else if (memoizedBytes != null) {
+ return memoizedBytes.size();
+ } else if (value != null) {
return value.getSerializedSize();
+ } else {
+ return 0;
}
- return bytes.size();
}
+ /**
+ * Returns a BytesString for this field in a thread-safe way.
+ */
public ByteString toByteString() {
- if (!isDirty) {
- return bytes;
+ if (delayedBytes != null) {
+ return delayedBytes;
+ }
+ if (memoizedBytes != null) {
+ return memoizedBytes;
}
synchronized (this) {
- if (!isDirty) {
- return bytes;
+ if (memoizedBytes != null) {
+ return memoizedBytes;
}
if (value == null) {
- bytes = ByteString.EMPTY;
+ memoizedBytes = ByteString.EMPTY;
} else {
- bytes = value.toByteString();
+ memoizedBytes = value.toByteString();
}
- isDirty = false;
- return bytes;
+ return memoizedBytes;
}
}
+ /**
+ * Might lazily parse the bytes that were previously passed in. Is thread-safe.
+ */
protected void ensureInitialized(MessageLite defaultInstance) {
if (value != null) {
return;
@@ -161,16 +305,35 @@ public class LazyFieldLite {
return;
}
try {
- if (bytes != null) {
- value = defaultInstance.getParserForType()
- .parseFrom(bytes, extensionRegistry);
+ if (delayedBytes != null) {
+ // The extensionRegistry shouldn't be null here since we have delayedBytes.
+ MessageLite parsedValue = defaultInstance.getParserForType()
+ .parseFrom(delayedBytes, extensionRegistry);
+ this.value = parsedValue;
+ this.memoizedBytes = delayedBytes;
+ this.delayedBytes = null;
} else {
- value = defaultInstance;
+ this.value = defaultInstance;
+ this.memoizedBytes = ByteString.EMPTY;
+ this.delayedBytes = null;
}
- } catch (IOException e) {
- // TODO(xiangl): Refactory the API to support the exception thrown from
- // lazily load messages.
+ } catch (InvalidProtocolBufferException e) {
+ // Nothing is logged and no exceptions are thrown. Clients will be unaware that this proto
+ // was invalid.
+ this.value = defaultInstance;
+ this.memoizedBytes = ByteString.EMPTY;
+ this.delayedBytes = null;
}
}
}
+
+
+ private static void checkArguments(ExtensionRegistryLite extensionRegistry, ByteString bytes) {
+ if (extensionRegistry == null) {
+ throw new NullPointerException("found null ExtensionRegistry");
+ }
+ if (bytes == null) {
+ throw new NullPointerException("found null ByteString");
+ }
+ }
}
diff --git a/java/src/main/java/com/google/protobuf/LiteralByteString.java b/java/src/main/java/com/google/protobuf/LiteralByteString.java
index 767b9f35..6893ddf1 100644
--- a/java/src/main/java/com/google/protobuf/LiteralByteString.java
+++ b/java/src/main/java/com/google/protobuf/LiteralByteString.java
@@ -154,7 +154,11 @@ class LiteralByteString extends ByteString {
@Override
public String toString(String charsetName)
throws UnsupportedEncodingException {
- return new String(bytes, getOffsetIntoBytes(), size(), charsetName);
+ // Optimize for empty strings, but ensure we don't silently ignore invalid
+ // encodings.
+ return size() == 0 && UTF_8.equals(charsetName)
+ ? ""
+ : new String(bytes, getOffsetIntoBytes(), size(), charsetName);
}
// =================================================================
diff --git a/java/src/main/java/com/google/protobuf/Message.java b/java/src/main/java/com/google/protobuf/Message.java
index 5c75b58b..fa0265e2 100644
--- a/java/src/main/java/com/google/protobuf/Message.java
+++ b/java/src/main/java/com/google/protobuf/Message.java
@@ -163,7 +163,7 @@ public interface Message extends MessageLite, MessageOrBuilder {
* field builder, which will then be nested into its parent builder.
* <p>
* NOTE: implementations that do not support nested builders will throw
- * <code>UnsupportedException</code>.
+ * <code>UnsupportedOperationException</code>.
*/
Builder getFieldBuilder(Descriptors.FieldDescriptor field);
@@ -181,7 +181,7 @@ public interface Message extends MessageLite, MessageOrBuilder {
* field builder, which will then be nested into its parent builder.
* <p>
* NOTE: implementations that do not support nested builders will throw
- * <code>UnsupportedException</code>.
+ * <code>UnsupportedOperationException</code>.
*/
Builder getRepeatedFieldBuilder(Descriptors.FieldDescriptor field,
int index);
diff --git a/java/src/main/java/com/google/protobuf/MessageReflection.java b/java/src/main/java/com/google/protobuf/MessageReflection.java
index 06e3c99b..de4bfd3e 100644
--- a/java/src/main/java/com/google/protobuf/MessageReflection.java
+++ b/java/src/main/java/com/google/protobuf/MessageReflection.java
@@ -45,13 +45,14 @@ import java.util.TreeMap;
*/
class MessageReflection {
- static void writeMessageTo(Message message, CodedOutputStream output,
+ static void writeMessageTo(
+ Message message,
+ Map<FieldDescriptor, Object> fields,
+ CodedOutputStream output,
boolean alwaysWriteRequiredFields)
throws IOException {
final boolean isMessageSet =
message.getDescriptorForType().getOptions().getMessageSetWireFormat();
-
- Map<FieldDescriptor, Object> fields = message.getAllFields();
if (alwaysWriteRequiredFields) {
fields = new TreeMap<FieldDescriptor, Object>(fields);
for (final FieldDescriptor field :
@@ -82,13 +83,15 @@ class MessageReflection {
}
}
- static int getSerializedSize(Message message) {
+ static int getSerializedSize(
+ Message message,
+ Map<FieldDescriptor, Object> fields) {
int size = 0;
final boolean isMessageSet =
message.getDescriptorForType().getOptions().getMessageSetWireFormat();
for (final Map.Entry<Descriptors.FieldDescriptor, Object> entry :
- message.getAllFields().entrySet()) {
+ fields.entrySet()) {
final Descriptors.FieldDescriptor field = entry.getKey();
final Object value = entry.getValue();
if (isMessageSet && field.isExtension() &&
@@ -340,14 +343,12 @@ class MessageReflection {
ByteString bytes, ExtensionRegistryLite registry,
Descriptors.FieldDescriptor descriptor, Message defaultInstance)
throws IOException;
-
+
/**
- * Read a primitive field from input. Note that builders and mutable
- * messages may use different Java types to represent a primtive field.
+ * Returns the UTF8 validation level for the field.
*/
- Object readPrimitiveField(
- CodedInputStream input, WireFormat.FieldType type,
- boolean checkUtf8) throws IOException;
+ WireFormat.Utf8Validation getUtf8Validation(Descriptors.FieldDescriptor
+ descriptor);
/**
* Returns a new merge target for a sub-field. When defaultInstance is
@@ -513,11 +514,18 @@ class MessageReflection {
return new BuilderAdapter(builder.newBuilderForField(field));
}
}
-
- public Object readPrimitiveField(
- CodedInputStream input, WireFormat.FieldType type,
- boolean checkUtf8) throws IOException {
- return FieldSet.readPrimitiveField(input, type, checkUtf8);
+
+ public WireFormat.Utf8Validation
+ getUtf8Validation(Descriptors.FieldDescriptor descriptor) {
+ if (descriptor.needsUtf8Check()) {
+ return WireFormat.Utf8Validation.STRICT;
+ }
+ // TODO(liujisi): support lazy strings for repeated fields.
+ if (!descriptor.isRepeated()
+ && builder instanceof GeneratedMessage.Builder) {
+ return WireFormat.Utf8Validation.LAZY;
+ }
+ return WireFormat.Utf8Validation.LOOSE;
}
public Object finish() {
@@ -651,11 +659,14 @@ class MessageReflection {
throw new UnsupportedOperationException(
"newMergeTargetForField() called on FieldSet object");
}
-
- public Object readPrimitiveField(
- CodedInputStream input, WireFormat.FieldType type,
- boolean checkUtf8) throws IOException {
- return FieldSet.readPrimitiveField(input, type, checkUtf8);
+
+ public WireFormat.Utf8Validation
+ getUtf8Validation(Descriptors.FieldDescriptor descriptor) {
+ if (descriptor.needsUtf8Check()) {
+ return WireFormat.Utf8Validation.STRICT;
+ }
+ // TODO(liujisi): support lazy strings for ExtesnsionSet.
+ return WireFormat.Utf8Validation.LOOSE;
}
public Object finish() {
@@ -767,8 +778,8 @@ class MessageReflection {
}
} else {
while (input.getBytesUntilLimit() > 0) {
- final Object value =
- target.readPrimitiveField(input, field.getLiteType(), field.needsUtf8Check());
+ final Object value = WireFormat.readPrimitiveField(
+ input, field.getLiteType(), target.getUtf8Validation(field));
target.addRepeatedField(field, value);
}
}
@@ -801,7 +812,8 @@ class MessageReflection {
}
break;
default:
- value = target.readPrimitiveField(input, field.getLiteType(), field.needsUtf8Check());
+ value = WireFormat.readPrimitiveField(
+ input, field.getLiteType(), target.getUtf8Validation(field));
break;
}
diff --git a/java/src/main/java/com/google/protobuf/Parser.java b/java/src/main/java/com/google/protobuf/Parser.java
index f636014b..227c02b7 100644
--- a/java/src/main/java/com/google/protobuf/Parser.java
+++ b/java/src/main/java/com/google/protobuf/Parser.java
@@ -227,8 +227,8 @@ public interface Parser<MessageType> {
* {@link MessageLite#writeDelimitedTo(java.io.OutputStream)} to write
* messages in this format.
*
- * @return True if successful, or false if the stream is at EOF when the
- * method starts. Any other error (including reaching EOF during
+ * @return Parsed message if successful, or null if the stream is at EOF when
+ * the method starts. Any other error (including reaching EOF during
* parsing) will cause an exception to be thrown.
*/
public MessageType parseDelimitedFrom(InputStream input)
diff --git a/java/src/main/java/com/google/protobuf/RopeByteString.java b/java/src/main/java/com/google/protobuf/RopeByteString.java
index fa23c9dd..168bcce2 100644
--- a/java/src/main/java/com/google/protobuf/RopeByteString.java
+++ b/java/src/main/java/com/google/protobuf/RopeByteString.java
@@ -30,9 +30,9 @@
package com.google.protobuf;
-import java.io.InvalidObjectException;
import java.io.IOException;
import java.io.InputStream;
+import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
@@ -420,7 +420,11 @@ class RopeByteString extends ByteString {
@Override
public String toString(String charsetName)
throws UnsupportedEncodingException {
- return new String(toByteArray(), charsetName);
+ // Optimize for empty strings, but ensure we don't silently ignore invalid
+ // encodings.
+ return size() == 0 && UTF_8.equals(charsetName)
+ ? ""
+ : new String(toByteArray(), charsetName);
}
// =================================================================
diff --git a/java/src/main/java/com/google/protobuf/TextFormat.java b/java/src/main/java/com/google/protobuf/TextFormat.java
index 4f6756ed..dd2b4600 100644
--- a/java/src/main/java/com/google/protobuf/TextFormat.java
+++ b/java/src/main/java/com/google/protobuf/TextFormat.java
@@ -409,9 +409,9 @@ public final class TextFormat {
case STRING:
generator.print("\"");
- generator.print(escapeNonAscii ?
- escapeText((String) value) :
- escapeDoubleQuotesAndBackslashes((String) value)
+ generator.print(escapeNonAscii
+ ? escapeText((String) value)
+ : escapeDoubleQuotesAndBackslashes((String) value)
.replace("\n", "\\n"));
generator.print("\"");
break;
@@ -730,8 +730,8 @@ public final class TextFormat {
}
final char c = currentToken.charAt(0);
- return ('0' <= c && c <= '9') ||
- c == '-' || c == '+';
+ return ('0' <= c && c <= '9')
+ || c == '-' || c == '+';
}
/**
@@ -749,10 +749,10 @@ public final class TextFormat {
public String consumeIdentifier() throws ParseException {
for (int i = 0; i < currentToken.length(); i++) {
final char c = currentToken.charAt(i);
- if (('a' <= c && c <= 'z') ||
- ('A' <= c && c <= 'Z') ||
- ('0' <= c && c <= '9') ||
- (c == '_') || (c == '.')) {
+ if (('a' <= c && c <= 'z')
+ || ('A' <= c && c <= 'Z')
+ || ('0' <= c && c <= '9')
+ || (c == '_') || (c == '.')) {
// OK
} else {
throw parseException(
@@ -941,14 +941,14 @@ public final class TextFormat {
* Otherwise, throw a {@link ParseException}.
*/
public boolean consumeBoolean() throws ParseException {
- if (currentToken.equals("true") ||
- currentToken.equals("t") ||
- currentToken.equals("1")) {
+ if (currentToken.equals("true")
+ || currentToken.equals("t")
+ || currentToken.equals("1")) {
nextToken();
return true;
- } else if (currentToken.equals("false") ||
- currentToken.equals("f") ||
- currentToken.equals("0")) {
+ } else if (currentToken.equals("false")
+ || currentToken.equals("f")
+ || currentToken.equals("0")) {
nextToken();
return false;
} else {
@@ -999,14 +999,15 @@ public final class TextFormat {
*/
private void consumeByteString(List<ByteString> list)
throws ParseException {
- final char quote = currentToken.length() > 0 ? currentToken.charAt(0)
- : '\0';
+ final char quote = currentToken.length() > 0
+ ? currentToken.charAt(0)
+ : '\0';
if (quote != '\"' && quote != '\'') {
throw parseException("Expected string.");
}
- if (currentToken.length() < 2 ||
- currentToken.charAt(currentToken.length() - 1) != quote) {
+ if (currentToken.length() < 2
+ || currentToken.charAt(currentToken.length() - 1) != quote) {
throw parseException("String missing ending quote.");
}
@@ -1340,8 +1341,8 @@ public final class TextFormat {
} else {
if (extension.descriptor.getContainingType() != type) {
throw tokenizer.parseExceptionPreviousToken(
- "Extension \"" + name + "\" does not extend message type \"" +
- type.getFullName() + "\".");
+ "Extension \"" + name + "\" does not extend message type \""
+ + type.getFullName() + "\".");
}
field = extension.descriptor;
}
@@ -1365,20 +1366,20 @@ public final class TextFormat {
}
}
// Again, special-case group names as described above.
- if (field != null && field.getType() == FieldDescriptor.Type.GROUP &&
- !field.getMessageType().getName().equals(name)) {
+ if (field != null && field.getType() == FieldDescriptor.Type.GROUP
+ && !field.getMessageType().getName().equals(name)) {
field = null;
}
if (field == null) {
if (!allowUnknownFields) {
throw tokenizer.parseExceptionPreviousToken(
- "Message type \"" + type.getFullName() +
- "\" has no field named \"" + name + "\".");
+ "Message type \"" + type.getFullName()
+ + "\" has no field named \"" + name + "\".");
} else {
logger.warning(
- "Message type \"" + type.getFullName() +
- "\" has no field named \"" + name + "\".");
+ "Message type \"" + type.getFullName()
+ + "\" has no field named \"" + name + "\".");
}
}
}
@@ -1391,8 +1392,9 @@ public final class TextFormat {
// start with "{" or "<" which indicates the beginning of a message body.
// If there is no ":" or there is a "{" or "<" after ":", this field has
// to be a message or the input is ill-formed.
- if (tokenizer.tryConsume(":") && !tokenizer.lookingAt("{") &&
- !tokenizer.lookingAt("<")) {
+ if (tokenizer.tryConsume(":")
+ && !tokenizer.lookingAt("{")
+ && !tokenizer.lookingAt("<")) {
skipFieldValue(tokenizer);
} else {
skipFieldMessage(tokenizer);
@@ -1516,16 +1518,16 @@ public final class TextFormat {
value = enumType.findValueByNumber(number);
if (value == null) {
throw tokenizer.parseExceptionPreviousToken(
- "Enum type \"" + enumType.getFullName() +
- "\" has no value with number " + number + '.');
+ "Enum type \"" + enumType.getFullName()
+ + "\" has no value with number " + number + '.');
}
} else {
final String id = tokenizer.consumeIdentifier();
value = enumType.findValueByName(id);
if (value == null) {
throw tokenizer.parseExceptionPreviousToken(
- "Enum type \"" + enumType.getFullName() +
- "\" has no value named \"" + id + "\".");
+ "Enum type \"" + enumType.getFullName()
+ + "\" has no value named \"" + id + "\".");
}
}
@@ -1578,8 +1580,9 @@ public final class TextFormat {
// start with "{" or "<" which indicates the beginning of a message body.
// If there is no ":" or there is a "{" or "<" after ":", this field has
// to be a message or the input is ill-formed.
- if (tokenizer.tryConsume(":") && !tokenizer.lookingAt("<") &&
- !tokenizer.lookingAt("{")) {
+ if (tokenizer.tryConsume(":")
+ && !tokenizer.lookingAt("<")
+ && !tokenizer.lookingAt("{")) {
skipFieldValue(tokenizer);
} else {
skipFieldMessage(tokenizer);
@@ -1617,11 +1620,11 @@ public final class TextFormat {
while (tokenizer.tryConsumeString()) {}
return;
}
- if (!tokenizer.tryConsumeIdentifier() && // includes enum & boolean
- !tokenizer.tryConsumeInt64() && // includes int32
- !tokenizer.tryConsumeUInt64() && // includes uint32
- !tokenizer.tryConsumeDouble() &&
- !tokenizer.tryConsumeFloat()) {
+ if (!tokenizer.tryConsumeIdentifier() // includes enum & boolean
+ && !tokenizer.tryConsumeInt64() // includes int32
+ && !tokenizer.tryConsumeUInt64() // includes uint32
+ && !tokenizer.tryConsumeDouble()
+ && !tokenizer.tryConsumeFloat()) {
throw tokenizer.parseException(
"Invalid field value: " + tokenizer.currentToken);
}
@@ -1647,19 +1650,19 @@ public final class TextFormat {
* which no defined short-hand escape sequence is defined will be escaped
* using 3-digit octal sequences.
*/
- private static String escapeBytes(final ByteSequence input) {
+ public static String escapeBytes(final ByteSequence input) {
final StringBuilder builder = new StringBuilder(input.size());
for (int i = 0; i < input.size(); i++) {
final byte b = input.byteAt(i);
switch (b) {
// Java does not recognize \a or \v, apparently.
- case 0x07: builder.append("\\a" ); break;
- case '\b': builder.append("\\b" ); break;
- case '\f': builder.append("\\f" ); break;
- case '\n': builder.append("\\n" ); break;
- case '\r': builder.append("\\r" ); break;
- case '\t': builder.append("\\t" ); break;
- case 0x0b: builder.append("\\v" ); break;
+ case 0x07: builder.append("\\a"); break;
+ case '\b': builder.append("\\b"); break;
+ case '\f': builder.append("\\f"); break;
+ case '\n': builder.append("\\n"); break;
+ case '\r': builder.append("\\r"); break;
+ case '\t': builder.append("\\t"); break;
+ case 0x0b: builder.append("\\v"); break;
case '\\': builder.append("\\\\"); break;
case '\'': builder.append("\\\'"); break;
case '"' : builder.append("\\\""); break;
@@ -1688,11 +1691,13 @@ public final class TextFormat {
* which no defined short-hand escape sequence is defined will be escaped
* using 3-digit octal sequences.
*/
- static String escapeBytes(final ByteString input) {
+ public static String escapeBytes(final ByteString input) {
return escapeBytes(new ByteSequence() {
+ @Override
public int size() {
return input.size();
}
+ @Override
public byte byteAt(int offset) {
return input.byteAt(offset);
}
@@ -1702,11 +1707,13 @@ public final class TextFormat {
/**
* Like {@link #escapeBytes(ByteString)}, but used for byte array.
*/
- static String escapeBytes(final byte[] input) {
+ public static String escapeBytes(final byte[] input) {
return escapeBytes(new ByteSequence() {
+ @Override
public int size() {
return input.length;
}
+ @Override
public byte byteAt(int offset) {
return input[offset];
}
@@ -1749,7 +1756,7 @@ public final class TextFormat {
code = code * 8 + digitValue(input.byteAt(i));
}
// TODO: Check that 0 <= code && code <= 0xFF.
- result[pos++] = (byte)code;
+ result[pos++] = (byte) code;
} else {
switch (c) {
case 'a' : result[pos++] = 0x07; break;
@@ -1777,12 +1784,12 @@ public final class TextFormat {
++i;
code = code * 16 + digitValue(input.byteAt(i));
}
- result[pos++] = (byte)code;
+ result[pos++] = (byte) code;
break;
default:
throw new InvalidEscapeSequenceException(
- "Invalid escape sequence: '\\" + (char)c + '\'');
+ "Invalid escape sequence: '\\" + (char) c + '\'');
}
}
} else {
@@ -1841,9 +1848,9 @@ public final class TextFormat {
/** Is this a hex digit? */
private static boolean isHex(final byte c) {
- return ('0' <= c && c <= '9') ||
- ('a' <= c && c <= 'f') ||
- ('A' <= c && c <= 'F');
+ return ('0' <= c && c <= '9')
+ || ('a' <= c && c <= 'f')
+ || ('A' <= c && c <= 'F');
}
/**
diff --git a/java/src/main/java/com/google/protobuf/WireFormat.java b/java/src/main/java/com/google/protobuf/WireFormat.java
index eba2528e..ba83b666 100644
--- a/java/src/main/java/com/google/protobuf/WireFormat.java
+++ b/java/src/main/java/com/google/protobuf/WireFormat.java
@@ -30,6 +30,8 @@
package com.google.protobuf;
+import java.io.IOException;
+
/**
* This class is used internally by the Protocol Buffer library and generated
* message implementations. It is public only because those generated messages
@@ -160,4 +162,84 @@ public final class WireFormat {
makeTag(MESSAGE_SET_TYPE_ID, WIRETYPE_VARINT);
static final int MESSAGE_SET_MESSAGE_TAG =
makeTag(MESSAGE_SET_MESSAGE, WIRETYPE_LENGTH_DELIMITED);
+
+ /**
+ * Validation level for handling incoming string field data which potentially
+ * contain non-UTF8 bytes.
+ */
+ enum Utf8Validation {
+ /** Eagerly parses to String; silently accepts invalid UTF8 bytes. */
+ LOOSE {
+ Object readString(CodedInputStream input) throws IOException {
+ return input.readString();
+ }
+ },
+ /** Eagerly parses to String; throws an IOException on invalid bytes. */
+ STRICT {
+ Object readString(CodedInputStream input) throws IOException {
+ return input.readStringRequireUtf8();
+ }
+ },
+ /** Keep data as ByteString; validation/conversion to String is lazy. */
+ LAZY {
+ Object readString(CodedInputStream input) throws IOException {
+ return input.readBytes();
+ }
+ };
+
+ /** Read a string field from the input with the proper UTF8 validation. */
+ abstract Object readString(CodedInputStream input) throws IOException;
+ }
+
+ /**
+ * Read a field of any primitive type for immutable messages from a
+ * CodedInputStream. Enums, groups, and embedded messages are not handled by
+ * this method.
+ *
+ * @param input The stream from which to read.
+ * @param type Declared type of the field.
+ * @param utf8Validation Different string UTF8 validation level for handling
+ * string fields.
+ * @return An object representing the field's value, of the exact
+ * type which would be returned by
+ * {@link Message#getField(Descriptors.FieldDescriptor)} for
+ * this field.
+ */
+ static Object readPrimitiveField(
+ CodedInputStream input,
+ FieldType type,
+ Utf8Validation utf8Validation) throws IOException {
+ switch (type) {
+ case DOUBLE : return input.readDouble ();
+ case FLOAT : return input.readFloat ();
+ case INT64 : return input.readInt64 ();
+ case UINT64 : return input.readUInt64 ();
+ case INT32 : return input.readInt32 ();
+ case FIXED64 : return input.readFixed64 ();
+ case FIXED32 : return input.readFixed32 ();
+ case BOOL : return input.readBool ();
+ case BYTES : return input.readBytes ();
+ case UINT32 : return input.readUInt32 ();
+ case SFIXED32: return input.readSFixed32();
+ case SFIXED64: return input.readSFixed64();
+ case SINT32 : return input.readSInt32 ();
+ case SINT64 : return input.readSInt64 ();
+
+ case STRING : return utf8Validation.readString(input);
+ case GROUP:
+ throw new IllegalArgumentException(
+ "readPrimitiveField() cannot handle nested groups.");
+ case MESSAGE:
+ throw new IllegalArgumentException(
+ "readPrimitiveField() cannot handle embedded messages.");
+ case ENUM:
+ // We don't handle enums because we don't know what to do if the
+ // value is not recognized.
+ throw new IllegalArgumentException(
+ "readPrimitiveField() cannot handle enums.");
+ }
+
+ throw new RuntimeException(
+ "There is no way to get here, but the compiler thinks otherwise.");
+ }
}
diff --git a/java/src/test/java/com/google/protobuf/GeneratedMessageTest.java b/java/src/test/java/com/google/protobuf/GeneratedMessageTest.java
index 41ed7bd0..2bd8d1a9 100644
--- a/java/src/test/java/com/google/protobuf/GeneratedMessageTest.java
+++ b/java/src/test/java/com/google/protobuf/GeneratedMessageTest.java
@@ -56,6 +56,7 @@ import protobuf_unittest.UnittestProto;
import protobuf_unittest.UnittestProto.ForeignEnum;
import protobuf_unittest.UnittestProto.ForeignMessage;
import protobuf_unittest.UnittestProto.ForeignMessageOrBuilder;
+import protobuf_unittest.UnittestProto.NestedTestAllTypes;
import protobuf_unittest.UnittestProto.TestAllExtensions;
import protobuf_unittest.UnittestProto.TestAllTypes;
import protobuf_unittest.UnittestProto.TestAllTypes.NestedMessage;
@@ -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() {
Descriptor descriptor = TestAllTypes.getDescriptor();
diff --git a/java/src/test/java/com/google/protobuf/LazyFieldLiteTest.java b/java/src/test/java/com/google/protobuf/LazyFieldLiteTest.java
index e67c6d27..211b5697 100644
--- a/java/src/test/java/com/google/protobuf/LazyFieldLiteTest.java
+++ b/java/src/test/java/com/google/protobuf/LazyFieldLiteTest.java
@@ -30,6 +30,9 @@
package com.google.protobuf;
+import static protobuf_unittest.UnittestProto.optionalInt32Extension;
+import static protobuf_unittest.UnittestProto.optionalInt64Extension;
+
import protobuf_unittest.UnittestProto.TestAllExtensions;
import protobuf_unittest.UnittestProto.TestAllTypes;
@@ -111,12 +114,146 @@ public class LazyFieldLiteTest extends TestCase {
assertNotEqual(message.toByteString(), lazyField.toByteString());
}
+ public void testMergeExtensions() throws Exception {
+ TestAllExtensions message = TestUtil.getAllExtensionsSet();
+ LazyFieldLite original = createLazyFieldLiteFromMessage(message);
+ LazyFieldLite merged = new LazyFieldLite();
+ merged.merge(original);
+ TestAllExtensions value = (TestAllExtensions) merged.getValue(
+ TestAllExtensions.getDefaultInstance());
+ assertEquals(message, value);
+ }
+
+ public void testEmptyLazyField() throws Exception {
+ LazyFieldLite field = new LazyFieldLite();
+ assertEquals(0, field.getSerializedSize());
+ assertEquals(ByteString.EMPTY, field.toByteString());
+ }
+
+ public void testInvalidProto() throws Exception {
+ // Silently fails and uses the default instance.
+ LazyFieldLite field = new LazyFieldLite(
+ TestUtil.getExtensionRegistry(), ByteString.copyFromUtf8("invalid"));
+ assertEquals(
+ TestAllTypes.getDefaultInstance(), field.getValue(TestAllTypes.getDefaultInstance()));
+ assertEquals(0, field.getSerializedSize());
+ assertEquals(ByteString.EMPTY, field.toByteString());
+ }
+
+ public void testMergeBeforeParsing() throws Exception {
+ TestAllTypes message1 = TestAllTypes.newBuilder().setOptionalInt32(1).build();
+ LazyFieldLite field1 = createLazyFieldLiteFromMessage(message1);
+ TestAllTypes message2 = TestAllTypes.newBuilder().setOptionalInt64(2).build();
+ LazyFieldLite field2 = createLazyFieldLiteFromMessage(message2);
+
+ field1.merge(field2);
+ TestAllTypes expected =
+ TestAllTypes.newBuilder().setOptionalInt32(1).setOptionalInt64(2).build();
+ assertEquals(expected, field1.getValue(TestAllTypes.getDefaultInstance()));
+ }
+
+ public void testMergeOneNotParsed() throws Exception {
+ // Test a few different paths that involve one message that was not parsed.
+ TestAllTypes message1 = TestAllTypes.newBuilder().setOptionalInt32(1).build();
+ TestAllTypes message2 = TestAllTypes.newBuilder().setOptionalInt64(2).build();
+ TestAllTypes expected =
+ TestAllTypes.newBuilder().setOptionalInt32(1).setOptionalInt64(2).build();
+
+ LazyFieldLite field1 = LazyFieldLite.fromValue(message1);
+ field1.getValue(TestAllTypes.getDefaultInstance()); // Force parsing.
+ LazyFieldLite field2 = createLazyFieldLiteFromMessage(message2);
+ field1.merge(field2);
+ assertEquals(expected, field1.getValue(TestAllTypes.getDefaultInstance()));
+
+ // Now reverse which one is parsed first.
+ field1 = LazyFieldLite.fromValue(message1);
+ field2 = createLazyFieldLiteFromMessage(message2);
+ field2.getValue(TestAllTypes.getDefaultInstance()); // Force parsing.
+ field1.merge(field2);
+ assertEquals(expected, field1.getValue(TestAllTypes.getDefaultInstance()));
+ }
+
+ public void testMergeInvalid() throws Exception {
+ // Test a few different paths that involve one message that was not parsed.
+ TestAllTypes message = TestAllTypes.newBuilder().setOptionalInt32(1).build();
+ LazyFieldLite valid = LazyFieldLite.fromValue(message);
+ LazyFieldLite invalid = new LazyFieldLite(
+ TestUtil.getExtensionRegistry(), ByteString.copyFromUtf8("invalid"));
+ invalid.merge(valid);
+
+ // We swallow the exception and just use the set field.
+ assertEquals(message, invalid.getValue(TestAllTypes.getDefaultInstance()));
+ }
+
+ public void testMergeKeepsExtensionsWhenPossible() throws Exception {
+ // In this test we attempt to only use the empty registry, which will strip out all extensions
+ // when serializing and then parsing. We verify that each code path will attempt to not
+ // serialize and parse a message that was set directly without going through the
+ // extensionRegistry.
+ TestAllExtensions messageWithExtensions =
+ TestAllExtensions.newBuilder().setExtension(optionalInt32Extension, 42).build();
+ TestAllExtensions emptyMessage = TestAllExtensions.newBuilder().build();
+
+ ExtensionRegistryLite emptyRegistry = ExtensionRegistryLite.getEmptyRegistry();
+
+ LazyFieldLite field = LazyFieldLite.fromValue(messageWithExtensions);
+ field.merge(createLazyFieldLiteFromMessage(emptyRegistry, emptyMessage));
+ assertEquals(messageWithExtensions, field.getValue(TestAllExtensions.getDefaultInstance()));
+
+ // Now reverse the order of the merging.
+ field = createLazyFieldLiteFromMessage(emptyRegistry, emptyMessage);
+ field.merge(LazyFieldLite.fromValue(messageWithExtensions));
+ assertEquals(messageWithExtensions, field.getValue(TestAllExtensions.getDefaultInstance()));
+
+ // Now try parsing the empty field first.
+ field = LazyFieldLite.fromValue(messageWithExtensions);
+ LazyFieldLite other = createLazyFieldLiteFromMessage(emptyRegistry, emptyMessage);
+ other.getValue(TestAllExtensions.getDefaultInstance()); // Force parsing.
+ field.merge(other);
+ assertEquals(messageWithExtensions, field.getValue(TestAllExtensions.getDefaultInstance()));
+
+ // And again reverse.
+ field = createLazyFieldLiteFromMessage(emptyRegistry, emptyMessage);
+ field.getValue(TestAllExtensions.getDefaultInstance()); // Force parsing.
+ other = LazyFieldLite.fromValue(messageWithExtensions);
+ field.merge(other);
+ assertEquals(messageWithExtensions, field.getValue(TestAllExtensions.getDefaultInstance()));
+ }
+
+ public void testMergeMightLoseExtensions() throws Exception {
+ // Test that we don't know about the extensions when parsing.
+ TestAllExtensions message1 =
+ TestAllExtensions.newBuilder().setExtension(optionalInt32Extension, 1).build();
+ TestAllExtensions message2 =
+ TestAllExtensions.newBuilder().setExtension(optionalInt64Extension, 2L).build();
+
+ LazyFieldLite field = LazyFieldLite.fromValue(message1);
+ field.merge(LazyFieldLite.fromValue(message2));
+
+ // We lose the extensions from message 2 because we have to serialize it and then parse it
+ // again, using the empty registry this time.
+ TestAllExtensions value =
+ (TestAllExtensions) field.getValue(TestAllExtensions.getDefaultInstance());
+ assertTrue(value.hasExtension(optionalInt32Extension));
+ assertEquals(Integer.valueOf(1), value.getExtension(optionalInt32Extension));
+ assertFalse(value.hasExtension(optionalInt64Extension));
+
+ // The field is still there, it is just unknown.
+ assertTrue(value.getUnknownFields()
+ .hasField(optionalInt64Extension.getDescriptor().getNumber()));
+ }
+
// Help methods.
private LazyFieldLite createLazyFieldLiteFromMessage(MessageLite message) {
+ return createLazyFieldLiteFromMessage(TestUtil.getExtensionRegistry(), message);
+ }
+
+ private LazyFieldLite createLazyFieldLiteFromMessage(
+ ExtensionRegistryLite extensionRegistry, MessageLite message) {
ByteString bytes = message.toByteString();
- return new LazyFieldLite(TestUtil.getExtensionRegistry(), bytes);
+ return new LazyFieldLite(extensionRegistry, bytes);
}
private void changeValue(LazyFieldLite lazyField) {
diff --git a/java/src/test/java/com/google/protobuf/LazyMessageLiteTest.java b/java/src/test/java/com/google/protobuf/LazyMessageLiteTest.java
index 9de794fe..afe0fffd 100644
--- a/java/src/test/java/com/google/protobuf/LazyMessageLiteTest.java
+++ b/java/src/test/java/com/google/protobuf/LazyMessageLiteTest.java
@@ -30,6 +30,7 @@
package com.google.protobuf;
+import protobuf_unittest.LazyFieldsLite.LazyExtension;
import protobuf_unittest.LazyFieldsLite.LazyInnerMessageLite;
import protobuf_unittest.LazyFieldsLite.LazyMessageLite;
import protobuf_unittest.LazyFieldsLite.LazyNestedInnerMessageLite;
@@ -285,4 +286,22 @@ public class LazyMessageLiteTest extends TestCase {
assertEquals(bytes, deserialized.toByteString());
}
+
+ public void testExtensions() throws Exception {
+ LazyInnerMessageLite.Builder innerBuilder = LazyInnerMessageLite.newBuilder();
+ innerBuilder.setExtension(
+ LazyExtension.extension, LazyExtension.newBuilder()
+ .setName("name").build());
+ assertTrue(innerBuilder.hasExtension(LazyExtension.extension));
+ assertEquals("name", innerBuilder.getExtension(LazyExtension.extension).getName());
+
+ LazyInnerMessageLite innerMessage = innerBuilder.build();
+ assertTrue(innerMessage.hasExtension(LazyExtension.extension));
+ assertEquals("name", innerMessage.getExtension(LazyExtension.extension).getName());
+
+ LazyMessageLite lite = LazyMessageLite.newBuilder()
+ .setInner(innerMessage).build();
+ assertTrue(lite.getInner().hasExtension(LazyExtension.extension));
+ assertEquals("name", lite.getInner().getExtension(LazyExtension.extension).getName());
+ }
}
diff --git a/java/src/test/java/com/google/protobuf/LiteralByteStringTest.java b/java/src/test/java/com/google/protobuf/LiteralByteStringTest.java
index ff39ca3f..046832de 100644
--- a/java/src/test/java/com/google/protobuf/LiteralByteStringTest.java
+++ b/java/src/test/java/com/google/protobuf/LiteralByteStringTest.java
@@ -289,7 +289,6 @@ public class LiteralByteStringTest extends TestCase {
assertEquals("Output.reset() resets the output", 0, output.size());
assertEquals("Output.reset() resets the output",
ByteString.EMPTY, output.toByteString());
-
}
public void testToString() throws UnsupportedEncodingException {
@@ -299,6 +298,27 @@ public class LiteralByteStringTest extends TestCase {
assertEquals(classUnderTest + " unicode must match", testString, roundTripString);
}
+ public void testToString_returnsCanonicalEmptyString() throws UnsupportedEncodingException{
+ assertSame(classUnderTest + " must be the same string references",
+ ByteString.EMPTY.toString(UTF_8), new LiteralByteString(new byte[]{}).toString(UTF_8));
+ }
+
+ public void testToString_raisesException() throws UnsupportedEncodingException{
+ try {
+ ByteString.EMPTY.toString("invalid");
+ fail("Should have thrown an exception.");
+ } catch (UnsupportedEncodingException expected) {
+ // This is success
+ }
+
+ try {
+ new LiteralByteString(referenceBytes).toString("invalid");
+ fail("Should have thrown an exception.");
+ } catch (UnsupportedEncodingException expected) {
+ // This is success
+ }
+ }
+
public void testEquals() {
assertEquals(classUnderTest + " must not equal null", false, stringUnderTest.equals(null));
assertEquals(classUnderTest + " must equal self", stringUnderTest, stringUnderTest);
@@ -311,7 +331,7 @@ public class LiteralByteStringTest extends TestCase {
byte[] mungedBytes = new byte[referenceBytes.length];
System.arraycopy(referenceBytes, 0, mungedBytes, 0, referenceBytes.length);
- mungedBytes[mungedBytes.length - 5] ^= 0xFF;
+ mungedBytes[mungedBytes.length - 5] = (byte) (mungedBytes[mungedBytes.length - 5] ^ 0xFF);
assertFalse(classUnderTest + " must not equal every string with the same length",
stringUnderTest.equals(new LiteralByteString(mungedBytes)));
}
diff --git a/java/src/test/java/com/google/protobuf/MapForProto2Test.java b/java/src/test/java/com/google/protobuf/MapForProto2Test.java
index 33ba7150..78cba1b4 100644
--- a/java/src/test/java/com/google/protobuf/MapForProto2Test.java
+++ b/java/src/test/java/com/google/protobuf/MapForProto2Test.java
@@ -34,6 +34,7 @@ import com.google.protobuf.Descriptors.FieldDescriptor;
import map_test.MapForProto2TestProto.TestMap;
import map_test.MapForProto2TestProto.TestMap.MessageValue;
import map_test.MapForProto2TestProto.TestMap.MessageWithRequiredFields;
+import map_test.MapForProto2TestProto.TestRecursiveMap;
import map_test.MapForProto2TestProto.TestUnknownEnumValue;
import junit.framework.TestCase;
@@ -499,4 +500,17 @@ public class MapForProto2Test extends TestCase {
message = builder.build();
assertTrue(message.isInitialized());
}
+
+ public void testRecursiveMap() throws Exception {
+ TestRecursiveMap.Builder builder = TestRecursiveMap.newBuilder();
+ builder.getMutableRecursiveMapField().put(
+ 1, TestRecursiveMap.newBuilder().setValue(2).build());
+ builder.getMutableRecursiveMapField().put(
+ 3, TestRecursiveMap.newBuilder().setValue(4).build());
+ ByteString data = builder.build().toByteString();
+
+ TestRecursiveMap message = TestRecursiveMap.parseFrom(data);
+ assertEquals(2, message.getRecursiveMapField().get(1).getValue());
+ assertEquals(4, message.getRecursiveMapField().get(3).getValue());
+ }
}
diff --git a/java/src/test/java/com/google/protobuf/MapTest.java b/java/src/test/java/com/google/protobuf/MapTest.java
index 9a25e302..b8e67b7c 100644
--- a/java/src/test/java/com/google/protobuf/MapTest.java
+++ b/java/src/test/java/com/google/protobuf/MapTest.java
@@ -57,22 +57,22 @@ public class MapTest extends TestCase {
builder.getMutableInt32ToStringField().put(1, "11");
builder.getMutableInt32ToStringField().put(2, "22");
builder.getMutableInt32ToStringField().put(3, "33");
-
+
builder.getMutableInt32ToBytesField().put(1, TestUtil.toBytes("11"));
builder.getMutableInt32ToBytesField().put(2, TestUtil.toBytes("22"));
builder.getMutableInt32ToBytesField().put(3, TestUtil.toBytes("33"));
-
+
builder.getMutableInt32ToEnumField().put(1, TestMap.EnumValue.FOO);
builder.getMutableInt32ToEnumField().put(2, TestMap.EnumValue.BAR);
builder.getMutableInt32ToEnumField().put(3, TestMap.EnumValue.BAZ);
-
+
builder.getMutableInt32ToMessageField().put(
1, MessageValue.newBuilder().setValue(11).build());
builder.getMutableInt32ToMessageField().put(
2, MessageValue.newBuilder().setValue(22).build());
builder.getMutableInt32ToMessageField().put(
3, MessageValue.newBuilder().setValue(33).build());
-
+
builder.getMutableStringToInt32Field().put("1", 11);
builder.getMutableStringToInt32Field().put("2", 22);
builder.getMutableStringToInt32Field().put("3", 33);
@@ -88,22 +88,22 @@ public class MapTest extends TestCase {
assertEquals("11", message.getInt32ToStringField().get(1));
assertEquals("22", message.getInt32ToStringField().get(2));
assertEquals("33", message.getInt32ToStringField().get(3));
-
+
assertEquals(3, message.getInt32ToBytesField().size());
assertEquals(TestUtil.toBytes("11"), message.getInt32ToBytesField().get(1));
assertEquals(TestUtil.toBytes("22"), message.getInt32ToBytesField().get(2));
assertEquals(TestUtil.toBytes("33"), message.getInt32ToBytesField().get(3));
-
+
assertEquals(3, message.getInt32ToEnumField().size());
assertEquals(TestMap.EnumValue.FOO, message.getInt32ToEnumField().get(1));
assertEquals(TestMap.EnumValue.BAR, message.getInt32ToEnumField().get(2));
assertEquals(TestMap.EnumValue.BAZ, message.getInt32ToEnumField().get(3));
-
+
assertEquals(3, message.getInt32ToMessageField().size());
assertEquals(11, message.getInt32ToMessageField().get(1).getValue());
assertEquals(22, message.getInt32ToMessageField().get(2).getValue());
assertEquals(33, message.getInt32ToMessageField().get(3).getValue());
-
+
assertEquals(3, message.getStringToInt32Field().size());
assertEquals(11, message.getStringToInt32Field().get("1").intValue());
assertEquals(22, message.getStringToInt32Field().get("2").intValue());
@@ -118,21 +118,21 @@ public class MapTest extends TestCase {
builder.getMutableInt32ToStringField().put(1, "111");
builder.getMutableInt32ToStringField().remove(2);
builder.getMutableInt32ToStringField().put(4, "44");
-
+
builder.getMutableInt32ToBytesField().put(1, TestUtil.toBytes("111"));
builder.getMutableInt32ToBytesField().remove(2);
builder.getMutableInt32ToBytesField().put(4, TestUtil.toBytes("44"));
-
+
builder.getMutableInt32ToEnumField().put(1, TestMap.EnumValue.BAR);
builder.getMutableInt32ToEnumField().remove(2);
builder.getMutableInt32ToEnumField().put(4, TestMap.EnumValue.QUX);
-
+
builder.getMutableInt32ToMessageField().put(
1, MessageValue.newBuilder().setValue(111).build());
builder.getMutableInt32ToMessageField().remove(2);
builder.getMutableInt32ToMessageField().put(
4, MessageValue.newBuilder().setValue(44).build());
-
+
builder.getMutableStringToInt32Field().put("1", 111);
builder.getMutableStringToInt32Field().remove("2");
builder.getMutableStringToInt32Field().put("4", 44);
@@ -148,22 +148,22 @@ public class MapTest extends TestCase {
assertEquals("111", message.getInt32ToStringField().get(1));
assertEquals("33", message.getInt32ToStringField().get(3));
assertEquals("44", message.getInt32ToStringField().get(4));
-
+
assertEquals(3, message.getInt32ToBytesField().size());
assertEquals(TestUtil.toBytes("111"), message.getInt32ToBytesField().get(1));
assertEquals(TestUtil.toBytes("33"), message.getInt32ToBytesField().get(3));
assertEquals(TestUtil.toBytes("44"), message.getInt32ToBytesField().get(4));
-
+
assertEquals(3, message.getInt32ToEnumField().size());
assertEquals(TestMap.EnumValue.BAR, message.getInt32ToEnumField().get(1));
assertEquals(TestMap.EnumValue.BAZ, message.getInt32ToEnumField().get(3));
assertEquals(TestMap.EnumValue.QUX, message.getInt32ToEnumField().get(4));
-
+
assertEquals(3, message.getInt32ToMessageField().size());
assertEquals(111, message.getInt32ToMessageField().get(1).getValue());
assertEquals(33, message.getInt32ToMessageField().get(3).getValue());
assertEquals(44, message.getInt32ToMessageField().get(4).getValue());
-
+
assertEquals(3, message.getStringToInt32Field().size());
assertEquals(111, message.getStringToInt32Field().get("1").intValue());
assertEquals(33, message.getStringToInt32Field().get("3").intValue());
@@ -183,17 +183,17 @@ public class MapTest extends TestCase {
TestMap.Builder builder = TestMap.newBuilder();
TestMap message = builder.build();
assertMapValuesCleared(message);
-
+
builder = message.toBuilder();
setMapValues(builder);
message = builder.build();
assertMapValuesSet(message);
-
+
builder = message.toBuilder();
updateMapValues(builder);
message = builder.build();
assertMapValuesUpdated(message);
-
+
builder = message.toBuilder();
builder.clear();
message = builder.build();
@@ -207,14 +207,14 @@ public class MapTest extends TestCase {
assertEquals(message.getSerializedSize(), message.toByteString().size());
message = TestMap.PARSER.parseFrom(message.toByteString());
assertMapValuesSet(message);
-
+
builder = message.toBuilder();
updateMapValues(builder);
message = builder.build();
assertEquals(message.getSerializedSize(), message.toByteString().size());
message = TestMap.PARSER.parseFrom(message.toByteString());
assertMapValuesUpdated(message);
-
+
builder = message.toBuilder();
builder.clear();
message = builder.build();
@@ -222,12 +222,12 @@ public class MapTest extends TestCase {
message = TestMap.PARSER.parseFrom(message.toByteString());
assertMapValuesCleared(message);
}
-
+
public void testMergeFrom() throws Exception {
TestMap.Builder builder = TestMap.newBuilder();
setMapValues(builder);
TestMap message = builder.build();
-
+
TestMap.Builder other = TestMap.newBuilder();
other.mergeFrom(message);
assertMapValuesSet(other.build());
@@ -236,7 +236,7 @@ public class MapTest extends TestCase {
public void testEqualsAndHashCode() throws Exception {
// Test that generated equals() and hashCode() will disregard the order
// of map entries when comparing/hashing map fields.
-
+
// We can't control the order of elements in a HashMap. The best we can do
// here is to add elements in different order.
TestMap.Builder b1 = TestMap.newBuilder();
@@ -244,23 +244,23 @@ public class MapTest extends TestCase {
b1.getMutableInt32ToInt32Field().put(3, 4);
b1.getMutableInt32ToInt32Field().put(5, 6);
TestMap m1 = b1.build();
-
+
TestMap.Builder b2 = TestMap.newBuilder();
b2.getMutableInt32ToInt32Field().put(5, 6);
b2.getMutableInt32ToInt32Field().put(1, 2);
b2.getMutableInt32ToInt32Field().put(3, 4);
TestMap m2 = b2.build();
-
+
assertEquals(m1, m2);
assertEquals(m1.hashCode(), m2.hashCode());
-
+
// Make sure we did compare map fields.
b2.getMutableInt32ToInt32Field().put(1, 0);
m2 = b2.build();
assertFalse(m1.equals(m2));
// Don't check m1.hashCode() != m2.hashCode() because it's not guaranteed
// to be different.
-
+
// Regression test for b/18549190: if a map is a subset of the other map,
// equals() should return false.
b2.getMutableInt32ToInt32Field().remove(1);
@@ -268,57 +268,95 @@ public class MapTest extends TestCase {
assertFalse(m1.equals(m2));
assertFalse(m2.equals(m1));
}
-
-
+
public void testNestedBuilderOnChangeEventPropagation() {
TestOnChangeEventPropagation.Builder parent =
TestOnChangeEventPropagation.newBuilder();
parent.getOptionalMessageBuilder().getMutableInt32ToInt32Field().put(1, 2);
TestOnChangeEventPropagation message = parent.build();
assertEquals(2, message.getOptionalMessage().getInt32ToInt32Field().get(1).intValue());
-
+
// Make a change using nested builder.
parent.getOptionalMessageBuilder().getMutableInt32ToInt32Field().put(1, 3);
-
+
// Should be able to observe the change.
message = parent.build();
assertEquals(3, message.getOptionalMessage().getInt32ToInt32Field().get(1).intValue());
-
+
// Make another change using mergeFrom()
TestMap.Builder other = TestMap.newBuilder();
other.getMutableInt32ToInt32Field().put(1, 4);
parent.getOptionalMessageBuilder().mergeFrom(other.build());
-
+
// Should be able to observe the change.
message = parent.build();
assertEquals(4, message.getOptionalMessage().getInt32ToInt32Field().get(1).intValue());
-
+
// Make yet another change by clearing the nested builder.
parent.getOptionalMessageBuilder().clear();
-
+
// Should be able to observe the change.
message = parent.build();
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.
-
+
private static FieldDescriptor f(String name) {
return TestMap.getDescriptor().findFieldByName(name);
}
-
+
private static Object getFieldValue(Message mapEntry, String name) {
FieldDescriptor field = mapEntry.getDescriptorForType().findFieldByName(name);
return mapEntry.getField(field);
}
-
+
private static Message.Builder setFieldValue(
Message.Builder mapEntry, String name, Object value) {
FieldDescriptor field = mapEntry.getDescriptorForType().findFieldByName(name);
mapEntry.setField(field, value);
return mapEntry;
}
-
+
private static void assertHasMapValues(Message message, String name, Map<?, ?> values) {
FieldDescriptor field = f(name);
for (Object entry : (List<?>) message.getField(field)) {
@@ -337,7 +375,7 @@ public class MapTest extends TestCase {
assertEquals(value, values.get(key));
}
}
-
+
private static <KeyType, ValueType>
Message newMapEntry(Message.Builder builder, String name, KeyType key, ValueType value) {
FieldDescriptor field = builder.getDescriptorForType().findFieldByName(name);
@@ -348,7 +386,7 @@ public class MapTest extends TestCase {
entryBuilder.setField(valueField, value);
return entryBuilder.build();
}
-
+
private static void setMapValues(Message.Builder builder, String name, Map<?, ?> values) {
List<Message> entryList = new ArrayList<Message>();
for (Map.Entry<?, ?> entry : values.entrySet()) {
@@ -357,7 +395,7 @@ public class MapTest extends TestCase {
FieldDescriptor field = builder.getDescriptorForType().findFieldByName(name);
builder.setField(field, entryList);
}
-
+
private static <KeyType, ValueType>
Map<KeyType, ValueType> mapForValues(
KeyType key1, ValueType value1, KeyType key2, ValueType value2) {
@@ -385,14 +423,14 @@ public class MapTest extends TestCase {
mapForValues(
11, MessageValue.newBuilder().setValue(22).build(),
33, MessageValue.newBuilder().setValue(44).build()));
-
+
// Test clearField()
builder.clearField(f("int32_to_int32_field"));
builder.clearField(f("int32_to_message_field"));
message = builder.build();
assertEquals(0, message.getInt32ToInt32Field().size());
assertEquals(0, message.getInt32ToMessageField().size());
-
+
// Test setField()
setMapValues(builder, "int32_to_int32_field",
mapForValues(11, 22, 33, 44));
@@ -405,7 +443,7 @@ public class MapTest extends TestCase {
assertEquals(44, message.getInt32ToInt32Field().get(33).intValue());
assertEquals(222, message.getInt32ToMessageField().get(111).getValue());
assertEquals(444, message.getInt32ToMessageField().get(333).getValue());
-
+
// Test addRepeatedField
builder.addRepeatedField(f("int32_to_int32_field"),
newMapEntry(builder, "int32_to_int32_field", 55, 66));
@@ -425,7 +463,7 @@ public class MapTest extends TestCase {
message = builder.build();
assertEquals(55, message.getInt32ToInt32Field().get(55).intValue());
assertEquals(555, message.getInt32ToMessageField().get(555).getValue());
-
+
// Test setRepeatedField
for (int i = 0; i < builder.getRepeatedFieldCount(f("int32_to_int32_field")); i++) {
Message mapEntry = (Message) builder.getRepeatedField(f("int32_to_int32_field"), i);
@@ -442,35 +480,35 @@ public class MapTest extends TestCase {
assertEquals(33, message.getInt32ToInt32Field().get(44).intValue());
assertEquals(55, message.getInt32ToInt32Field().get(55).intValue());
}
-
+
public void testTextFormat() throws Exception {
TestMap.Builder builder = TestMap.newBuilder();
setMapValues(builder);
TestMap message = builder.build();
-
+
String textData = TextFormat.printToString(message);
-
+
builder = TestMap.newBuilder();
TextFormat.merge(textData, builder);
message = builder.build();
-
+
assertMapValuesSet(message);
}
-
+
public void testDynamicMessage() throws Exception {
TestMap.Builder builder = TestMap.newBuilder();
setMapValues(builder);
TestMap message = builder.build();
-
+
Message dynamicDefaultInstance =
DynamicMessage.getDefaultInstance(TestMap.getDescriptor());
Message dynamicMessage = dynamicDefaultInstance
.newBuilderForType().mergeFrom(message.toByteString()).build();
-
+
assertEquals(message, dynamicMessage);
assertEquals(message.hashCode(), dynamicMessage.hashCode());
}
-
+
public void testReflectionEqualsAndHashCode() throws Exception {
// Test that generated equals() and hashCode() will disregard the order
// of map entries when comparing/hashing map fields.
@@ -479,22 +517,22 @@ public class MapTest extends TestCase {
Message dynamicDefaultInstance =
DynamicMessage.getDefaultInstance(TestMap.getDescriptor());
FieldDescriptor field = f("int32_to_int32_field");
-
+
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", 3, 4));
b1.addRepeatedField(field, newMapEntry(b1, "int32_to_int32_field", 5, 6));
Message m1 = b1.build();
-
+
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", 1, 2));
b2.addRepeatedField(field, newMapEntry(b2, "int32_to_int32_field", 3, 4));
Message m2 = b2.build();
-
+
assertEquals(m1, m2);
assertEquals(m1.hashCode(), m2.hashCode());
-
+
// Make sure we did compare map fields.
b2.setRepeatedField(field, 0, newMapEntry(b1, "int32_to_int32_field", 0, 0));
m2 = b2.build();
@@ -502,7 +540,7 @@ public class MapTest extends TestCase {
// Don't check m1.hashCode() != m2.hashCode() because it's not guaranteed
// to be different.
}
-
+
public void testUnknownEnumValues() throws Exception {
TestMap.Builder builder = TestMap.newBuilder();
builder.getMutableInt32ToEnumFieldValue().put(0, 0);
@@ -517,7 +555,7 @@ public class MapTest extends TestCase {
assertEquals(TestMap.EnumValue.UNRECOGNIZED,
message.getInt32ToEnumField().get(2));
assertEquals(1000, message.getInt32ToEnumFieldValue().get(2).intValue());
-
+
// Unknown enum values should be preserved after:
// 1. Serialization and parsing.
// 2. toBuild().
@@ -528,7 +566,7 @@ public class MapTest extends TestCase {
assertEquals(1000, builder.getInt32ToEnumFieldValue().get(2).intValue());
builder = TestMap.newBuilder().mergeFrom(message);
assertEquals(1000, builder.getInt32ToEnumFieldValue().get(2).intValue());
-
+
// hashCode()/equals() should take unknown enum values into account.
builder.getMutableInt32ToEnumFieldValue().put(2, 1001);
TestMap message2 = builder.build();
@@ -538,17 +576,17 @@ public class MapTest extends TestCase {
// should be the same.
assertTrue(message.getInt32ToEnumField().equals(message2.getInt32ToEnumField()));
}
-
+
public void testUnknownEnumValuesInReflectionApi() throws Exception {
Descriptor descriptor = TestMap.getDescriptor();
EnumDescriptor enumDescriptor = TestMap.EnumValue.getDescriptor();
FieldDescriptor field = descriptor.findFieldByName("int32_to_enum_field");
-
+
Map<Integer, Integer> data = new HashMap<Integer, Integer>();
data.put(0, 0);
data.put(1, 1);
data.put(2, 1000); // unknown value.
-
+
TestMap.Builder builder = TestMap.newBuilder();
for (Map.Entry<Integer, Integer> entry : data.entrySet()) {
builder.getMutableInt32ToEnumFieldValue().put(entry.getKey(), entry.getValue());
diff --git a/java/src/test/java/com/google/protobuf/RopeByteStringTest.java b/java/src/test/java/com/google/protobuf/RopeByteStringTest.java
index b3970196..0f2344d6 100644
--- a/java/src/test/java/com/google/protobuf/RopeByteStringTest.java
+++ b/java/src/test/java/com/google/protobuf/RopeByteStringTest.java
@@ -118,6 +118,34 @@ public class RopeByteStringTest extends LiteralByteStringTest {
flatString.hashCode(), unicode.hashCode());
}
+ @Override
+ public void testToString_returnsCanonicalEmptyString() throws UnsupportedEncodingException {
+ RopeByteString ropeByteString =
+ RopeByteString.newInstanceForTest(ByteString.EMPTY, ByteString.EMPTY);
+ assertSame(classUnderTest + " must be the same string references",
+ ByteString.EMPTY.toString(UTF_8), ropeByteString.toString(UTF_8));
+ }
+
+ public void testToString_raisesException() throws UnsupportedEncodingException{
+ try {
+ ByteString byteString =
+ RopeByteString.newInstanceForTest(ByteString.EMPTY, ByteString.EMPTY);
+ byteString.toString("invalid");
+ fail("Should have thrown an exception.");
+ } catch (UnsupportedEncodingException expected) {
+ // This is success
+ }
+
+ try {
+ ByteString byteString = RopeByteString.concatenate(ByteString.copyFromUtf8("foo"),
+ ByteString.copyFromUtf8("bar"));
+ byteString.toString("invalid");
+ fail("Should have thrown an exception.");
+ } catch (UnsupportedEncodingException expected) {
+ // This is success
+ }
+ }
+
public void testJavaSerialization() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(out);
diff --git a/java/src/test/java/com/google/protobuf/lazy_fields_lite.proto b/java/src/test/java/com/google/protobuf/lazy_fields_lite.proto
index 015dc267..5580f72d 100644
--- a/java/src/test/java/com/google/protobuf/lazy_fields_lite.proto
+++ b/java/src/test/java/com/google/protobuf/lazy_fields_lite.proto
@@ -54,6 +54,15 @@ message LazyInnerMessageLite {
optional int32 num = 1;
optional int32 num_with_default = 2 [default = 42];
optional LazyNestedInnerMessageLite nested = 3 [lazy = true];
+
+ extensions 1000 to max;
+}
+
+message LazyExtension {
+ extend LazyInnerMessageLite {
+ optional LazyExtension extension = 1000;
+ }
+ optional string name = 1;
}
message LazyNestedInnerMessageLite {
diff --git a/java/src/test/java/com/google/protobuf/map_for_proto2_lite_test.proto b/java/src/test/java/com/google/protobuf/map_for_proto2_lite_test.proto
index a1fe856c..d5418f28 100644
--- a/java/src/test/java/com/google/protobuf/map_for_proto2_lite_test.proto
+++ b/java/src/test/java/com/google/protobuf/map_for_proto2_lite_test.proto
@@ -63,6 +63,13 @@ message TestUnknownEnumValue {
// parsing behavior of TestMap regarding unknown enum values.
map<int32, int32> int32_to_int32_field = 4;
}
+
+// Test that the maps initialization code works correctly when the map field
+// references the containing message.
+message TestRecursiveMap {
+ optional int32 value = 1;
+ map<int32, TestRecursiveMap> recursive_map_field = 2;
+}
package map_for_proto2_lite_test;
option java_package = "map_lite_test";
option optimize_for = LITE_RUNTIME;
diff --git a/java/src/test/java/com/google/protobuf/map_for_proto2_test.proto b/java/src/test/java/com/google/protobuf/map_for_proto2_test.proto
index a0ec7ac5..a9be5166 100644
--- a/java/src/test/java/com/google/protobuf/map_for_proto2_test.proto
+++ b/java/src/test/java/com/google/protobuf/map_for_proto2_test.proto
@@ -65,3 +65,10 @@ message TestUnknownEnumValue {
// parsing behavior of TestMap regarding unknown enum values.
map<int32, int32> int32_to_int32_field = 4;
}
+
+// Test that the maps initialization code works correctly when the map field
+// references the containing message.
+message TestRecursiveMap {
+ optional int32 value = 1;
+ map<int32, TestRecursiveMap> recursive_map_field = 2;
+}
diff --git a/java/src/test/java/com/google/protobuf/map_test.proto b/java/src/test/java/com/google/protobuf/map_test.proto
index 105ee3f8..bf692c22 100644
--- a/java/src/test/java/com/google/protobuf/map_test.proto
+++ b/java/src/test/java/com/google/protobuf/map_test.proto
@@ -36,6 +36,7 @@ option java_package = "map_test";
option java_outer_classname = "MapTestProto";
option java_generate_equals_and_hash = true;
+
message TestMap {
message MessageValue {
optional int32 value = 1;
diff --git a/java/src/test/java/com/google/protobuf/test_bad_identifiers.proto b/java/src/test/java/com/google/protobuf/test_bad_identifiers.proto
index 67035fd5..dc082615 100644
--- a/java/src/test/java/com/google/protobuf/test_bad_identifiers.proto
+++ b/java/src/test/java/com/google/protobuf/test_bad_identifiers.proto
@@ -45,6 +45,7 @@ option java_package = "com.google.protobuf";
option java_outer_classname = "TestBadIdentifiersProto";
option java_generate_equals_and_hash = true;
+
message TestMessage {
optional string cached_size = 1;
optional string serialized_size = 2;