From d46dc44e79ea9151e594e02a0ab3bec69786593c Mon Sep 17 00:00:00 2001 From: Oleg Sukhodolsky Date: Mon, 31 Oct 2005 12:31:38 +0000 Subject: [PATCH] FallThrough check now can check last case group (property checkLastCaseGroup, bug 1245942). --- .../checks/coding/FallThroughCheck.java | 25 ++++++++++++++-- .../checks/coding/messages.properties | 1 + .../checkstyle/coding/InputFallThrough.java | 4 +-- .../checks/coding/FallThroughCheckTest.java | 30 ++++++++++++++++--- .../checks/whitespace/ParenPadCheckTest.java | 2 +- src/xdocs/config_coding.xml | 19 ++++++++++++ src/xdocs/releasenotes.xml | 4 +++ 7 files changed, 75 insertions(+), 10 deletions(-) diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/FallThroughCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/FallThroughCheck.java index 5a5e85056..3f523c4ea 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/FallThroughCheck.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/FallThroughCheck.java @@ -38,6 +38,9 @@ import com.puppycrawl.tools.checkstyle.api.DetailAST; */ public class FallThroughCheck extends Check { + /** Do we need to check last case group. */ + private boolean mCheckLastGroup; + /** Creates new instance of the check. */ public FallThroughCheck() { @@ -56,19 +59,35 @@ public class FallThroughCheck extends Check return getDefaultTokens(); } + /** + * Configures whether we need to check last case group or not. + * @param aValue new value of the property. + */ + public void setCheckLastCaseGroup(boolean aValue) + { + mCheckLastGroup = aValue; + } + /** {@inheritDoc} */ public void visitToken(DetailAST aAST) { final DetailAST nextGroup = (DetailAST) aAST.getNextSibling(); - if (nextGroup == null || nextGroup.getType() != TokenTypes.CASE_GROUP) { - // last group we shouldn't check it + final boolean isLastGroup = + (nextGroup == null || nextGroup.getType() != TokenTypes.CASE_GROUP); + if (isLastGroup && !mCheckLastGroup) { + // we do not need to check last group return; } final DetailAST slist = aAST.findFirstToken(TokenTypes.SLIST); if (!isTerminated(slist, true, true)) { - log(nextGroup, "fall.through"); + if (!isLastGroup) { + log(nextGroup, "fall.through"); + } + else { + log(aAST, "fall.through.last"); + } } } diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/messages.properties b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/messages.properties index 5b98d0fa1..8bdc5dd2a 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/messages.properties +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/messages.properties @@ -72,6 +72,7 @@ explicit.init=Variable ''{0}'' explicitly initialized to ''{1}'' (default value default.comes.last=Default should be last label in the switch. missing.ctor=Class should define a constructor. fall.through=Fall through from previous branch of the switch statement. +fall.through.last=Fall through from the last branch of the switch statement. require.this.variable=Reference to instance variable ''{0}'' needs \"this.\". require.this.unfound.variable=Unable find where ''{0}'' is declared. require.this.method=Method call to ''{0}'' needs \"this.\". diff --git a/src/testinputs/com/puppycrawl/tools/checkstyle/coding/InputFallThrough.java b/src/testinputs/com/puppycrawl/tools/checkstyle/coding/InputFallThrough.java index 17a7137e6..ed071eb88 100644 --- a/src/testinputs/com/puppycrawl/tools/checkstyle/coding/InputFallThrough.java +++ b/src/testinputs/com/puppycrawl/tools/checkstyle/coding/InputFallThrough.java @@ -108,7 +108,7 @@ public class InputFallThrough return; default: return; - } + } case 24: switch (j) { case 1: @@ -117,7 +117,7 @@ public class InputFallThrough break; default: return; - } + } default: //fall through!!! // this is the last label i++; diff --git a/src/tests/com/puppycrawl/tools/checkstyle/checks/coding/FallThroughCheckTest.java b/src/tests/com/puppycrawl/tools/checkstyle/checks/coding/FallThroughCheckTest.java index 7441a0b16..f2329a813 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/checks/coding/FallThroughCheckTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/checks/coding/FallThroughCheckTest.java @@ -7,11 +7,33 @@ import java.io.File; public class FallThroughCheckTest extends BaseCheckTestCase { - public void testIt() - throws Exception + private DefaultConfiguration checkConfig; + + public void setUp() + { + checkConfig = createCheckConfig(FallThroughCheck.class); + } + + public void testIt() throws Exception + { + checkConfig.addAttribute("checkLastCaseGroup", "true"); + final String[] expected = { + "12:13: Fall through from previous branch of the switch statement.", + "36:13: Fall through from previous branch of the switch statement.", + "51:13: Fall through from previous branch of the switch statement.", + "68:13: Fall through from previous branch of the switch statement.", + "85:13: Fall through from previous branch of the switch statement.", + "103:13: Fall through from previous branch of the switch statement.", + "121:13: Fall through from previous branch of the switch statement.", + "121:13: Fall through from the last branch of the switch statement.", + }; + verify(checkConfig, + getPath("coding" + File.separator + "InputFallThrough.java"), + expected); + } + + public void testDefault() throws Exception { - final DefaultConfiguration checkConfig = - createCheckConfig(FallThroughCheck.class); final String[] expected = { "12:13: Fall through from previous branch of the switch statement.", "36:13: Fall through from previous branch of the switch statement.", diff --git a/src/tests/com/puppycrawl/tools/checkstyle/checks/whitespace/ParenPadCheckTest.java b/src/tests/com/puppycrawl/tools/checkstyle/checks/whitespace/ParenPadCheckTest.java index 30c5f4bce..84747d1d8 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/checks/whitespace/ParenPadCheckTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/checks/whitespace/ParenPadCheckTest.java @@ -58,7 +58,7 @@ public class ParenPadCheckTest }; verify(checkConfig, getPath("InputWhitespace.java"), expected); } - + public void testDefaultForIterator() throws Exception { diff --git a/src/xdocs/config_coding.xml b/src/xdocs/config_coding.xml index 15a342887..5ffdf8294 100755 --- a/src/xdocs/config_coding.xml +++ b/src/xdocs/config_coding.xml @@ -1863,6 +1863,25 @@ if ("something".equals(x))

+ + + + + + + + + + + + + + +
namedescriptiontypedefault value
checkLastCaseGroup + Whether we need to check last case group or not. + Booleanfalse
+
+

To configure the check: diff --git a/src/xdocs/releasenotes.xml b/src/xdocs/releasenotes.xml index b192cae1b..685fb4be5 100755 --- a/src/xdocs/releasenotes.xml +++ b/src/xdocs/releasenotes.xml @@ -36,6 +36,10 @@ classloader of this class can be used to load the correct resource bundle. Patch 1309516 from Ralf (rakus). +

  • + FallThrough check now can check last case group (property + checkLastCaseGroup, bug 1245942). +
  • Fixed Bugs: