KickJava   Java API By Example, From Geeks To Geeks.

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


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 import com.puppycrawl.tools.checkstyle.checks.CheckUtils;
26
27 /**
28  * <p>
29  * Checks that each variable declaration is in its own statement
30  * and on its own line.
31  * </p>
32  * <p>
33  * Rationale: <a
34  * HREF="http://java.sun.com/docs/codeconv/html/CodeConventions.doc5.html#2991">
35  * the SUN Code conventions chapter 6.1</a> recommends that
36  * declarations should be one per line.
37  * </p>
38  * <p>
39  * An example of how to configure the check is:
40  * </p>
41  * <pre>
42  * &lt;module name="MultipleVariableDeclarations"/&gt;
43  * </pre>
44  * @author o_sukhodolsky
45  */

46 public class MultipleVariableDeclarationsCheck extends Check
47 {
48     /** Creates new instance of the check. */
49     public MultipleVariableDeclarationsCheck()
50     {
51     }
52
53     /** {@inheritDoc} */
54     public int[] getDefaultTokens()
55     {
56         return new int[] {TokenTypes.VARIABLE_DEF};
57     }
58
59     /** {@inheritDoc} */
60     public void visitToken(DetailAST aAST)
61     {
62         DetailAST nextNode = (DetailAST) aAST.getNextSibling();
63         final boolean isCommaSeparated =
64             ((nextNode != null) && (nextNode.getType() == TokenTypes.COMMA));
65
66         if (nextNode == null) {
67             // no next statement - no check
68
return;
69         }
70
71         if ((nextNode.getType() == TokenTypes.COMMA)
72             || (nextNode.getType() == TokenTypes.SEMI))
73         {
74             nextNode = (DetailAST) nextNode.getNextSibling();
75         }
76
77         if ((nextNode != null)
78             && (nextNode.getType() == TokenTypes.VARIABLE_DEF))
79         {
80             final DetailAST firstNode = CheckUtils.getFirstNode(aAST);
81             if (isCommaSeparated) {
82                 log(firstNode, "multiple.variable.declarations.comma");
83                 return;
84             }
85
86             final DetailAST lastNode = getLastNode(aAST);
87             final DetailAST firstNextNode = CheckUtils.getFirstNode(nextNode);
88
89             if (firstNextNode.getLineNo() == lastNode.getLineNo()) {
90                 log(firstNode, "multiple.variable.declarations");
91             }
92         }
93
94     }
95
96     /**
97      * Finds sub-node for given node maximum (line, column) pair.
98      * @param aNode the root of tree for search.
99      * @return sub-node with maximum (line, column) pair.
100      */

101     private static DetailAST getLastNode(final DetailAST aNode)
102     {
103         DetailAST currentNode = aNode;
104         DetailAST child = (DetailAST) aNode.getFirstChild();
105         while (child != null) {
106             final DetailAST newNode = getLastNode(child);
107             if ((newNode.getLineNo() > currentNode.getLineNo())
108                 || ((newNode.getLineNo() == currentNode.getLineNo())
109                     && (newNode.getColumnNo() > currentNode.getColumnNo())))
110             {
111                 currentNode = newNode;
112             }
113             child = (DetailAST) child.getNextSibling();
114         }
115
116         return currentNode;
117     }
118 }
119
Popular Tags