Since Checkstyle 3.0
'}'
) for code blocks.
This check supports if-else, try-catch-finally blocks, switch statements, switch cases,
while-loops, for-loops, method definitions, class definitions, constructor definitions,
instance, static initialization blocks, annotation definitions and enum definitions.
For right curly brace of expression blocks of arrays, lambdas and class instances
please follow issue
#5945.
For right curly brace of enum constant please follow issue
#7519.
name | description | type | default value | since |
---|---|---|---|---|
option | Specify the policy on placement of a right curly brace ('}' ). |
RightCurlyOption | same |
3.0 |
tokens | tokens to check | subset of tokens LITERAL_TRY , LITERAL_CATCH , LITERAL_FINALLY , LITERAL_IF , LITERAL_ELSE , CLASS_DEF , METHOD_DEF , CTOR_DEF , LITERAL_FOR , LITERAL_WHILE , LITERAL_DO , STATIC_INIT , INSTANCE_INIT , ANNOTATION_DEF , ENUM_DEF , INTERFACE_DEF , RECORD_DEF , COMPACT_CTOR_DEF , LITERAL_SWITCH , LITERAL_CASE . | LITERAL_TRY , LITERAL_CATCH , LITERAL_FINALLY , LITERAL_IF , LITERAL_ELSE . | 3.0 |
To configure the check:
<module name="Checker"> <module name="TreeWalker"> <module name="RightCurly"/> </module> </module>
Example:
public class Example1 { public void test() { boolean foo = false; if (foo) { bar(); } // violation, 'should be on the same line' // as the next part of a multi-block statement (one that directly // contains multiple blocks: if/else-if/else, do/while or try/catch/finally). else { bar(); } if (foo) { bar(); } else { bar(); } if (foo) { bar(); } int i = 0; // violation above, 'should be alone on a line.' if (foo) { bar(); } i = 0; try { bar(); } // violation, 'should be on the same line' // as the next part of a multi-block statement (one that directly // contains multiple blocks: if/else-if/else, do/while or try/catch/finally). catch (Exception e) { bar(); } try { bar(); } catch (Exception e) { bar(); } } private void bar() { } public void testSingleLine() { bar(); } // OK, because singleline is allowed }
To configure the check with policy alone
for else
and
METHOD_DEF
tokens:
<module name="Checker"> <module name="TreeWalker"> <module name="RightCurly"> <property name="option" value="alone"/> <property name="tokens" value="LITERAL_ELSE, METHOD_DEF"/> </module> </module> </module>
Example:
public class Example2 { public void test() { boolean foo = false; if (foo) { bar(); } else { bar(); } // violation above, 'should be alone on a line.' if (foo) { bar(); } else { bar(); } try { bar(); } catch (Exception e) { // OK above because config is set to token METHOD_DEF and LITERAL_ELSE bar(); } } private void bar() { } public void violate() { Object bar = "bar"; } // violation above, 'should be alone on a line.' public void ok() { bar(); } }
To configure the check with policy alone
for
Switch
Statements and Switch Cases:
<module name="Checker"> <module name="TreeWalker"> <module name="RightCurly"> <property name="option" value="alone"/> <property name="tokens" value="LITERAL_SWITCH, LITERAL_CASE"/> </module> </module> </module>
Example:
class Example3 { public void method0() { int mode = 0; int x; switch (mode) { case 1: int y = 1; break; case 2: {x = 1;} // violation '}' at column 22 should be alone on a line' case 3: int z = 0; {break;} // ok, the braces is not a first child of case default: x = 0; } // ok, RightCurly is alone } public void method01() { int mode = 0; switch (mode) { case 1: int x = 1; break; default: x = 0; } // violation above, 'should be alone on a line.' } }
To configure the check with policy alone_or_singleline
for if
and
METHOD_DEF
tokens:
<module name="Checker"> <module name="TreeWalker"> <module name="RightCurly"> <property name="option" value="alone_or_singleline"/> <property name="tokens" value="LITERAL_IF, METHOD_DEF"/> </module> </module> </module>
Example:
public class Example4 { public void test() { boolean foo = false; if (foo) { bar(); } else { // violation, 'should be alone on a line.' bar(); } if (foo) { bar(); } else { bar(); } try { bar(); } catch (Exception e) { // OK because config did not set token LITERAL_TRY bar(); } } private void bar() { } public void violate() { bar(); } // OK , because singleline }
To configure the check with policy alone_or_singleline
for
Switch
Statements and Switch Cases:
<module name="Checker"> <module name="TreeWalker"> <module name="RightCurly"> <property name="option" value="alone_or_singleline"/> <property name="tokens" value="LITERAL_SWITCH, LITERAL_CASE"/> </module> </module> </module>
Example:
class Example5 { public void method0() { int mode = 0; int x; switch (mode) { case 1: int y = 1; break; case 2: {x = 1;} // ok, RightCurly is in single line case 3: int z = 0; {break;} // ok, the braces is not a first child of case default: x = 0; } } public static void method7() { int mode = 0; switch (mode) { case 1: int x = 5; } // ok, RightCurly is on the same line as LeftCurly } public void method() { int mode = 0; int x; switch (mode) { case 1: x = 1; } // violation above, 'should be alone on a line.' } }
All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks.blocks