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