001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2014  Oliver Burn
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018////////////////////////////////////////////////////////////////////////////////
019package com.puppycrawl.tools.checkstyle.checks.coding;
020
021import com.puppycrawl.tools.checkstyle.api.Check;
022import com.puppycrawl.tools.checkstyle.api.DetailAST;
023import com.puppycrawl.tools.checkstyle.api.TokenTypes;
024import com.puppycrawl.tools.checkstyle.checks.CheckUtils;
025
026/**
027 * <p>
028 * Checks that each variable declaration is in its own statement
029 * and on its own line.
030 * </p>
031 * <p>
032 * Rationale: <a
033 * href="http://java.sun.com/docs/codeconv/html/CodeConventions.doc5.html#2991">
034 * the SUN Code conventions chapter 6.1</a> recommends that
035 * declarations should be one per line.
036 * </p>
037 * <p>
038 * An example of how to configure the check is:
039 * </p>
040 * <pre>
041 * &lt;module name="MultipleVariableDeclarations"/&gt;
042 * </pre>
043 * @author o_sukhodolsky
044 */
045public class MultipleVariableDeclarationsCheck extends Check
046{
047    /** Creates new instance of the check. */
048    public MultipleVariableDeclarationsCheck()
049    {
050    }
051
052    @Override
053    public int[] getDefaultTokens()
054    {
055        return new int[] {TokenTypes.VARIABLE_DEF};
056    }
057
058    @Override
059    public void visitToken(DetailAST ast)
060    {
061        DetailAST nextNode = ast.getNextSibling();
062        final boolean isCommaSeparated =
063            ((nextNode != null) && (nextNode.getType() == TokenTypes.COMMA));
064
065        if (nextNode == null) {
066            // no next statement - no check
067            return;
068        }
069
070        if ((nextNode.getType() == TokenTypes.COMMA)
071            || (nextNode.getType() == TokenTypes.SEMI))
072        {
073            nextNode = nextNode.getNextSibling();
074        }
075
076        if ((nextNode != null)
077            && (nextNode.getType() == TokenTypes.VARIABLE_DEF))
078        {
079            final DetailAST firstNode = CheckUtils.getFirstNode(ast);
080            if (isCommaSeparated) {
081                // Check if the multiple variable declarations are in a
082                // for loop initializer. If they are, then no warning
083                // should be displayed. Declaring multiple variables in
084                // a for loop initializer is a good way to minimize
085                // variable scope. Refer Feature Request Id - 2895985
086                // for more details
087                if (ast.getParent().getType() != TokenTypes.FOR_INIT) {
088                    log(firstNode, "multiple.variable.declarations.comma");
089                }
090                return;
091            }
092
093            final DetailAST lastNode = getLastNode(ast);
094            final DetailAST firstNextNode = CheckUtils.getFirstNode(nextNode);
095
096            if (firstNextNode.getLineNo() == lastNode.getLineNo()) {
097                log(firstNode, "multiple.variable.declarations");
098            }
099        }
100
101    }
102
103    /**
104     * Finds sub-node for given node maximum (line, column) pair.
105     * @param node the root of tree for search.
106     * @return sub-node with maximum (line, column) pair.
107     */
108    private static DetailAST getLastNode(final DetailAST node)
109    {
110        DetailAST currentNode = node;
111        DetailAST child = node.getFirstChild();
112        while (child != null) {
113            final DetailAST newNode = getLastNode(child);
114            if ((newNode.getLineNo() > currentNode.getLineNo())
115                || ((newNode.getLineNo() == currentNode.getLineNo())
116                    && (newNode.getColumnNo() > currentNode.getColumnNo())))
117            {
118                currentNode = newNode;
119            }
120            child = child.getNextSibling();
121        }
122
123        return currentNode;
124    }
125}