Clover Coverage Report - Checkstyle
Coverage timestamp: Fri May 9 2008 10:48:13 EST
../../../../../../img/srcFileCovDistChart7.png 83% of files have more coverage
70   226   54   6.36
8   148   0.77   11
11     4.91  
1    
 
  ParameterAssignmentCheck       Line # 42 64% 0.64044946
189.54 70 54 54 0.77
 
  (1)
 
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 com.puppycrawl.tools.checkstyle.api.Check;
22    import com.puppycrawl.tools.checkstyle.api.DetailAST;
23    import com.puppycrawl.tools.checkstyle.api.TokenTypes;
24    import java.util.HashSet;
25    import java.util.Set;
26    import java.util.Stack;
27   
28    /**
29    * <p>
30    * Disallow assignment of parameters.
31    * </p>
32    * <p>
33    * Rationale:
34    * Parameter assignment is often considered poor
35    * programming practice. Forcing developers to declare
36    * parameters as final is often onerous. Having a check
37    * ensure that parameters are never assigned would give
38    * the best of both worlds.
39    * </p>
40    * @author <a href="mailto:simon@redhillconsulting.com.au">Simon Harris</a>
41    */
 
42    public final class ParameterAssignmentCheck extends Check
43    {
44    /** Stack of methods' parameters. */
45    private final Stack mParameterNamesStack = new Stack();
46    /** Current set of perameters. */
47    private Set mParameterNames;
48   
49    /** {@inheritDoc} */
 
50  1 toggle public int[] getDefaultTokens()
51    {
52  1 return new int[] {
53    TokenTypes.CTOR_DEF,
54    TokenTypes.METHOD_DEF,
55    TokenTypes.ASSIGN,
56    TokenTypes.PLUS_ASSIGN,
57    TokenTypes.MINUS_ASSIGN,
58    TokenTypes.STAR_ASSIGN,
59    TokenTypes.DIV_ASSIGN,
60    TokenTypes.MOD_ASSIGN,
61    TokenTypes.SR_ASSIGN,
62    TokenTypes.BSR_ASSIGN,
63    TokenTypes.SL_ASSIGN,
64    TokenTypes.BAND_ASSIGN,
65    TokenTypes.BXOR_ASSIGN,
66    TokenTypes.BOR_ASSIGN,
67    TokenTypes.INC,
68    TokenTypes.POST_INC,
69    TokenTypes.DEC,
70    TokenTypes.POST_DEC,
71    };
72    }
73   
74    /** {@inheritDoc} */
 
75  0 toggle public int[] getRequiredTokens()
76    {
77  0 return getDefaultTokens();
78    }
79   
80    /** {@inheritDoc} */
 
81  1 toggle public void beginTree(DetailAST aRootAST)
82    {
83    // clear data
84  1 mParameterNamesStack.clear();
85  1 mParameterNames = null;
86    }
87   
88    /** {@inheritDoc} */
 
89  13 toggle public void visitToken(DetailAST aAST)
90    {
91  13 switch (aAST.getType()) {
92  0 case TokenTypes.CTOR_DEF:
93  3 case TokenTypes.METHOD_DEF:
94  3 visitMethodDef(aAST);
95  3 break;
96  5 case TokenTypes.ASSIGN:
97  2 case TokenTypes.PLUS_ASSIGN:
98  0 case TokenTypes.MINUS_ASSIGN:
99  0 case TokenTypes.STAR_ASSIGN:
100  0 case TokenTypes.DIV_ASSIGN:
101  0 case TokenTypes.MOD_ASSIGN:
102  0 case TokenTypes.SR_ASSIGN:
103  0 case TokenTypes.BSR_ASSIGN:
104  0 case TokenTypes.SL_ASSIGN:
105  0 case TokenTypes.BAND_ASSIGN:
106  0 case TokenTypes.BXOR_ASSIGN:
107  0 case TokenTypes.BOR_ASSIGN:
108  7 visitAssign(aAST);
109  7 break;
110  0 case TokenTypes.INC:
111  2 case TokenTypes.POST_INC:
112  0 case TokenTypes.DEC:
113  1 case TokenTypes.POST_DEC:
114  3 visitIncDec(aAST);
115  3 break;
116  0 default:
117  0 throw new IllegalStateException(aAST.toString());
118    }
119    }
120   
121    /** {@inheritDoc} */
 
122  13 toggle public void leaveToken(DetailAST aAST)
123    {
124  13 switch (aAST.getType()) {
125  0 case TokenTypes.CTOR_DEF:
126  3 case TokenTypes.METHOD_DEF:
127  3 leaveMethodDef();
128  3 break;
129  5 case TokenTypes.ASSIGN:
130  2 case TokenTypes.PLUS_ASSIGN:
131  0 case TokenTypes.MINUS_ASSIGN:
132  0 case TokenTypes.STAR_ASSIGN:
133  0 case TokenTypes.DIV_ASSIGN:
134  0 case TokenTypes.MOD_ASSIGN:
135  0 case TokenTypes.SR_ASSIGN:
136  0 case TokenTypes.BSR_ASSIGN:
137  0 case TokenTypes.SL_ASSIGN:
138  0 case TokenTypes.BAND_ASSIGN:
139  0 case TokenTypes.BXOR_ASSIGN:
140  0 case TokenTypes.BOR_ASSIGN:
141  0 case TokenTypes.INC:
142  2 case TokenTypes.POST_INC:
143  0 case TokenTypes.DEC:
144  1 case TokenTypes.POST_DEC:
145    // Do nothing
146  10 break;
147  0 default:
148  0 throw new IllegalStateException(aAST.toString());
149    }
150    }
151   
152    /**
153    * Ckecks if this is assignments of parameter.
154    * @param aAST assignment to check.
155    */
 
156  7 toggle private void visitAssign(DetailAST aAST)
157    {
158  7 checkIdent(aAST);