OperatorWrap

Since Checkstyle 3.0

Description

Checks the policy on how to wrap lines on operators.

See the Java Language Specification for more information about instanceof operator.

Properties

Examples

To configure the check:


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

Example:


class Example1 {
  void example() {
    String s = "Hello" + // violation ''\+' should be on a new line'
      "World";

    if (10 == // violation ''==' should be on a new line'
            20) {
    }

    int c = 10 /
            5; // violation above ''/' should be on a new line'

    int b
            = 10;
    int e =
            10;
    b
            += 10;
    b +=
            10;
    c
            *= 10;
    c
            -= 5;
    c -=
            5;
    c
            /= 2;
    c
            %= 1;
    c
            >>= 1;
    c
        >>>= 1;
    c
            &=1 ;
    c
            <<= 1;
  }
}

To configure the check for assignment operators at the end of a line:


<module name="Checker">
  <module name="TreeWalker">
    <module name="OperatorWrap">
      <property name="option" value="eol"/>
      <property name="tokens"
                value="ASSIGN,DIV_ASSIGN,PLUS_ASSIGN,MINUS_ASSIGN,STAR_ASSIGN,
                       MOD_ASSIGN,SR_ASSIGN,BSR_ASSIGN,SL_ASSIGN,
                       BXOR_ASSIGN,BOR_ASSIGN,BAND_ASSIGN"/>
    </module>
  </module>
</module>

Example:


class Example2 {
  void example() {
    String s = "Hello" +
      "World";

    if (10 ==
            20) {
    }

    int c = 10 /
            5;

    int b
            = 10; // violation ''=' should be on the previous line'
    int e =
            10;
    b
            += 10; // violation ''\+=' should be on the previous line'
    b +=
            10;
    c
            *= 10; // violation ''*=' should be on the previous line'
    c
            -= 5; // violation ''-=' should be on the previous line'
    c -=
            5;
    c
            /= 2; // violation ''/=' should be on the previous line'
    c
            %= 1; // violation ''%=' should be on the previous line'
    c
            >>= 1; // violation ''>>=' should be on the previous line'
    c
        >>>= 1; // violation ''>>>=' should be on the previous line'
    c
            &=1 ; // violation ''&=' should be on the previous line'
    c
            <<= 1; // violation ''<<=' should be on the previous line'
  }
}

To configure the check for operators at the beginning of a line:


<module name="Checker">
  <module name="TreeWalker">
    <module name="OperatorWrap">
      <property name="option" value="nl"/>
    </module>
  </module>
</module>

Example:


class Example3 {
  void example() {
    String s = "Hello" + // violation '+' should be on a new line
            "World";

    if (10 == // violation '==' should be on a new line
            20) {
    }

    int c = 10 /
            5; // violation above '/' should be on a new line

    int b
            = 10;
    int e =
            10;
    b
            += 10;
    b +=
            10;
    c
            *= 10;
    c
            -= 5;
    c -=
            5;
    c
            /= 2;
    c
            %= 1;
    c
            >>= 1;
    c
            >>>= 1;
    c
            &=1 ;
    c
            <<= 1;
  }
}

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.whitespace.OperatorWrapCheck

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

Parent Module

TreeWalker