Clover Coverage Report - Checkstyle
Coverage timestamp: Fri May 9 2008 10:48:13 EST
../../../../../../img/srcFileCovDistChart9.png 55% of files have more coverage
65   240   37   13
30   140   0.57   5
5     7.4  
1    
 
  LeftCurlyCheck       Line # 67 89% 0.89
38.82 65 37 37 0.57
 
  (8)
 
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.blocks;
20   
21    import com.puppycrawl.tools.checkstyle.api.DetailAST;
22    import com.puppycrawl.tools.checkstyle.api.TokenTypes;
23    import com.puppycrawl.tools.checkstyle.api.Utils;
24    import com.puppycrawl.tools.checkstyle.checks.AbstractOptionCheck;
25   
26    /**
27    * <p>
28    * Checks the placement of left curly braces on types, methods and
29    * other blocks:
30    * {@link TokenTypes#LITERAL_CATCH LITERAL_CATCH}, {@link
31    * TokenTypes#LITERAL_DO LITERAL_DO}, {@link TokenTypes#LITERAL_ELSE
32    * LITERAL_ELSE}, {@link TokenTypes#LITERAL_FINALLY LITERAL_FINALLY}, {@link
33    * TokenTypes#LITERAL_FOR LITERAL_FOR}, {@link TokenTypes#LITERAL_IF
34    * LITERAL_IF}, {@link TokenTypes#LITERAL_SWITCH LITERAL_SWITCH}, {@link
35    * TokenTypes#LITERAL_SYNCHRONIZED LITERAL_SYNCHRONIZED}, {@link
36    * TokenTypes#LITERAL_TRY LITERAL_TRY}, {@link TokenTypes#LITERAL_WHILE
37    * LITERAL_WHILE}.
38    * </p>
39    *
40    * <p>
41    * The policy to verify is specified using the {@link LeftCurlyOption} class and
42    * defaults to {@link LeftCurlyOption#EOL}. Policies {@link LeftCurlyOption#EOL}
43    * and {@link LeftCurlyOption#NLOW} take into account property maxLineLength.
44    * The default value for maxLineLength is 80.
45    * </p>
46    * <p>
47    * An example of how to configure the check is:
48    * </p>
49    * <pre>
50    * &lt;module name="LeftCurly"/&gt;
51    * </pre>
52    * <p>
53    * An example of how to configure the check with policy
54    * {@link LeftCurlyOption#NLOW} and maxLineLength 120 is:
55    * </p>
56    * <pre>
57    * &lt;module name="LeftCurly"&gt;
58    * &lt;property name="option"
59    * value="nlow"/&gt; &lt;property name="maxLineLength" value="120"/&gt; &lt;
60    * /module&gt;
61    * </pre>
62    *
63    * @author Oliver Burn
64    * @author lkuehne
65    * @version 1.0
66    */
 
67    public class LeftCurlyCheck
68    extends AbstractOptionCheck
69    {
70    /** default maximum line length */
71    private static final int DEFAULT_MAX_LINE_LENGTH = 80;
72   
73    /** TODO: replace this ugly hack **/
74    private int mMaxLineLength = DEFAULT_MAX_LINE_LENGTH;
75   
76    /**
77    * Creates a default instance and sets the policy to EOL.
78    */
 
79  8 toggle public LeftCurlyCheck()
80    {
81  8 super(LeftCurlyOption.EOL);
82    }
83   
84    /**
85    * Sets the maximum line length used in calculating the placement of the
86    * left curly brace.
87    * @param aMaxLineLength the max allowed line length
88    */
 
89  0 toggle public void setMaxLineLength(int aMaxLineLength)
90    {
91  0 mMaxLineLength = aMaxLineLength;
92    }
93   
94    /** {@inheritDoc} */
 
95  8 toggle public int[] getDefaultTokens()
96    {
97  8 return new int[] {
98    TokenTypes.INTERFACE_DEF,
99    TokenTypes.CLASS_DEF,
100    TokenTypes.ANNOTATION_DEF,
101    TokenTypes.ENUM_DEF,
102    TokenTypes.CTOR_DEF,
103    TokenTypes.METHOD_DEF,
104    TokenTypes.ENUM_CONSTANT_DEF,
105    TokenTypes.LITERAL_WHILE,
106    TokenTypes.LITERAL_TRY,
107    TokenTypes.LITERAL_CATCH,
108    TokenTypes.LITERAL_FINALLY,
109    TokenTypes.LITERAL_SYNCHRONIZED,
110    TokenTypes.LITERAL_SWITCH,
111    TokenTypes.LITERAL_DO,
112    TokenTypes.LITERAL_IF,
113    TokenTypes.LITERAL_ELSE,
114    TokenTypes.LITERAL_FOR,
115    // TODO: need to handle....
116    //TokenTypes.STATIC_INIT,
117    };
118    }
119   
120    /** {@inheritDoc} */
 
121  191 toggle public void visitToken(DetailAST aAST)
122    {
123  191 final DetailAST startToken;
124  191 final DetailAST brace;
125   
126  191 switch (aAST.getType()) {
127  9 case TokenTypes.CTOR_DEF :
128  71 case TokenTypes.METHOD_DEF :
129  80 startToken = aAST;
130  80 brace = aAST.findFirstToken(TokenTypes.SLIST);
131  80 break;
132   
133  18 case TokenTypes.INTERFACE_DEF :
134  14 case TokenTypes.CLASS_DEF :
135  3 case TokenTypes.ANNOTATION_DEF :
136  7 case TokenTypes.ENUM_DEF :
137  10 case TokenTypes.ENUM_CONSTANT_DEF :
138  52 startToken = (DetailAST) aAST.getFirstChild();
139  52 final DetailAST objBlock = aAST.findFirstToken(TokenTypes.OBJBLOCK);
140  52 brace = (objBlock == null)
141    ? null
142    : (DetailAST) objBlock.getFirstChild();
143  52 break;
144   
145  6 case TokenTypes.LITERAL_WHILE:
146  2 case TokenTypes.LITERAL_CATCH:
147  2 case TokenTypes.LITERAL_SYNCHRONIZED:
148  7 case TokenTypes.LITERAL_FOR:
149  2 case TokenTypes.LITERAL_TRY:
150  2 case TokenTypes.LITERAL_FINALLY:
151  4 case TokenTypes.LITERAL_DO:
152  21 case TokenTypes.LITERAL_IF :
153  46 startToken = aAST;
154  46 brace = aAST.findFirstToken(TokenTypes.SLIST);
155  46 break;
156   
157  11 case TokenTypes.LITERAL_ELSE :
158  11 startToken = aAST;
159  11 final DetailAST candidate = (DetailAST) aAST.getFirstChild();
160  11 brace =
161  11 (candidate.getType() == TokenTypes.SLIST)
162    ? candidate
163    : null; // silently ignore
164  11 break;
165   
166  2 case TokenTypes.LITERAL_SWITCH :
167  2 startToken = aAST;
168  2 brace = aAST.findFirstToken(TokenTypes.LCURLY);
169  2 break;
170   
171  0