示例#1
0
  /** {@inheritDoc} */
  public void print(AST node, NodeWriter out) throws IOException {
    printBlankLinesBefore((JavaNode) node, out);

    for (AST child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
      PrinterFactory.create(child, out).print(child, out);
    }

    switch (out.last) {
      case JavaTokenTypes.CLASS_DEF:
        switch (((JavaNode) node).getParent().getType()) {
            // anonymous inner class is a VARIABLE_DEF assignment, let
            // VariableDeclarationPrinter.java handle the masquerading
          case JavaTokenTypes.ASSIGN:
            break;

            // anonymous inner class is a return expression
          case JavaTokenTypes.LITERAL_return:
            break;

            // anonymous inner class is a METHOD_CALL param expression
          case JavaTokenTypes.SLIST:
          case JavaTokenTypes.ELIST:
            break;

            // anonymous inner class is local assignment
          default:
            out.last = JavaTokenTypes.RCURLY;
            out.printNewline();

            break;
        }

        break;
    }
  }
示例#2
0
  /** {@inheritDoc} */
  public void print(AST node, NodeWriter out) throws IOException {
    printCommentsBefore(node, out);

    int offset = out.print(node.getText(), node.getType());

    trackPosition((JavaNode) node, out.line, offset, out);

    if (node.getFirstChild().getType() != JavaTokenTypes.SEMI) {
      out.print(SPACE, JavaTokenTypes.WS);
    }

    for (AST child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
      PrinterFactory.create(child, out).print(child, out);
    }
  }
  /** {@inheritDoc} */
  public void print(AST node, NodeWriter out) throws IOException {
    AST modifier = node.getFirstChild();

    if ((AbstractPrinter.settings.getBoolean(
                ConventionKeys.INSERT_FINAL_MODIFIER_FOR_METHOD_PARAMETERS,
                ConventionDefaults.INSERT_FINAL_MODIFIER_FOR_METHOD_PARAMETERS)
            && ((JavaNode) node).getParent().getParent().getType() == JavaTokenTypes.METHOD_DEF)
        || AbstractPrinter.settings.getBoolean(
            ConventionKeys.INSERT_FINAL_MODIFIER_FOR_PARAMETERS,
            ConventionDefaults.INSERT_FINAL_MODIFIER_FOR_PARAMETERS)) {
      boolean finalAlreadyExists = false;
      for (AST child = modifier.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child.getType() == JavaTokenTypes.FINAL) {
          finalAlreadyExists = true;
          break;
        }
      }
      if (!finalAlreadyExists) {
        AST finalModifier = out.getJavaNodeFactory().create(JavaTokenTypes.FINAL, "final");
        modifier.addChild(finalModifier);
      }
    }
    PrinterFactory.create(modifier, out).print(modifier, out);

    AST type = modifier.getNextSibling();
    PrinterFactory.create(type, out).print(type, out);

    // align the parameter
    if ((out.state.paramOffset != ParametersPrinter.OFFSET_NONE)
        && (out.column < out.state.paramOffset)) {
      out.print(out.getString(out.state.paramOffset - out.column), JavaTokenTypes.WS);
    } else {
      out.print(SPACE, JavaTokenTypes.WS);
    }

    AST identifier = type.getNextSibling();
    PrinterFactory.create(identifier, out).print(identifier, out);
  }
示例#4
0
  /** {@inheritDoc} */
  public void print(AST node, NodeWriter out) throws IOException {
    printCommentsBefore(node, out);

    // print space between else and if:
    // if { ... } else if { ... }
    switch (out.last) {
      case JavaTokenTypes.LITERAL_else:
        out.print(SPACE, JavaTokenTypes.LITERAL_if);

        break;
    }

    boolean spaceBefore =
        AbstractPrinter.settings.getBoolean(
            ConventionKeys.SPACE_BEFORE_STATEMENT_PAREN,
            ConventionDefaults.SPACE_BEFORE_STATEMENT_PAREN);

    int offset = 1;

    if (spaceBefore) {
      offset = out.print(IF_SPACE, JavaTokenTypes.LITERAL_if);
    } else {
      offset = out.print(IF, JavaTokenTypes.LITERAL_if);
    }

    trackPosition((JavaNode) node, out.line, offset, out);

    AST lparen = node.getFirstChild();

    boolean insertBraces =
        AbstractPrinter.settings.getBoolean(
            ConventionKeys.BRACE_INSERT_IF_ELSE, ConventionDefaults.BRACE_INSERT_IF_ELSE);

    JavaNode rparen = printExpressionList(lparen, insertBraces, out);
    AST body = rparen.getNextSibling();

    boolean leftBraceNewline =
        AbstractPrinter.settings.getBoolean(
            ConventionKeys.BRACE_NEWLINE_LEFT, ConventionDefaults.BRACE_NEWLINE_LEFT);
    boolean rightBraceNewline =
        AbstractPrinter.settings.getBoolean(
            ConventionKeys.BRACE_NEWLINE_RIGHT, ConventionDefaults.BRACE_NEWLINE_RIGHT);
    boolean hasBraces = body.getType() == JavaTokenTypes.SLIST;

    out.last = JavaTokenTypes.LITERAL_if;

    JavaNode next = (JavaNode) body.getNextSibling();

    if (hasBraces) {
      PrinterFactory.create(body, out).print(body, out);
    } else // no braces, single statement
    {
      if (insertBraces) // insert braces manually
      {
        if (out.pendingComment == null) {
          out.printLeftBrace(
              leftBraceNewline && !out.newline, NodeWriter.NEWLINE_YES, NodeWriter.NEWLINE_YES);
        } else {
          out.printLeftBrace(NodeWriter.NEWLINE_NO, NodeWriter.NEWLINE_NO);
          rparen.setHiddenAfter(out.pendingComment);
          printCommentsAfter(rparen, NodeWriter.NEWLINE_NO, NodeWriter.NEWLINE_YES, out);
          out.pendingComment = null;
        }

        PrinterFactory.create(body, out).print(body, out);
        out.printRightBrace(rightBraceNewline || (next == null));
      } else {
        if (!out.newline) out.printNewline();

        out.indent();
        PrinterFactory.create(body, out).print(body, out);
        out.unindent();
      }
    }

    // print the else-if or else part
    if (next != null) {
      printCommentsBefore(next, out);

      if (!out.newline && (out.last == JavaTokenTypes.RCURLY)) {
        out.print(
            out.getString(
                AbstractPrinter.settings.getInt(
                    ConventionKeys.INDENT_SIZE_BRACE_RIGHT_AFTER,
                    ConventionDefaults.INDENT_SIZE_BRACE_RIGHT_AFTER)),
            JavaTokenTypes.WS);
      }

      offset = out.print(ELSE, JavaTokenTypes.LITERAL_else);

      trackPosition(next, out.line, offset, out);

      JavaNode block = (JavaNode) next.getNextSibling();

      switch (block.getType()) {
        case JavaTokenTypes.SLIST: // block
          // print the endline comment for the else
          printCommentsAfter(next, NodeWriter.NEWLINE_NO, !leftBraceNewline, out);
          // either we print a LITERAL_if or LITERAL_else but
          // we don't care as both will lead to the same result
          out.last = JavaTokenTypes.LITERAL_if;
          PrinterFactory.create(block, out).print(block, out);

          break;

        case JavaTokenTypes.LITERAL_if: // the else-if 'if'
          out.last = JavaTokenTypes.LITERAL_else;
          print(block, out);

          break;

        default: // single expression
          if (insertBraces) {
            out.pendingComment = next.getCommentAfter();

            if (out.pendingComment == null) {
              printCommentsAfter(next, NodeWriter.NEWLINE_NO, !leftBraceNewline, out);
              out.printLeftBrace(
                  rightBraceNewline, NodeWriter.NEWLINE_NO, !rightBraceNewline && !out.newline);
              out.printNewline();
            } else {
              out.printLeftBrace(NodeWriter.NEWLINE_NO, NodeWriter.NEWLINE_NO);
              printCommentsAfter(next, NodeWriter.NEWLINE_NO, NodeWriter.NEWLINE_YES, out);
            }

            PrinterFactory.create(block, out).print(block, out);
            out.printRightBrace();
          } else {
            printCommentsAfter(next, NodeWriter.NEWLINE_NO, NodeWriter.NEWLINE_NO, out);
            out.printNewline();
            out.indent();
            PrinterFactory.create(block, out).print(block, out);
            out.unindent();
          }
      }
    }

    // do as if always braces printed for the correct blank lines behaviour
    out.last = JavaTokenTypes.RCURLY;
  }