Clover Coverage Report - Checkstyle
Coverage timestamp: Fri May 9 2008 10:48:13 EST
../../../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
92   390   46   6.13
30   188   0.5   15
15     3.07  
1    
 
  FallThroughCheck       Line # 66 97.1% 0.9708029
46.05 92 46 46 0.5
 
  (3)
 
1    ////////////////////////////////////////////////////////////////////////////////
2    // checkstyle: Checks Java source code for adherence to a set of rules.
3    // Copyright (C) 2001-2005 Oliver Burn
4    //
5    // This library is free software; you can redistribute it and/or
6    // modify it under the terms of the GNU Lesser General Public
7    // License as published by the Free Software Foundation; either
8    // version 2.1 of the License, or (at your option) any later version.
9    //
10    // This library is distributed in the hope that it will be useful,
11    // but WITHOUT ANY WARRANTY; without even the implied warranty of
12    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    // Lesser General Public License for more details.
14    //
15    // You should have received a copy of the GNU Lesser General Public
16    // License along with this library; if not, write to the Free Software
17    // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18    ////////////////////////////////////////////////////////////////////////////////
19    package com.puppycrawl.tools.checkstyle.checks.coding;
20   
21    import java.util.regex.Matcher;
22    import java.util.regex.Pattern;
23   
24    import com.puppycrawl.tools.checkstyle.api.Check;
25    import com.puppycrawl.tools.checkstyle.api.DetailAST;
26    import com.puppycrawl.tools.checkstyle.api.TokenTypes;
27    import com.puppycrawl.tools.checkstyle.api.Utils;
28   
29    /**
30    * Checks for fall through in switch statements
31    * Finds locations where a case contains Java code -
32    * but lacks a break, return, throw or continue statement.
33    *
34    * <p>
35    * The check honors special comments to suppress warnings about
36    * the fall through. By default the comments "fallthru",
37    * "fall through", "falls through" and "fallthrough" are recognized.
38    * </p>
39    * <p>
40    * The following fragment of code will NOT trigger the check,
41    * because of the comment "fallthru".
42    * </p>
43    * <pre>
44    * case 3:
45    * x = 2;
46    * // fallthru
47    * case 4:
48    * </pre>
49    * <p>
50    * The recognized relief comment can be configured with the property
51    * <code>reliefPattern</code>. Default value of this regular expression
52    * is "fallthru|fall through|fallthrough|falls through".
53    * </p>
54    * <p>
55    * An example of how to configure the check is:
56    * </p>
57    * <pre>
58    * &lt;module name="FallThrough"&gt;
59    * &lt;property name=&quot;reliefPattern&quot;
60    * value=&quot;Fall Through&quot;/&gt;
61    * &lt;/module&gt;
62    * </pre>
63    *
64    * @author o_sukhodolsky
65    */
 
66    public class FallThroughCheck extends Check
67    {
68    /** Do we need to check last case group. */
69    private boolean mCheckLastGroup;
70   
71    /** Relief pattern to allow fall throught to the next case branch. */
72    private String mReliefPattern = "fallthru|falls? ?through";
73   
74    /** Relief regexp. */
75    private Pattern mRegExp;
76   
77    /** Creates new instance of the check. */
 
78  3 toggle public FallThroughCheck()
79    {
80    // do nothing
81    }
82   
83    /** {@inheritDoc} */
 
84  3 toggle public int[] getDefaultTokens()
85    {
86  3 return new int[]{TokenTypes.CASE_GROUP};
87    }
88   
89    /** {@inheritDoc} */
 
90  0 toggle public int[] getRequiredTokens()
91    {
92  0 return getDefaultTokens();
93    }
94   
95    /**
96    * Set the relief pattern.
97    *
98    * @param aPattern
99    * The regular expression pattern.
100    */
 
101  1 toggle public void setReliefPattern(String aPattern)
102    {
103  1 mReliefPattern = aPattern;
104    }
105   
106    /**
107    * Configures whether we need to check last case group or not.
108    * @param aValue new value of the property.
109    */
 
110  1 toggle public void setCheckLastCaseGroup(boolean aValue)
111    {
112  1 mCheckLastGroup = aValue;
113    }
114   
115    /** {@inheritDoc} */
 
116  3 toggle public void init()
117    {
118  3 super.init();
119  3 mRegExp = Utils.getPattern(mReliefPattern);
120    }
121   
122    /** {@inheritDoc} */
 
123  285 toggle public void visitToken(DetailAST aAST)
124    {
125  285 final DetailAST nextGroup = (DetailAST) aAST.getNextSibling();
126  285 final boolean isLastGroup =
127    ((nextGroup == null)
128    || (nextGroup.getType() != TokenTypes.CASE_GROUP));
129  285 if (isLastGroup && !mCheckLastGroup) {
130    // we do not need to check last group
131  30 return;
132    }
133   
134  255 final DetailAST slist = aAST.findFirstToken(TokenTypes.SLIST);
135   
136  255 if (!isTerminated(slist, true, true)) {
137  103 if (!hasFallTruComment(aAST, nextGroup)) {
138  50 if (!isLastGroup) {
139  48 log(nextGroup, "fall.through");
140    }
141    else {
142  2 log(aAST, "fall.through.last");
143    }
144    }
145    }
146    }
147   
148    /**
149    * Checks if a given subtree terminated by return, throw or,
150    * if allowed break, continue.
151    * @param aAST root of given subtree
152    * @param aUseBreak should we consider break as terminator.
153    * @param aUseCon