@Override
    protected String buildWriteMethodBody() {
        resetStringBuilder();
        buildString("{");

        buildString("if($2 == null) {");
        buildString("  if($3) {");
        buildString("    throw new %s(\"Attempted to write null\");", MessageTypeException.class.getName());
        buildString("  }");
        buildString("  $1.writeNil();");
        buildString("  return;");
        buildString("}");

        buildString("%s _$$_t = (%s)$2;", origName, origName);
        buildString("$1.writeArrayBegin(%d);", entries.length);

        for (int i = 0; i < entries.length; i++) {
            BeansFieldEntry e = entries[i];
            if (!e.isAvailable()) {
                buildString("$1.writeNil();");
                continue;
            }
            Class<?> type = e.getType();
            if (type.isPrimitive()) {
                buildString("$1.%s(_$$_t.%s());", primitiveWriteName(type), e.getGetterName());
            } else {
                buildString("if(_$$_t.%s() == null) {", e.getGetterName());
                if (e.isNotNullable()) {
                    buildString("throw new %s();", MessageTypeException.class.getName());
                } else {
                    buildString("$1.writeNil();");
                }
                buildString("} else {");
                buildString("  this.templates[%d].write($1, _$$_t.%s());", i, e.getGetterName());
                buildString("}");
            }
        }

        buildString("$1.writeArrayEnd();");
        buildString("}");
        return getBuiltString();
    }