protected String toFormStringExpression(JParameter argument, Style classStyle)
      throws UnableToCompleteException {
    JType type = argument.getType();
    String expr = argument.getName();

    if (type.isPrimitive() != null) {
      return "\"\"+" + expr;
    }
    if (STRING_TYPE == type) {
      return expr;
    }
    if (type.isClass() != null && isOverlayArrayType(type.isClass())) {
      return "(new " + JSON_ARRAY_CLASS + "(" + expr + ")).toString()";
    }
    if (type.isClass() != null && OVERLAY_VALUE_TYPE.isAssignableFrom(type.isClass())) {
      return "(new " + JSON_OBJECT_CLASS + "(" + expr + ")).toString()";
    }
    if (type.getQualifiedBinaryName().startsWith("java.lang.")) {
      return String.format("(%s != null ? %s.toString() : null)", expr, expr);
    }

    Json jsonAnnotation = argument.getAnnotation(Json.class);
    final Style style = jsonAnnotation != null ? jsonAnnotation.style() : classStyle;

    return locator.encodeExpression(type, expr, style) + ".toString()";
  }
  private void writeSubresourceLocatorImpl(JMethod method) throws UnableToCompleteException {
    JClassType iface = method.getReturnType().isInterface();
    if (iface == null || !REST_SERVICE_TYPE.isAssignableFrom(iface)) {
      getLogger()
          .log(
              ERROR,
              "Invalid subresource locator method. Method must have return type of an interface that extends RestService: "
                  + method.getReadableDeclaration());
      throw new UnableToCompleteException();
    }

    Path pathAnnotation = method.getAnnotation(Path.class);
    if (pathAnnotation == null) {
      getLogger()
          .log(
              ERROR,
              "Invalid subresource locator method. Method must have @Path annotation: "
                  + method.getReadableDeclaration());
      throw new UnableToCompleteException();
    }
    String pathExpression = wrap(pathAnnotation.value());

    for (JParameter arg : method.getParameters()) {
      PathParam paramPath = arg.getAnnotation(PathParam.class);
      if (paramPath != null) {
        pathExpression = pathExpression(pathExpression, arg, paramPath);
      }
    }

    p(method.getReadableDeclaration(false, false, false, false, true) + " {").i(1);
    {
      JType type = method.getReturnType();
      String name;
      if (type instanceof JClassType) {
        JClassType restService = (JClassType) type;
        RestServiceClassCreator generator =
            new RestServiceClassCreator(getLogger(), context, restService);
        name = generator.create();
      } else {
        throw new UnsupportedOperationException("Subresource method may not return: " + type);
      }
      p(method.getReturnType().getQualifiedSourceName() + " __subresource = new " + name + "();");
      p(
          "(("
              + RestServiceProxy.class.getName()
              + ")__subresource).setResource(getResource().resolve("
              + pathExpression
              + "));");
      p("return __subresource;");
    }
    i(-1).p("}");
  }
  protected String toStringExpression(JType type, String expr) {
    if (type.isPrimitive() != null) {
      return "\"\"+" + expr;
    }
    if (STRING_TYPE == type) {
      return expr;
    }
    if (type.isClass() != null && isOverlayArrayType(type.isClass())) {
      return "(new " + JSON_ARRAY_CLASS + "(" + expr + ")).toString()";
    }
    if (type.isClass() != null && OVERLAY_VALUE_TYPE.isAssignableFrom(type.isClass())) {
      return "(new " + JSON_OBJECT_CLASS + "(" + expr + ")).toString()";
    }

    return String.format("(%s != null ? %s.toString() : null)", expr, expr);
  }
  @Override
  protected void generate() throws UnableToCompleteException {

    if (source.isInterface() == null) {
      getLogger().log(ERROR, "Type is not an interface.");
      throw new UnableToCompleteException();
    }

    locator = new JsonEncoderDecoderInstanceLocator(context, getLogger());

    this.XML_CALLBACK_TYPE = find(XmlCallback.class, getLogger(), context);
    this.METHOD_CALLBACK_TYPE = find(MethodCallback.class, getLogger(), context);
    this.TEXT_CALLBACK_TYPE = find(TextCallback.class, getLogger(), context);
    this.JSON_CALLBACK_TYPE = find(JsonCallback.class, getLogger(), context);
    this.OVERLAY_CALLBACK_TYPE = find(OverlayCallback.class, getLogger(), context);
    this.DOCUMENT_TYPE = find(Document.class, getLogger(), context);
    this.METHOD_TYPE = find(Method.class, getLogger(), context);
    this.STRING_TYPE = find(String.class, getLogger(), context);
    this.JSON_VALUE_TYPE = find(JSONValue.class, getLogger(), context);
    this.OVERLAY_VALUE_TYPE = find(JavaScriptObject.class, getLogger(), context);
    this.OVERLAY_ARRAY_TYPES = new HashSet<JClassType>();
    this.OVERLAY_ARRAY_TYPES.add(find(JsArray.class, getLogger(), context));
    this.OVERLAY_ARRAY_TYPES.add(find(JsArrayBoolean.class, getLogger(), context));
    this.OVERLAY_ARRAY_TYPES.add(find(JsArrayInteger.class, getLogger(), context));
    this.OVERLAY_ARRAY_TYPES.add(find(JsArrayNumber.class, getLogger(), context));
    this.OVERLAY_ARRAY_TYPES.add(find(JsArrayString.class, getLogger(), context));
    this.QUERY_PARAM_LIST_TYPES = new HashSet<JClassType>();
    this.QUERY_PARAM_LIST_TYPES.add(find(List.class, getLogger(), context));
    this.QUERY_PARAM_LIST_TYPES.add(find(Set.class, getLogger(), context));
    this.REST_SERVICE_TYPE = find(RestService.class, getLogger(), context);

    String path = null;
    Path pathAnnotation = source.getAnnotation(Path.class);
    if (pathAnnotation != null) {
      path = pathAnnotation.value();
    }

    RemoteServiceRelativePath relativePath = source.getAnnotation(RemoteServiceRelativePath.class);
    if (relativePath != null) {
      path = relativePath.value();
    }

    p("private " + RESOURCE_CLASS + " resource = null;");
    p();

    p("public void setResource(" + RESOURCE_CLASS + " resource) {").i(1);
    {
      p("this.resource = resource;");
    }
    i(-1).p("}");

    p("public " + RESOURCE_CLASS + " getResource() {").i(1);
    {
      p("if (this.resource == null) {").i(1);
      if (path == null) {
        p("this.resource = new " + RESOURCE_CLASS + "(" + DEFAULTS_CLASS + ".getServiceRoot());");
      } else {
        p(
            "this.resource = new "
                + RESOURCE_CLASS
                + "("
                + DEFAULTS_CLASS
                + ".getServiceRoot()).resolve("
                + quote(path)
                + ");");
      }
      i(-1).p("}");
      p("return this.resource;");
    }
    i(-1).p("}");

    Options options = source.getAnnotation(Options.class);
    if (options != null && options.dispatcher() != Dispatcher.class) {
      p(
          "private "
              + DISPATCHER_CLASS
              + " dispatcher = "
              + options.dispatcher().getName()
              + ".INSTANCE;");
    } else {
      p("private " + DISPATCHER_CLASS + " dispatcher = null;");
    }

    p();
    p("public void setDispatcher(" + DISPATCHER_CLASS + " dispatcher) {").i(1);
    {
      p("this.dispatcher = dispatcher;");
    }
    i(-1).p("}");

    p();
    p("public " + DISPATCHER_CLASS + " getDispatcher() {").i(1);
    {
      p("return this.dispatcher;");
    }
    i(-1).p("}");

    for (JMethod method : source.getInheritableMethods()) {
      JClassType iface = method.getReturnType().isInterface();
      if (iface != null && REST_SERVICE_TYPE.isAssignableFrom(iface))
        writeSubresourceLocatorImpl(method);
      else writeMethodImpl(method);
    }
  }