LeftCurly

Since Checkstyle 3.0

Description

Checks for the placement of left curly braces ('{') for code blocks.

Properties

Examples

To configure the check:

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

Example:

class Example1
{ // violation, ''{' at column 1 should be on the previous line.'
  private interface TestInterface
  { // violation, ''{' at column 3 should be on the previous line.'
  }

  private
    class
    MyClass { // OK
  }

  enum Colors {RED, // OK
    BLUE,
    GREEN;
  }
}
        

To configure the check to apply the nl policy to type blocks:

<module name="Checker">
  <module name="TreeWalker">
    <module name="LeftCurly">
      <property name="option" value="nl"/>
      <property name="tokens" value="CLASS_DEF,INTERFACE_DEF"/>
    </module>
  </module>
</module>
        

Example:

class Example2
{ // OK
  private interface TestInterface
  { // OK
  }

  private
    class
    MyClass { // violation, ''{' at column 13 should be on a new line.'
  }

  enum Colors {RED, // OK
    BLUE,
    GREEN;
  }
}
        

To configure the check to apply the nlow policy to type blocks:

<module name="Checker">
  <module name="TreeWalker">
    <module name="LeftCurly">
      <property name="option" value="nlow"/>
      <property name="tokens" value="CLASS_DEF,INTERFACE_DEF"/>
    </module>
  </module>
</module>
        

Example:

class Example3
{ // violation, ''{' at column 1 should be on the previous line.'
  private interface TestInterface { // OK
  }

  private
    class
    MyClass { // violation, ''{' at column 13 should be on a new line.'
  }

  enum Colors {RED, // OK
    BLUE,
    GREEN;
  }
}
        

An example of how to configure the check to validate enum definitions:

<module name="Checker">
  <module name="TreeWalker">
    <module name="LeftCurly">
      <property name="ignoreEnums" value="false"/>
    </module>
  </module>
</module>
        

Example:

class Example4
{ // violation, ''{' at column 1 should be on the previous line.'
  private interface TestInterface
  { // violation, ''{' at column 3 should be on the previous line.'
  }

  private
    class
    MyClass { // OK
  }

  enum Colors {RED, // violation, ''{' at column 15 should have line break after.'
    BLUE,
    GREEN;
  }
}
        

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.

Package

com.puppycrawl.tools.checkstyle.checks.blocks

Parent Module

TreeWalker