WriteMessageContext messageContext = writer.pushContext(fieldName, this, out);
marshaller.writeTo(writer, value);
if (value instanceof Message) {
UnknownFieldSet unknownFieldSet = ((Message) value).getUnknownFieldSet();
if (unknownFieldSet != null) {
// validate that none of the unknown fields are also declared by the known descriptor
for (Descriptors.FieldDescriptor fd : getFieldDescriptors()) {
if (unknownFieldSet.hasTag(WireFormat.makeTag(fd.getNumber(), fd.getLiteType().getWireType()))) {
throw new IOException("Field " + fd.getFullName() + " is a known field so it is illegal to be present in the unknown field set");
}
}
// write the unknown fields
unknownFieldSet.writeTo(messageContext.out);
}
}
UnknownFieldSet unknownFieldSet = value instanceof Message ? ((Message) value).getUnknownFieldSet() : null;
// validate that all the required fields were written either by the marshaller or by the UnknownFieldSet
for (Descriptors.FieldDescriptor fd : getFieldDescriptors()) {
if (fd.isRequired() && !messageContext.isFieldMarked(fd.getNumber())
&& (unknownFieldSet == null || !unknownFieldSet.hasTag(WireFormat.makeTag(fd.getNumber(), fd.getLiteType().getWireType())))) {
throw new IllegalStateException("Required field \"" + fd.getFullName()
+ "\" should have been written by a calling a suitable method of "
+ MessageMarshaller.ProtoStreamWriter.class.getName());
}
}