RightCurly
Since Checkstyle 3.0
Description
'}') for code blocks. This check
supports if-else, try-catch-finally blocks, switch statements, switch cases, switch default,
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.
Properties
| 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_DEFAULT . | LITERAL_TRY , LITERAL_CATCH , LITERAL_FINALLY , LITERAL_IF , LITERAL_ELSE . | 3.0 |
Examples
To configure the check:
<module name="Checker">
<module name="TreeWalker">
<module name="RightCurly"/>
</module>
</module>
Example:
public class Example1 {
public void test() {
boolean f = false;
if (f) {
bar();
} // violation, 'should be on the same line'
else {
bar();
}
if (f) {
bar();
} else {
bar();
}
if (f) { bar(); } int i = 0; // violation, 'should be alone on a line.'
try {
bar();
} // violation, 'should be on the same line'
catch (Exception e) {
bar();
}
}
private void bar() {
}
public void testSingleLine() { bar(); }
public void violate() { Object b = "b"; }
public void method0() {
int mode = 0;
int x;
switch (mode) {
case 1: int y = 1; break;
case 2: {x = 1;}
case 3: int z = 0; {break;}
default: {x = 0;}
}
switch (mode) {
case 1: x = 1; break;
default: x = 0; }
}
}
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 f = false;
if (f) {
bar();
}
else {
bar();
}
if (f) {
bar();
} else {
bar();
}
if (f) { bar(); } int i = 0;
try {
bar();
}
catch (Exception e) {
bar();
}
}
private void bar() {
}
public void testSingleLine()
{ bar(); } // violation, 'should be alone on a line.'
public void violate()
{ Object b = "b"; } // violation, 'should be alone on a line.'
public void method0() {
int mode = 0;
int x;
switch (mode) {
case 1: int y = 1; break;
case 2: {x = 1;}
case 3: int z = 0; {break;}
default: {x = 0;}
}
switch (mode) {
case 1: x = 1; break;
default: x = 0; }
}
}
To configure the check with policy alone for
Switch
Statements, Switch Cases and Switch Default:
<module name="Checker">
<module name="TreeWalker">
<module name="RightCurly">
<property name="option" value="alone"/>
<property name="tokens" value="LITERAL_SWITCH, LITERAL_CASE, LITERAL_DEFAULT"/>
</module>
</module>
</module>
Example:
public class Example3 {
public void test() {
boolean f = false;
if (f) {
bar();
}
else {
bar();
}
if (f) {
bar();
} else {
bar();
}
if (f) { bar(); } int i = 0;
try {
bar();
}
catch (Exception e) {
bar();
}
}
private void bar() {
}
public void testSingleLine() { bar(); }
public void violate() { Object b = "b"; }
public void method0() {
int mode = 0;
int x;
switch (mode) {
case 1: int y = 1; break;
case 2: {x = 1;} // violation, 'should be alone on a line.'
case 3: int z = 0; {break;}
default: {x = 0;} // violation, 'should be alone on a line.'
}
switch (mode) {
case 1: x = 1; break;
default: x = 0; } // violation, '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 f = false;
if (f) {
bar();
}
else {
bar();
}
if (f) {
bar();
} else { // violation, 'should be alone on a line.'
bar();
}
if (f) { bar(); } int i = 0; // violation, 'should be alone on a line.'
try {
bar();
}
catch (Exception e) {
bar();
}
}
private void bar() {
}
public void testSingleLine() { bar(); }
public void violate() { Object b = "b"; }
public void method0() {
int mode = 0;
int x;
switch (mode) {
case 1: int y = 1; break;
case 2: {x = 1;}
case 3: int z = 0; {break;}
default: {x = 0;}
}
switch (mode) {
case 1: x = 1; break;
default: x = 0; }
}
}
To configure the check with policy alone_or_singleline for
Switch
Statements, Switch Cases and Switch Default:
<module name="Checker">
<module name="TreeWalker">
<module name="RightCurly">
<property name="option" value="alone_or_singleline"/>
<property name="tokens" value="LITERAL_SWITCH, LITERAL_CASE, LITERAL_DEFAULT"/>
</module>
</module>
</module>
Example:
public class Example5 {
public void test() {
boolean f = false;
if (f) {
bar();
}
else {
bar();
}
if (f) {
bar();
} else {
bar();
}
if (f) { bar(); } int i = 0;
try {
bar();
}
catch (Exception e) {
bar();
}
}
private void bar() {
}
public void testSingleLine() { bar(); }
public void violate() { Object b = "b"; }
public void method0() {
int mode = 0;
int x;
switch (mode) {
case 1: int y = 1; break;
case 2: {x = 1;}
case 3: int z = 0; {break;}
default: {x = 0;}
}
switch (mode) {
case 1: x = 1; break;
default: x = 0; } // violation, 'should be alone on a line.'
}
}
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






