Commit 20042b72 authored by Jisi Liu's avatar Jisi Liu

Fix Java maps reflection to call onChange to populate changes to parent

builders.

Change-Id: Ibf6ae3c0fe6bc31f74b8018c81a5af461b1c24ea
parent 21fb217e
......@@ -2171,10 +2171,12 @@ public abstract class GeneratedMessage extends AbstractMessage
}
public void setRepeated(Builder builder, int index, Object value) {
builder.onChanged();
getMapField(builder).getMutableList().set(index, (Message) value);
}
public void addRepeated(Builder builder, Object value) {
builder.onChanged();
getMapField(builder).getMutableList().add((Message) value);
}
......@@ -2197,6 +2199,7 @@ public abstract class GeneratedMessage extends AbstractMessage
}
public void clear(Builder builder) {
builder.onChanged();
getMapField(builder).getMutableList().clear();
}
......@@ -2395,7 +2398,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 =
......
......@@ -301,6 +301,45 @@ public class MapTest extends TestCase {
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) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment