From cd6e8a32d28ec94eefbf016638f6cc7d2662517a Mon Sep 17 00:00:00 2001 From: Oliver Burn Date: Fri, 18 Apr 2003 11:14:48 +0000 Subject: [PATCH] Patch from Oleg Sukhodolsky to add MissingSwitchDefaultCheck to check that a switch statement has a default clause (request 564199). --- docs/checkstyle_checks.xml | 1 + docs/config_misc.html | 30 ++++++++ docs/releasenotes.html | 4 ++ docs/sun_checks.xml | 3 + .../checks/IllegalInstantiationCheck.java | 6 +- .../checks/MissingSwitchDefaultCheck.java | 69 +++++++++++++++++++ .../checkstyle/checks/messages.properties | 1 + .../checkstyle/InputMissingSwitchDefault.java | 20 ++++++ .../puppycrawl/tools/checkstyle/AllTests.java | 2 + .../checks/MissingSwitchDefaultCheckTest.java | 26 +++++++ 10 files changed, 159 insertions(+), 3 deletions(-) create mode 100755 src/checkstyle/com/puppycrawl/tools/checkstyle/checks/MissingSwitchDefaultCheck.java create mode 100755 src/testinputs/com/puppycrawl/tools/checkstyle/InputMissingSwitchDefault.java create mode 100755 src/tests/com/puppycrawl/tools/checkstyle/checks/MissingSwitchDefaultCheckTest.java diff --git a/docs/checkstyle_checks.xml b/docs/checkstyle_checks.xml index a1329e5d9..aea02960f 100644 --- a/docs/checkstyle_checks.xml +++ b/docs/checkstyle_checks.xml @@ -99,5 +99,6 @@ + diff --git a/docs/config_misc.html b/docs/config_misc.html index a857b9e36..012ad5081 100644 --- a/docs/config_misc.html +++ b/docs/config_misc.html @@ -40,6 +40,9 @@
  • NewlineAtEndOfFile
  • +
  • + MissingSwitchDefault +
  • SimplifyBooleanExpression
  • @@ -290,6 +293,33 @@ <module name="InnerAssignment"> <property name="tokens" value="ASSIGN,PLUS_ASSIGN,MINUS_ASSIGN"/> </module> + +

    Package

    +

    + com.puppycrawl.tools.checkstyle.checks +

    +

    Parent Module

    +

    + TreeWalker +

    +

    MissingSwitchDefault

    Description

    +

    + Checks that switch statement has "default" clause. +

    +

    + Rationale: It's usually a good idea to introduce a default case + in every switch statement. Even if the developer is sure that + all currently possible cases are covered, this should be + expressed in the default branch, e.g. by using an + assertion. This way the code is protected aginst later changes, + e.g. introduction of new types in an enumeration type. +

    +

    Examples

    +

    + To configure the check: +

    +
    +<module name="MissingSwitchDefault"/>
           

    Package

    diff --git a/docs/releasenotes.html b/docs/releasenotes.html index ec5e5c653..5ac38a815 100644 --- a/docs/releasenotes.html +++ b/docs/releasenotes.html @@ -104,6 +104,10 @@ check that classes are declared final if they only contain private constructors (request 696290). +

  • Patch from Oleg Sukhodolsky to add + MissingSwitchDefaultCheck to check that a switch statement has a + default clause (request 564199).
  • +

    diff --git a/docs/sun_checks.xml b/docs/sun_checks.xml index 6119351dc..71d385d72 100644 --- a/docs/sun_checks.xml +++ b/docs/sun_checks.xml @@ -115,6 +115,9 @@ + + + diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/IllegalInstantiationCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/IllegalInstantiationCheck.java index 263e73037..ccb4f1737 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/IllegalInstantiationCheck.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/IllegalInstantiationCheck.java @@ -82,7 +82,7 @@ public class IllegalInstantiationCheck TokenTypes.PACKAGE_DEF }; } - + /** * Prevent user from changing tokens in the configuration. * @see com.puppycrawl.tools.checkstyle.api.Check @@ -91,7 +91,7 @@ public class IllegalInstantiationCheck { return new int[] {}; } - + /** @see com.puppycrawl.tools.checkstyle.api.Check */ public int[] getRequiredTokens() { @@ -101,7 +101,7 @@ public class IllegalInstantiationCheck TokenTypes.PACKAGE_DEF }; } - + /** @see com.puppycrawl.tools.checkstyle.api.Check */ public void beginTree(DetailAST aRootAST) { diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/MissingSwitchDefaultCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/MissingSwitchDefaultCheck.java new file mode 100755 index 000000000..2577fb8ba --- /dev/null +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/MissingSwitchDefaultCheck.java @@ -0,0 +1,69 @@ +//////////////////////////////////////////////////////////////////////////////// +// checkstyle: Checks Java source code for adherence to a set of rules. +// Copyright (C) 2001-2002 Oliver Burn +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +//////////////////////////////////////////////////////////////////////////////// +package com.puppycrawl.tools.checkstyle.checks; + +import com.puppycrawl.tools.checkstyle.api.Check; +import com.puppycrawl.tools.checkstyle.api.DetailAST; +import com.puppycrawl.tools.checkstyle.api.TokenTypes; + +/** + *

    + * Checks that switch statement has "default" clause. + *

    + *

    + * Rationale: It's usually a good idea to introduce a + * default case in every switch statement. Even if + * the developer is sure that all currently possible + * cases are covered, this should be expressed in the + * default branch, e.g. by using an assertion. This way + * the code is protected aginst later changes, e.g. + * introduction of new types in an enumeration type. + *

    + *

    + * An example of how to configure the check is: + *

    + *
    + * <module name="MissingSwitchDefault"/>
    + * 
    + * @author o_sukhodolsky + */ +public class MissingSwitchDefaultCheck + extends Check +{ + /** @see Check */ + public int[] getDefaultTokens() + { + return new int[]{TokenTypes.LITERAL_SWITCH}; + } + + /** @see Check */ + public void visitToken(DetailAST aAst) + { + DetailAST child = aAst.findFirstToken(TokenTypes.CASE_GROUP); + while (child != null) { + if ((child.getType() == TokenTypes.CASE_GROUP) + && child.branchContains(TokenTypes.LITERAL_DEFAULT)) + { + return; + } + child = (DetailAST) child.getNextSibling(); + } + log(aAst.getLineNo(), "missing.switch.default"); + } +} diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/messages.properties b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/messages.properties index 57a577c82..0ab794261 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/messages.properties +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/messages.properties @@ -79,3 +79,4 @@ doublechecked.locking.avoid=The double-checked locking idiom is broken and shoul design.forInheritance=Method ''{0}'' is not designed for inheritance - needs to be abstract, final or empty. final.class=Class {0} should be declared as final +missing.switch.default=switch without \"default\" clause diff --git a/src/testinputs/com/puppycrawl/tools/checkstyle/InputMissingSwitchDefault.java b/src/testinputs/com/puppycrawl/tools/checkstyle/InputMissingSwitchDefault.java new file mode 100755 index 000000000..a8fda9db3 --- /dev/null +++ b/src/testinputs/com/puppycrawl/tools/checkstyle/InputMissingSwitchDefault.java @@ -0,0 +1,20 @@ +public class test { + public void foo() { + int i = 1; + switch (i) { + case 1: i++; break; + case 2: i--; break; + default: return; + } + } +} + +class bad_test { + public void foo() { + int i = 1; + switch (i) { + case 1: i++; break; + case 2: i--; break; + } + } +} diff --git a/src/tests/com/puppycrawl/tools/checkstyle/AllTests.java b/src/tests/com/puppycrawl/tools/checkstyle/AllTests.java index 83784cc0e..2f18c6193 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/AllTests.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/AllTests.java @@ -29,6 +29,7 @@ import com.puppycrawl.tools.checkstyle.checks.LocalVariableNameCheckTest; import com.puppycrawl.tools.checkstyle.checks.MemberNameCheckTest; import com.puppycrawl.tools.checkstyle.checks.MethodLengthCheckTest; import com.puppycrawl.tools.checkstyle.checks.MethodNameCheckTest; +import com.puppycrawl.tools.checkstyle.checks.MissingSwitchDefaultCheckTest; import com.puppycrawl.tools.checkstyle.checks.ModifierOrderCheckTest; import com.puppycrawl.tools.checkstyle.checks.NeedBracesCheckTest; import com.puppycrawl.tools.checkstyle.checks.NewlineAtEndOfFileCheckTest; @@ -135,6 +136,7 @@ public class AllTests { suite.addTest(new TestSuite(WhitespaceAroundTest.class)); suite.addTest(new TestSuite(XMLLoggerTest.class)); suite.addTest(new TestSuite(FinalClassCheckTest.class)); + suite.addTest(new TestSuite(MissingSwitchDefaultCheckTest.class)); //$JUnit-END$ return suite; } diff --git a/src/tests/com/puppycrawl/tools/checkstyle/checks/MissingSwitchDefaultCheckTest.java b/src/tests/com/puppycrawl/tools/checkstyle/checks/MissingSwitchDefaultCheckTest.java new file mode 100755 index 000000000..e19626c09 --- /dev/null +++ b/src/tests/com/puppycrawl/tools/checkstyle/checks/MissingSwitchDefaultCheckTest.java @@ -0,0 +1,26 @@ +package com.puppycrawl.tools.checkstyle.checks; + +import com.puppycrawl.tools.checkstyle.BaseCheckTestCase; +import com.puppycrawl.tools.checkstyle.DefaultConfiguration; + +public class MissingSwitchDefaultCheckTest + extends BaseCheckTestCase +{ + private DefaultConfiguration mCheckConfig; + + public void setUp() + { + mCheckConfig = createCheckConfig(MissingSwitchDefaultCheck.class); + } + + public void testMissingSwitchDefault() throws Exception + { + final String[] expected = { + "15: switch without \"default\" clause", + }; + verify( + mCheckConfig, + getPath("InputMissingSwitchDefault.java"), + expected); + } +}