protected String buildWriteMethodBody() {
        resetStringBuilder();
        buildString("\n{\n");

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

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

        for (int i = 0; i < entries.length; i++) {
            FieldEntry e = entries[i];
            if (!e.isAvailable()) {
                buildString("  $1.writeNil();\n");
                continue;
            }
            DefaultFieldEntry de = (DefaultFieldEntry) e;
            boolean isPrivate = Modifier.isPrivate(de.getField().getModifiers());
            Class<?> type = de.getType();
            if (type.isPrimitive()) { // primitive types
                if (!isPrivate) {
                    buildString("  $1.%s(_$$_t.%s);\n", primitiveWriteName(type), de.getName());
                } else {
                    buildString(
                            "  %s.writePrivateField($1, _$$_t, %s.class, \"%s\", templates[%d]);\n",
                            DefaultBuildContext.class.getName(), de.getField().getDeclaringClass().getName(), de.getName(), i);
                }
            } else { // reference types
                if (!isPrivate) {
                    buildString("  if (_$$_t.%s == null) {\n", de.getName());
                } else {
                    buildString(
                            "  if (%s.readPrivateField(_$$_t, %s.class, \"%s\") == null) {\n",
                            DefaultBuildContext.class.getName(), de.getField().getDeclaringClass().getName(), de.getName());
                }
                if (de.isNotNullable()) {
                    buildString(
                            "    throw new %s(\"%s cannot be null by @NotNullable\");\n",
                            MessageTypeException.class.getName(), de.getName());
                } else {
                    buildString("    $1.writeNil();\n");
                }
                buildString("  } else {\n");
                if (!isPrivate) {
                    buildString("    templates[%d].write($1, _$$_t.%s);\n", i, de.getName());
                } else {
                    buildString(
                            "    %s.writePrivateField($1, _$$_t, %s.class, \"%s\", templates[%d]);\n",
                            DefaultBuildContext.class.getName(), de.getField().getDeclaringClass().getName(), de.getName(), i);
                }
                buildString("  }\n");
            }
        }

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