aboutsummaryrefslogtreecommitdiff
path: root/java/src/main
diff options
context:
space:
mode:
authorFeng Xiao <xfxyjwf@gmail.com>2014-10-03 11:24:42 -0700
committerFeng Xiao <xfxyjwf@gmail.com>2014-10-03 11:24:42 -0700
commit56aa52d98657a9f093e85d2d2e15c87e5e50c967 (patch)
tree904d0aae3dc5fb2bd11328cb476b0ffe9543d2ba /java/src/main
parent84731a111f45e5d11d0d11fe4b53c1818d936df1 (diff)
parent725326f1eef7549349f6712322c8e073a9dd7fe9 (diff)
downloadprotobuf-56aa52d98657a9f093e85d2d2e15c87e5e50c967.tar.gz
protobuf-56aa52d98657a9f093e85d2d2e15c87e5e50c967.tar.bz2
protobuf-56aa52d98657a9f093e85d2d2e15c87e5e50c967.zip
Merge pull request #38 from xfxyjwf/fix17
Fix a bug that causes DynamicMessage.setField() to not work for repeated enum fields.
Diffstat (limited to 'java/src/main')
-rw-r--r--java/src/main/java/com/google/protobuf/DynamicMessage.java25
1 files changed, 22 insertions, 3 deletions
diff --git a/java/src/main/java/com/google/protobuf/DynamicMessage.java b/java/src/main/java/com/google/protobuf/DynamicMessage.java
index 6bba8eb8..c9ce667b 100644
--- a/java/src/main/java/com/google/protobuf/DynamicMessage.java
+++ b/java/src/main/java/com/google/protobuf/DynamicMessage.java
@@ -38,6 +38,7 @@ import com.google.protobuf.Descriptors.OneofDescriptor;
import java.io.InputStream;
import java.io.IOException;
import java.util.Collections;
+import java.util.List;
import java.util.Map;
/**
@@ -483,8 +484,13 @@ public final class DynamicMessage extends AbstractMessage {
public Builder setField(FieldDescriptor field, Object value) {
verifyContainingType(field);
ensureIsMutable();
+ // TODO(xiaofeng): This check should really be put in FieldSet.setField()
+ // where all other such checks are done. However, currently
+ // FieldSet.setField() permits Integer value for enum fields probably
+ // because of some internal features we support. Should figure it out
+ // and move this check to a more appropriate place.
if (field.getType() == FieldDescriptor.Type.ENUM) {
- verifyEnumType(field, value);
+ ensureEnumValueDescriptor(field, value);
}
OneofDescriptor oneofDescriptor = field.getContainingOneof();
if (oneofDescriptor != null) {
@@ -572,8 +578,9 @@ public final class DynamicMessage extends AbstractMessage {
}
}
- /** Verifies that the value is EnumValueDescriptor and matchs Enum Type. */
- private void verifyEnumType(FieldDescriptor field, Object value) {
+ /** Verifies that the value is EnumValueDescriptor and matches Enum Type. */
+ private void ensureSingularEnumValueDescriptor(
+ FieldDescriptor field, Object value) {
if (value == null) {
throw new NullPointerException();
}
@@ -587,6 +594,18 @@ public final class DynamicMessage extends AbstractMessage {
}
}
+ /** Verifies the value for an enum field. */
+ private void ensureEnumValueDescriptor(
+ FieldDescriptor field, Object value) {
+ if (field.isRepeated()) {
+ for (Object item : (List) value) {
+ ensureSingularEnumValueDescriptor(field, item);
+ }
+ } else {
+ ensureSingularEnumValueDescriptor(field, value);
+ }
+ }
+
private void ensureIsMutable() {
if (fields.isImmutable()) {
fields = fields.clone();