KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > coding > DefaultComesLastCheck


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
25 /**
26  * <p>
27  * Check that the <code>default</code> is after all the <code>case</code>s
28  * in a <code>switch</code> statement.
29  * </p>
30  * <p>
31  * Rationale: Java allows <code>default</code> anywhere within the
32  * <code>switch</code> statement. But if it comes after the last
33  * <code>case</code> then it is more readable.
34  * </p>
35  * <p>
36  * An example of how to configure the check is:
37  * </p>
38  * <pre>
39  * &lt;module name="DefaultComesLast"/&gt;
40  * </pre>
41  * @author o_sukhodolsky
42  */

43 public class DefaultComesLastCheck extends Check
44 {
45     /** Creates new instance of the check. */
46     public DefaultComesLastCheck()
47     {
48         // do nothing
49
}
50
51     /** {@inheritDoc} */
52     public int[] getDefaultTokens()
53     {
54         return new int[] {
55             TokenTypes.LITERAL_DEFAULT,
56         };
57     }
58
59     /** {@inheritDoc} */
60     public int[] getAcceptableTokens()
61     {
62         return getDefaultTokens();
63     }
64
65     /** {@inheritDoc} */
66     public void visitToken(DetailAST aAST)
67     {
68         final DetailAST defaultGroupAST = aAST.getParent();
69         //default keywords used in annotations too - not what we're
70
//interested in
71
if (defaultGroupAST.getType() != TokenTypes.ANNOTATION_FIELD_DEF) {
72             final DetailAST switchAST = defaultGroupAST.getParent();
73             final DetailAST lastGroupAST =
74                 switchAST.getLastChild().getPreviousSibling();
75
76             if ((defaultGroupAST.getLineNo() != lastGroupAST.getLineNo())
77                 || (defaultGroupAST.getColumnNo()
78                     != lastGroupAST.getColumnNo()))
79             {
80                 log(aAST, "default.comes.last");
81             }
82         }
83     }
84 }
85
Popular Tags