avoid repeated calls to build() during deserialization by obtaining field types from the Message's defaultInstance (#1215)
This commit is contained in:
parent
3bf1967c0a
commit
c744ccd51c
@ -263,6 +263,9 @@ public class ProtoTypeAdapter
|
|||||||
Message.Builder protoBuilder =
|
Message.Builder protoBuilder =
|
||||||
(Message.Builder) getCachedMethod(protoClass, "newBuilder").invoke(null);
|
(Message.Builder) getCachedMethod(protoClass, "newBuilder").invoke(null);
|
||||||
|
|
||||||
|
Message defaultInstance =
|
||||||
|
(Message) getCachedMethod(protoClass, "getDefaultInstance").invoke(null);
|
||||||
|
|
||||||
Descriptor protoDescriptor =
|
Descriptor protoDescriptor =
|
||||||
(Descriptor) getCachedMethod(protoClass, "getDescriptor").invoke(null);
|
(Descriptor) getCachedMethod(protoClass, "getDescriptor").invoke(null);
|
||||||
// Call setters on all of the available fields
|
// Call setters on all of the available fields
|
||||||
@ -300,8 +303,7 @@ public class ProtoTypeAdapter
|
|||||||
fieldValue = context.deserialize(jsonElement, protoArrayFieldType);
|
fieldValue = context.deserialize(jsonElement, protoArrayFieldType);
|
||||||
protoBuilder.setField(fieldDescriptor, fieldValue);
|
protoBuilder.setField(fieldDescriptor, fieldValue);
|
||||||
} else {
|
} else {
|
||||||
Message prototype = protoBuilder.build();
|
Object field = defaultInstance.getField(fieldDescriptor);
|
||||||
Object field = prototype.getField(fieldDescriptor);
|
|
||||||
fieldValue = context.deserialize(jsonElement, field.getClass());
|
fieldValue = context.deserialize(jsonElement, field.getClass());
|
||||||
protoBuilder.setField(fieldDescriptor, fieldValue);
|
protoBuilder.setField(fieldDescriptor, fieldValue);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user