code: private <Message extends PMessage<Message, Field>, Field extends PField>
      int writeMessage(LittleEndianBinaryWriter out, Message message)
              throws IOException {
          int len = 0;
          if (message instanceof PUnion) {
              if (((PUnion) message).unionFieldIsSet()) {
                  PField field = ((PUnion) message).unionField();
                  len += writeFieldValue(out, field.getId(), field.getDescriptor(), message.get(field.getId()));
              }
          } else {
              for (PField field : message.descriptor()
                                            .getFields()) {
                  if (message.has(field.getId())) {
                      len += writeFieldValue(out, field.getId(), field.getDescriptor(), message.get(field.getId()));
                  }
              }
          }
          // write STOP field.
          return len + out.writeVarint(STOP);
      }
docs: --- MESSAGE ---