diff --git a/src/xdocs-examples/java/com/puppycrawl/tools/checkstyle/checks/blocks/RightCurlyCheckExamplesTest.java b/src/xdocs-examples/java/com/puppycrawl/tools/checkstyle/checks/blocks/RightCurlyCheckExamplesTest.java index 3c44ac4168e..732751ef204 100644 --- a/src/xdocs-examples/java/com/puppycrawl/tools/checkstyle/checks/blocks/RightCurlyCheckExamplesTest.java +++ b/src/xdocs-examples/java/com/puppycrawl/tools/checkstyle/checks/blocks/RightCurlyCheckExamplesTest.java @@ -19,12 +19,13 @@ package com.puppycrawl.tools.checkstyle.checks.blocks; -import org.junit.jupiter.api.Disabled; +import static com.puppycrawl.tools.checkstyle.checks.blocks.RightCurlyCheck.MSG_KEY_LINE_ALONE; +import static com.puppycrawl.tools.checkstyle.checks.blocks.RightCurlyCheck.MSG_KEY_LINE_SAME; + import org.junit.jupiter.api.Test; import com.puppycrawl.tools.checkstyle.AbstractExamplesModuleTestSupport; -@Disabled("until https://github.com/checkstyle/checkstyle/issues/13345") public class RightCurlyCheckExamplesTest extends AbstractExamplesModuleTestSupport { @Override protected String getPackageLocation() { @@ -34,45 +35,48 @@ protected String getPackageLocation() { @Test public void testExample1() throws Exception { final String[] expected = { - + "19:5: " + getCheckMessage(MSG_KEY_LINE_SAME, "}", "5"), + "32:23: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", "23"), + "40:5: " + getCheckMessage(MSG_KEY_LINE_SAME, "}", "5"), }; - verifyWithInlineConfigParser(getPath("Example1.txt"), expected); + verifyWithInlineConfigParser(getPath("Example1.java"), expected); } @Test public void testExample2() throws Exception { final String[] expected = { - + "22:21: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", "21"), + "43:47: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", "47"), }; - verifyWithInlineConfigParser(getPath("Example2.txt"), expected); + verifyWithInlineConfigParser(getPath("Example2.java"), expected); } @Test public void testExample3() throws Exception { final String[] expected = { - + "35:16: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", "16"), }; - verifyWithInlineConfigParser(getPath("Example3.txt"), expected); + verifyWithInlineConfigParser(getPath("Example3.java"), expected); } @Test public void testExample4() throws Exception { final String[] expected = { - + "22:5: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", "5"), }; - verifyWithInlineConfigParser(getPath("Example4.txt"), expected); + verifyWithInlineConfigParser(getPath("Example4.java"), expected); } @Test public void testExample5() throws Exception { final String[] expected = { - + "41:16: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", "16"), }; - verifyWithInlineConfigParser(getPath("Example5.txt"), expected); + verifyWithInlineConfigParser(getPath("Example5.java"), expected); } } diff --git a/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/blocks/rightcurly/Example1.java b/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/blocks/rightcurly/Example1.java new file mode 100644 index 00000000000..92472f84a32 --- /dev/null +++ b/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/blocks/rightcurly/Example1.java @@ -0,0 +1,60 @@ +/*xml + + + + + +*/ + +package com.puppycrawl.tools.checkstyle.checks.blocks.rightcurly; + +// xdoc section -- start +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 +} +// xdoc section -- end diff --git a/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/blocks/rightcurly/Example1.txt b/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/blocks/rightcurly/Example1.txt deleted file mode 100644 index 8553ec24f9b..00000000000 --- a/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/blocks/rightcurly/Example1.txt +++ /dev/null @@ -1,50 +0,0 @@ -/*xml - - - - - -*/ - -// xdoc section -- start -public class Test { - - public void test() { - - if (foo) { - bar(); - } // violation, right curly must be in the same line as the 'else' keyword - else { - bar(); - } - - if (foo) { - bar(); - } else { // OK - bar(); - } - - if (foo) { bar(); } int i = 0; // violation - // ^^^ statement is not allowed on same line after curly right brace - - if (foo) { bar(); } // OK - int i = 0; - - try { - bar(); - } // violation, rightCurly must be in the same line as 'catch' keyword - catch (Exception e) { - bar(); - } - - try { - bar(); - } catch (Exception e) { // OK - bar(); - } - - } // OK - - public void testSingleLine() { bar(); } // OK, because singleline is allowed -} -// xdoc section -- end diff --git a/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/blocks/rightcurly/Example2.txt b/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/blocks/rightcurly/Example2.java similarity index 52% rename from src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/blocks/rightcurly/Example2.txt rename to src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/blocks/rightcurly/Example2.java index e61235eca64..dc9449da2e6 100644 --- a/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/blocks/rightcurly/Example2.txt +++ b/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/blocks/rightcurly/Example2.java @@ -9,33 +9,42 @@ */ +package com.puppycrawl.tools.checkstyle.checks.blocks.rightcurly; + // xdoc section -- start -public class Test { +public class Example2 { public void test() { + boolean foo = false; if (foo) { bar(); - } else { bar(); } // violation, right curly must be alone on line + } else { bar(); } + // violation above, 'should be alone on a line.' if (foo) { bar(); } else { bar(); - } // OK + } try { bar(); - } catch (Exception e) { // OK because config is set to token METHOD_DEF and LITERAL_ELSE + } catch (Exception e) { + // OK above because config is set to token METHOD_DEF and LITERAL_ELSE bar(); } - } // OK + } + + private void bar() { + } - public void violate() { bar; } // violation, singleline is not allowed here + public void violate() { Object bar = "bar"; } + // violation above, 'should be alone on a line.' public void ok() { bar(); - } // OK + } } // xdoc section -- end diff --git a/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/blocks/rightcurly/Example3.java b/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/blocks/rightcurly/Example3.java new file mode 100644 index 00000000000..3b359e9f5c0 --- /dev/null +++ b/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/blocks/rightcurly/Example3.java @@ -0,0 +1,40 @@ +/*xml + + + + + + + + +*/ + +package com.puppycrawl.tools.checkstyle.checks.blocks.rightcurly; + +// xdoc section -- start +class Example3 { + + public void method0() { + int mode = 0; + switch (mode) { + case 1: + int x = 1; + break; + 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.' + } + +} +// xdoc section -- end diff --git a/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/blocks/rightcurly/Example3.txt b/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/blocks/rightcurly/Example3.txt deleted file mode 100644 index 1fed41ed5af..00000000000 --- a/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/blocks/rightcurly/Example3.txt +++ /dev/null @@ -1,37 +0,0 @@ -/*xml - - - - - - - - -*/ - -// xdoc section -- start -class Test { - - public void method0() { - int mode = 0; - switch (mode) { - case 1: - int x = 1; - break; - default: - x = 0; - } // ok, RightCurly is alone - } - - public void method0() { - int mode = 0; - switch (mode) { - case 1: - int x = 1; - break; - default: - x = 0; } // violation, RightCurly should be alone on a line - } - -} -// xdoc section -- end diff --git a/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/blocks/rightcurly/Example4.txt b/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/blocks/rightcurly/Example4.java similarity index 65% rename from src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/blocks/rightcurly/Example4.txt rename to src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/blocks/rightcurly/Example4.java index ec83cdfcb11..49cf9e5ee1d 100644 --- a/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/blocks/rightcurly/Example4.txt +++ b/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/blocks/rightcurly/Example4.java @@ -9,31 +9,37 @@ */ +package com.puppycrawl.tools.checkstyle.checks.blocks.rightcurly; + // xdoc section -- start -public class Test { +public class Example4 { public void test() { + boolean foo = false; if (foo) { bar(); - } else { // violation, right curly must be alone on line + } else { // violation, 'should be alone on a line.' bar(); } if (foo) { bar(); - } // OK + } else { bar(); } try { bar(); - } catch (Exception e) { // OK because config did not set token LITERAL_TRY + } catch (Exception e) { // OK because config did not set token LITERAL_TRY bar(); } - } // OK + } + + private void bar() { + } public void violate() { bar(); } // OK , because singleline } diff --git a/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/blocks/rightcurly/Example5.java b/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/blocks/rightcurly/Example5.java new file mode 100644 index 00000000000..730135fdb94 --- /dev/null +++ b/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/blocks/rightcurly/Example5.java @@ -0,0 +1,45 @@ +/*xml + + + + + + + + +*/ + +package com.puppycrawl.tools.checkstyle.checks.blocks.rightcurly; + +// xdoc section -- start +class Example5 { + + public void method0() { + int mode = 0; + switch (mode) { + case 1: + int x = 1; + break; + 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.' + } +} +// xdoc section -- end diff --git a/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/blocks/rightcurly/Example5.txt b/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/blocks/rightcurly/Example5.txt deleted file mode 100644 index 9fb46d450ed..00000000000 --- a/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/blocks/rightcurly/Example5.txt +++ /dev/null @@ -1,40 +0,0 @@ -/*xml - - - - - - - - -*/ - -// xdoc section -- start -class Test { - - public void method0() { - int mode = 0; - switch (mode) { - case 1: - int x = 1; - break; - default: - x = 0; - } // ok - } - - public static void method7() { - int mode = 0; - int x; - switch (mode) { case 1: 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, right curly should be alone on line - } -} -// xdoc section -- end diff --git a/src/xdocs/checks/blocks/rightcurly.xml b/src/xdocs/checks/blocks/rightcurly.xml index 13ca6d9529a..bc46c4ee522 100644 --- a/src/xdocs/checks/blocks/rightcurly.xml +++ b/src/xdocs/checks/blocks/rightcurly.xml @@ -113,43 +113,51 @@

Example:

-public class Test { +public class Example1 { public void test() { + boolean foo = false; if (foo) { bar(); - } // violation, right curly must be in the same line as the 'else' keyword + } // 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 { // OK + } else { bar(); } - if (foo) { bar(); } int i = 0; // violation - // ^^^ statement is not allowed on same line after curly right brace + if (foo) { bar(); } int i = 0; + // violation above, 'should be alone on a line.' - if (foo) { bar(); } // OK - int i = 0; + if (foo) { bar(); } + i = 0; try { bar(); - } // violation, rightCurly must be in the same line as 'catch' keyword + } // 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) { // OK + } catch (Exception e) { bar(); } - } // OK + } + + private void bar() { + } public void testSingleLine() { bar(); } // OK, because singleline is allowed } @@ -172,33 +180,40 @@ public class Test {

Example:

-public class Test { +public class Example2 { public void test() { + boolean foo = false; if (foo) { bar(); - } else { bar(); } // violation, right curly must be alone on line + } else { bar(); } + // violation above, 'should be alone on a line.' if (foo) { bar(); } else { bar(); - } // OK + } try { bar(); - } catch (Exception e) { // OK because config is set to token METHOD_DEF and LITERAL_ELSE + } catch (Exception e) { + // OK above because config is set to token METHOD_DEF and LITERAL_ELSE bar(); } - } // OK + } + + private void bar() { + } - public void violate() { bar; } // violation, singleline is not allowed here + public void violate() { Object bar = "bar"; } + // violation above, 'should be alone on a line.' public void ok() { bar(); - } // OK + } }

@@ -219,28 +234,29 @@ public class Test {

Example:

-class Test { - - public void method0() { - int mode = 0; - switch (mode) { - case 1: - int x = 1; - break; - default: - x = 0; - } // ok, RightCurly is alone - } - - public void method0() { - int mode = 0; - switch (mode) { - case 1: - int x = 1; - break; - default: - x = 0; } // violation, RightCurly should be alone on a line - } +class Example3 { + + public void method0() { + int mode = 0; + switch (mode) { + case 1: + int x = 1; + break; + 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.' + } } @@ -263,30 +279,34 @@ class Test {

Example:

-public class Test { +public class Example4 { public void test() { + boolean foo = false; if (foo) { bar(); - } else { // violation, right curly must be alone on line + } else { // violation, 'should be alone on a line.' bar(); } if (foo) { bar(); - } // OK + } else { bar(); } try { bar(); - } catch (Exception e) { // OK because config did not set token LITERAL_TRY + } catch (Exception e) { // OK because config did not set token LITERAL_TRY bar(); } - } // OK + } + + private void bar() { + } public void violate() { bar(); } // OK , because singleline } @@ -309,32 +329,35 @@ public class Test {

Example:

-class Test { - - public void method0() { - int mode = 0; - switch (mode) { - case 1: - int x = 1; - break; - default: - x = 0; - } // ok - } - - public static void method7() { - int mode = 0; - int x; - switch (mode) { case 1: 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, right curly should be alone on line +class Example5 { + + public void method0() { + int mode = 0; + switch (mode) { + case 1: + int x = 1; + break; + 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.' + } } diff --git a/src/xdocs/checks/blocks/rightcurly.xml.template b/src/xdocs/checks/blocks/rightcurly.xml.template index ac6e2817f1a..6618086e1aa 100644 --- a/src/xdocs/checks/blocks/rightcurly.xml.template +++ b/src/xdocs/checks/blocks/rightcurly.xml.template @@ -35,13 +35,13 @@

To configure the check:

+ value="/resources/com/puppycrawl/tools/checkstyle/checks/blocks/rightcurly/Example1.java"/>

Example:

+ value="/resources/com/puppycrawl/tools/checkstyle/checks/blocks/rightcurly/Example1.java"/>

@@ -52,13 +52,13 @@

+ value="/resources/com/puppycrawl/tools/checkstyle/checks/blocks/rightcurly/Example2.java"/>

Example:

+ value="/resources/com/puppycrawl/tools/checkstyle/checks/blocks/rightcurly/Example2.java"/>

@@ -69,13 +69,13 @@

+ value="/resources/com/puppycrawl/tools/checkstyle/checks/blocks/rightcurly/Example3.java"/>

Example:

+ value="/resources/com/puppycrawl/tools/checkstyle/checks/blocks/rightcurly/Example3.java"/>

@@ -87,13 +87,13 @@

+ value="/resources/com/puppycrawl/tools/checkstyle/checks/blocks/rightcurly/Example4.java"/>

Example:

+ value="/resources/com/puppycrawl/tools/checkstyle/checks/blocks/rightcurly/Example4.java"/>

@@ -104,13 +104,13 @@

+ value="/resources/com/puppycrawl/tools/checkstyle/checks/blocks/rightcurly/Example5.java"/>

Example:

+ value="/resources/com/puppycrawl/tools/checkstyle/checks/blocks/rightcurly/Example5.java"/>