Clover Coverage Report - Checkstyle
Coverage timestamp: Fri May 9 2008 10:48:13 EST
../../../../../../img/srcFileCovDistChart9.png 55% of files have more coverage
47   190   19   15.67
20   106   0.4   1.5
3     6.33  
2    
 
  DeclarationOrderCheck       Line # 57 88.6% 0.8857143
19.54 47 19 19 0.4
  DeclarationOrderCheck.ScopeState       Line # 80 - -1.0
0 0 0 0 -
 
  (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.Scope;
24    import com.puppycrawl.tools.checkstyle.api.ScopeUtils;
25    import com.puppycrawl.tools.checkstyle.api.TokenTypes;
26    import java.util.Stack;
27   
28    /**
29    * <p>
30    * Checks that the parts of a class or interface declaration
31    * appear in the order suggested by the
32    * <a
33    * href="http://java.sun.com/docs/codeconv/html/CodeConventions.doc2.html#1852"
34    * >Code Conventions for the Java Programming Language</a>.
35    * </p>
36    * <p>
37    * <ol>
38    * <li> Class (static) variables. First the public class variables, then
39    * the protected, then package level (no access modifier), and then
40    * the private. </li>
41    * <li> Instance variables. First the public class variables, then
42    * the protected, then package level (no access modifier), and then
43    * the private. </li>
44    * <li> Constructors </li>
45    * <li> Methods </li>
46    * </ol>
47    * </p>
48    * <p>
49    * An example of how to configure the check is:
50    * </p>
51    * <pre>
52    * &lt;module name="DeclarationOrder"/&gt;
53    * </pre>
54    *
55    * @author r_auckenthaler
56    */
 
57    public class DeclarationOrderCheck extends Check
58    {
59    /** State for the VARIABLE_DEF */
60    private static final int STATE_STATIC_VARIABLE_DEF = 1;
61   
62    /** State for the VARIABLE_DEF */
63    private static final int STATE_INSTANCE_VARIABLE_DEF = 2;
64   
65    /** State for the CTOR_DEF */
66    private static final int STATE_CTOR_DEF = 3;
67   
68    /** State for the METHOD_DEF */
69    private static final int STATE_METHOD_DEF = 4;
70   
71    /**
72    * List of Declaration States. This is necessary due to
73    * inner classes that have their own state
74    */
75    private final Stack mScopeStates = new Stack();
76   
77    /**
78    * private class to encapsulate the state
79    */
 
80    private class ScopeState
81    {
82    /** The state the check is in */
83    private int mScopeState = STATE_STATIC_VARIABLE_DEF;
84   
85    /** The sub-state the check is in */
86    private Scope mDeclarationAccess = Scope.PUBLIC;
87    }
88   
89    /** {@inheritDoc} */
 
90  1 toggle public int[] getDefaultTokens()
91    {
92  1 return new int[] {
93    TokenTypes.CTOR_DEF,
94    TokenTypes.METHOD_DEF,
95    TokenTypes.MODIFIERS,
96    TokenTypes.OBJBLOCK,
97    };
98    }
99   
100    /** {@inheritDoc} */
 
101  70 toggle public void visitToken(DetailAST aAST)
102    {
103  70 final int parentType = aAST.getParent().getType();
104  70 ScopeState state;
105   
106  70 switch(aAST.getType()) {
107  5 case TokenTypes.OBJBLOCK:
108  5 mScopeStates.push(new ScopeState());
109  5 break;
110   
111  4 case TokenTypes.CTOR_DEF:
112  4 if (parentType != TokenTypes.OBJBLOCK) {
113  0 return;
114    }
115   
116  4 state = (ScopeState) mScopeStates.peek();
117  4 if (state.mScopeState > STATE_CTOR_DEF) {
118  2 log(aAST, "declaration.order.constructor");
119    }
120    else {
121  2 state.mScopeState = STATE_CTOR_DEF;
122    }
123  4 break;
124   
125  9 case TokenTypes.METHOD_DEF:
126  9 state = (ScopeState) mScopeStates.peek();
127  9 if (parentType != TokenTypes.OBJBLOCK) {
128  0 return;
129    }
130   
131  9 if (state.mScopeState > STATE_METHOD_DEF) {
132  0 log(aAST, "declaration.order.method");
133    }
134    else {
135  9 state.mScopeState = STATE_METHOD_DEF;
136    }
137  9 break;
138   
139  52 case TokenTypes.MODIFIERS:
140  52 if ((parentType != TokenTypes.VARIABLE_DEF)
141    || (aAST.getParent().getParent().getType()
142    != TokenTypes.OBJBLOCK))
143    {
144  23 return;
145    }
146   
147  29 state = (ScopeState) mScopeStates.peek();
148  29 if (aAST.findFirstToken(TokenTypes.LITERAL_STATIC) != null) {
149  25 if (state.mScopeState > STATE_STATIC_VARIABLE_DEF) {
150  5 log(aAST, "declaration.order.static");
151    }
152    else {
153  20 state.mScopeState = STATE_STATIC_VARIABLE_DEF;
154    }
155    }
156    else {
157  4 if (state.mScopeState > STATE_INSTANCE_VARIABLE_DEF) {
158  2 log(aAST, "declaration.order.instance");
159    }
160  2 else if (state.mScopeState == STATE_STATIC_VARIABLE_DEF) {
161  2 state.mDeclarationAccess = Scope.PUBLIC;
162  2 state.mScopeState = STATE_INSTANCE_VARIABLE_DEF;
163    }
164    }
165   
166  29 final Scope access = ScopeUtils.getScopeFromMods(aAST);
167  29 if (state.mDeclarationAccess.compareTo(access) > 0) {
168  16 log(aAST, "declaration.order.access");
169    }
170    else {
171  13 state.mDeclarationAccess = access;
172    }
173  29 break;
174   
175  0 default:
176    }
177    }
178   
179    /** {@inheritDoc} */
 
180  70 toggle public void leaveToken(DetailAST aAST)
181    {
182  70 switch(aAST.getType()) {
183  5 case TokenTypes.OBJBLOCK:
184  5 mScopeStates.pop();
185  5 break;
186   
187  65 default:
188    }
189    }
190    }