TextBlockGoogleStyleFormatting

Since Checkstyle 12.3.0

Description

Checks correct format of Java Text Blocks as specified in Google Java Style Guide.
This Check performs two validations:
  1. It ensures that the opening and closing text-block quotes (""") each appear on their own line, with no other item preceding them.
  2. Opening and closing quotes are vertically aligned.
  3. Each line of text in the text block must be indented at least as much as the opening and closing quotes.
Note: Closing quotes can be followed by additional code on the same line.

Examples

To configure the check:


<module name="Checker">
  <module name="TreeWalker">
    <module name="TextBlockGoogleStyleFormatting"/>
  </module>
</module>

Example of correct cases:


public class Example1 {

  public String testMethod1() {

    final String simpleScript =
"""
        contents of Text block
""";

    final String simpleScript1 =
        """
        contents of Text block
        """;

    String stringFollowedBy =
        """
          Hello there
        """ + getName();

    getData(
        """
        Hello,
        This is a multi-line message.
        """, 0);

    return
        """
        this is sample text
        """;
  }

  public void getData(String text, int num) {}

  public String getName() {
    return null;
  }
}

Example: Opening quotes are not on new line:


public class Example2 {

  public String testMethod1() {
    // violation below 'Opening quotes (""") of text-block must be on the new line'
    final String simpleScript1Violate = """
                                         contents of the text block
      """; // violation 'Text-block quotes are not vertically aligned'
    final String simpleScript1Correct =
        """
        contents of the text block
        """;

    final String simpleScript2Violate = simpleScript1Violate +
        // violation below 'quotes (""") of text-block must be on the new line'
        simpleScript1Violate + """
                                this is simple script
        """; // violation 'Text-block quotes are not vertically aligned'
    final String simpleScript2Correct = simpleScript1Violate +
        simpleScript1Violate +
            """
            contents of the text block
            """;

    getData(
        // violation below 'quotes (""") of text-block must be on the new line'
        1, """
            this is a multi-line message
           """);
    getData(
        1,
           """
           this is a multi-line message
           """);

    // violation below 'Opening quotes (""") of text-block must be on the new line'
    return """
            this is sample text
        """; // violation 'Text-block quotes are not vertically aligned'
  }
  public void getData(String text, int num) {}
  public void getData(int num, String text) {}
}

Example: Closing quotes are not on new line


public class Example3 {
  public String testMethod1() {
    final String simpleScript1Violate =
            """
            this is simple test""";
    // 2 violations above:
    //   'Closing quotes (""") of text-block must be on the new line.'
    //   'Text-block quotes are not vertically aligned'
    final String simpleScript1Correct =
            """
            this is simple test
            """;

    String simpleScript2Violate = simpleScript1Correct
        +
        """
        very good""".charAt(0);
    // 2 violations above:
    //   'Closing quotes (""") of text-block must be on the new line.'
    //   'Text-block quotes are not vertically aligned'
    String simpleScript2Correct = simpleScript1Correct
        +
        """
        very good
        """.charAt(0);

    getData(
        """
        This is a multi-line message.""");
    // 2 violations above:
    //   'Closing quotes (""") of text-block must be on the new line.'
    //   'Text-block quotes are not vertically aligned'
    getData(
        """
        This is a multi-line message.
        """);

    return
        """
        THE MULTI-LINE MESSAGE""";
    // 2 violations above:
    //   'Closing quotes (""") of text-block must be on the new line.'
    //   'Text-block quotes are not vertically aligned'
  }
  public void getData(String text, int num) {}
  public void getData(String text) {}
}

Example: Opening and closing quotes are on new lines but not vertically aligned


public class Example4 {

  public String testMethod1() {

    final String simpleScriptViolate =
      """
          s
           """; // violation 'text-block quotes are not vertically aligned'
    final String simpleScriptCorrect =
      """
          s
      """;

    getData(
      1,
        """
        this is a multi-line message
           """); // violation 'Text-block quotes are not vertically aligned'
    getData(
      1,
        """
        this is a multi-line message
        """);

    return
        """
        this is sample text
      """; // violation 'text-block quotes are not vertically aligned'
  }
  public void getData(int num, String text) {}
}

Example: Each line of text in the text block must be indented at least as much as the opening and closing quotes.


public class Example5 {

  public String testMethod1() {
    // violation 2 lines below 'Each line of text in the text block must be indented'
    final String simpleScriptViolate =
            """
         s
            """;
    final String simpleScriptCorrect =
            """
            s
            """;
    // violation 3 lines below 'Each line of text in the text block must be indented'
    getData(
      1,
        """
       this is a multi-line message
        """);
    getData(
      1,
        """
        this is a multi-line message
        """);

    return
        """
        this is sample text
      """; // violation 'text-block quotes are not vertically aligned'
  }
  public void getData(int num, String text) {}
}

Example of Usage

Violation Messages

All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.

Fully Qualified Name

com.puppycrawl.tools.checkstyle.checks.coding.TextBlockGoogleStyleFormattingCheck

Use this fully qualified class name in configuration when an exact class reference is required.

Parent Module

TreeWalker