NoLineWrap

Since Checkstyle 5.8

Description

Checks that chosen statements are not line-wrapped. By default, this Check restricts wrapping import and package statements, but it's possible to check any statement.

Properties

name description type default value since
skipAnnotations Specify whether to skip annotations to be part of target token. boolean true 13.9.0
tokens tokens to check subset of tokens IMPORT , STATIC_IMPORT , MODULE_IMPORT , PACKAGE_DEF , CLASS_DEF , METHOD_DEF , CTOR_DEF , ENUM_DEF , INTERFACE_DEF , RECORD_DEF , COMPACT_CTOR_DEF . PACKAGE_DEF , IMPORT , STATIC_IMPORT , MODULE_IMPORT . 5.8

Examples

To configure the check to force no line-wrapping in package and import statements (default values):


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

Examples of line-wrapped statements (bad case):


package com.puppycrawl.      // violation 'should not be line-wrapped'
  tools.checkstyle.checks.whitespace.nolinewrap;

import com.puppycrawl.tools. // violation 'should not be line-wrapped'
  checkstyle.api.AbstractCheck;

import static java.math.     // violation 'should not be line-wrapped'
  BigInteger.ZERO;

class
  Example1 {

  public
    Example1() {}
  public void
    doSomething() {}
  @Deprecated
  private void doNothing() {}
}

To configure the check to force no line-wrapping only in import statements:


<module name="Checker">
  <module name="TreeWalker">
    <module name="NoLineWrap">
      <property name="tokens" value="IMPORT"/>
    </module>
  </module>
</module>

Example:


package com.puppycrawl.      // ok, PACKAGE_DEF is not part of the tokens
  tools.checkstyle.checks.whitespace.nolinewrap;

import com.puppycrawl.tools. // violation 'should not be line-wrapped'
  checkstyle.api.AbstractCheck;

import static java.math.     // ok, STATIC_IMPORT is not part of the tokens
  BigInteger.ZERO;

class
  Example2 {

  public
    Example2() {}
  public void
    doSomething() {}
  @Deprecated
  private void doNothing() {}
}

To configure the check for Java 21+ (default values):


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

Example of line-wrapped statements for Java 21+ (bad case):


// violation below 'should not be line-wrapped'
package com.puppycrawl.
  tools.checkstyle.checks.whitespace.nolinewrap;

// violation below 'should not be line-wrapped'
import java.util.
  List;

// violation below 'should not be line-wrapped'
import module
  java.base;

// violation below 'should not be line-wrapped'
import static java.math.
  BigInteger.ZERO;

To configure the check to force no line-wrapping annotations:


<module name="Checker">
  <module name="TreeWalker">
    <module name="NoLineWrap">
      <property name="tokens" value="IMPORT, STATIC_IMPORT,
      PACKAGE_DEF, CLASS_DEF, METHOD_DEF, CTOR_DEF"/>
      <property name="skipAnnotations" value="false"/>
    </module>
  </module>
</module>

Example:


package com.puppycrawl.      // violation 'should not be line-wrapped'
  tools.checkstyle.checks.whitespace.nolinewrap;

import com.puppycrawl.tools. // violation 'should not be line-wrapped'
  checkstyle.api.AbstractCheck;

import static java.math.     // violation 'should not be line-wrapped'
  BigInteger.ZERO;

class                        // violation 'should not be line-wrapped'
  Example4 {

  public                     // violation 'should not be line-wrapped'
    Example4() {}
  public void                // violation 'should not be line-wrapped'
    doSomething() {}
  @Deprecated                // violation 'should not be line-wrapped'
  private void doNothing() {}
}

Use Cases

To configure the check to force no line-wrapping only in class, method and constructor definitions:


<module name="Checker">
  <module name="TreeWalker">
    <module name="NoLineWrap">
      <property name="tokens" value="CLASS_DEF, METHOD_DEF, CTOR_DEF"/>
    </module>
  </module>
</module>

Example:


package com.puppycrawl.      // ok, PACKAGE_DEF is not part of the tokens
  tools.checkstyle.checks.whitespace.nolinewrap;

import com.puppycrawl.tools. // ok, IMPORT is not part of the tokens
  checkstyle.api.AbstractCheck;

import static java.math.     // ok, STATIC_IMPORT is not part of the tokens
  BigInteger.ZERO;

class                        // violation 'should not be line-wrapped'
  Example3 {

  public                     // violation 'should not be line-wrapped'
    Example3() {}
  public void                // violation 'should not be line-wrapped'
    doSomething() {}
}

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.NoLineWrapCheck

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

Parent Module

TreeWalker