Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
P
protobuf
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
protobuf
Commits
345d49a5
Commit
345d49a5
authored
Oct 02, 2014
by
Feng Xiao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix a bug that causes DynamicMessage.setField() to not work for repeated
enum fields.
parent
61477681
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
35 additions
and
2 deletions
+35
-2
DynamicMessage.java
java/src/main/java/com/google/protobuf/DynamicMessage.java
+19
-2
DynamicMessageTest.java
...src/test/java/com/google/protobuf/DynamicMessageTest.java
+16
-0
No files found.
java/src/main/java/com/google/protobuf/DynamicMessage.java
View file @
345d49a5
...
...
@@ -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
)
{
verifyEnum
Typ
e
(
field
,
value
);
verifyEnum
Valu
e
(
field
,
value
);
}
OneofDescriptor
oneofDescriptor
=
field
.
getContainingOneof
();
if
(
oneofDescriptor
!=
null
)
{
...
...
@@ -573,7 +579,7 @@ public final class DynamicMessage extends AbstractMessage {
}
/** Verifies that the value is EnumValueDescriptor and matchs Enum Type. */
private
void
verify
EnumTyp
e
(
FieldDescriptor
field
,
Object
value
)
{
private
void
verify
SingleEnumValu
e
(
FieldDescriptor
field
,
Object
value
)
{
if
(
value
==
null
)
{
throw
new
NullPointerException
();
}
...
...
@@ -587,6 +593,17 @@ public final class DynamicMessage extends AbstractMessage {
}
}
/** Verifies the value for an enum field. */
private
void
verifyEnumValue
(
FieldDescriptor
field
,
Object
value
)
{
if
(
field
.
isRepeated
())
{
for
(
Object
item
:
(
List
)
value
)
{
verifySingleEnumValue
(
field
,
item
);
}
}
else
{
verifySingleEnumValue
(
field
,
value
);
}
}
private
void
ensureIsMutable
()
{
if
(
fields
.
isImmutable
())
{
fields
=
fields
.
clone
();
...
...
java/src/test/java/com/google/protobuf/DynamicMessageTest.java
View file @
345d49a5
...
...
@@ -30,6 +30,7 @@
package
com
.
google
.
protobuf
;
import
com.google.protobuf.Descriptors.EnumDescriptor
;
import
com.google.protobuf.Descriptors.FieldDescriptor
;
import
com.google.protobuf.Descriptors.OneofDescriptor
;
...
...
@@ -307,4 +308,19 @@ public class DynamicMessageTest extends TestCase {
message
=
builder
.
build
();
assertSame
(
null
,
message
.
getOneofFieldDescriptor
(
oneof
));
}
// Regression test for a bug that makes setField() not work for repeated
// enum fields.
public
void
testSettersForRepeatedEnumField
()
throws
Exception
{
DynamicMessage
.
Builder
builder
=
DynamicMessage
.
newBuilder
(
TestAllTypes
.
getDescriptor
());
FieldDescriptor
repeatedEnumField
=
TestAllTypes
.
getDescriptor
().
findFieldByName
(
"repeated_nested_enum"
);
EnumDescriptor
enumDescriptor
=
TestAllTypes
.
NestedEnum
.
getDescriptor
();
builder
.
setField
(
repeatedEnumField
,
enumDescriptor
.
getValues
());
DynamicMessage
message
=
builder
.
build
();
assertEquals
(
enumDescriptor
.
getValues
(),
message
.
getField
(
repeatedEnumField
));
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment