001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2018 the original author or authors.
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////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.checks.coding;
021
022import com.puppycrawl.tools.checkstyle.StatelessCheck;
023import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
024import com.puppycrawl.tools.checkstyle.api.DetailAST;
025import com.puppycrawl.tools.checkstyle.api.TokenTypes;
026import com.puppycrawl.tools.checkstyle.utils.CheckUtils;
027
028/**
029 * <p>
030 * Checks that each variable declaration is in its own statement
031 * and on its own line.
032 * </p>
033 * <p>
034 * Rationale: <a
035 * href="http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-141270.html">
036 * the SUN Code conventions chapter 6.1</a> recommends that
037 * declarations should be one per line.
038 * </p>
039 * <p>
040 * An example of how to configure the check is:
041 * </p>
042 * <pre>
043 * &lt;module name="MultipleVariableDeclarations"/&gt;
044 * </pre>
045 * @author o_sukhodolsky
046 */
047@StatelessCheck
048public class MultipleVariableDeclarationsCheck extends AbstractCheck {
049
050    /**
051     * A key is pointing to the warning message text in "messages.properties"
052     * file.
053     */
054    public static final String MSG_MULTIPLE = "multiple.variable.declarations";
055
056    /**
057     * A key is pointing to the warning message text in "messages.properties"
058     * file.
059     */
060    public static final String MSG_MULTIPLE_COMMA = "multiple.variable.declarations.comma";
061
062    @Override
063    public int[] getAcceptableTokens() {
064        return getRequiredTokens();
065    }
066
067    @Override
068    public int[] getDefaultTokens() {
069        return getRequiredTokens();
070    }
071
072    @Override
073    public int[] getRequiredTokens() {
074        return new int[] {TokenTypes.VARIABLE_DEF};
075    }
076
077    @Override
078    public void visitToken(DetailAST ast) {
079        DetailAST nextNode = ast.getNextSibling();
080
081        if (nextNode != null) {
082            final boolean isCommaSeparated = nextNode.getType() == TokenTypes.COMMA;
083
084            if (isCommaSeparated
085                || nextNode.getType() == TokenTypes.SEMI) {
086                nextNode = nextNode.getNextSibling();
087            }
088
089            if (nextNode != null
090                    && nextNode.getType() == TokenTypes.VARIABLE_DEF) {
091                final DetailAST firstNode = CheckUtils.getFirstNode(ast);
092                if (isCommaSeparated) {
093                    // Check if the multiple variable declarations are in a
094                    // for loop initializer. If they are, then no warning
095                    // should be displayed. Declaring multiple variables in
096                    // a for loop initializer is a good way to minimize
097                    // variable scope. Refer Feature Request Id - 2895985
098                    // for more details
099                    if (ast.getParent().getType() != TokenTypes.FOR_INIT) {
100                        log(firstNode, MSG_MULTIPLE_COMMA);
101                    }
102                }
103                else {
104                    final DetailAST lastNode = getLastNode(ast);
105                    final DetailAST firstNextNode = CheckUtils.getFirstNode(nextNode);
106
107                    if (firstNextNode.getLineNo() == lastNode.getLineNo()) {
108                        log(firstNode, MSG_MULTIPLE);
109                    }
110                }
111            }
112        }
113    }
114
115    /**
116     * Finds sub-node for given node maximum (line, column) pair.
117     * @param node the root of tree for search.
118     * @return sub-node with maximum (line, column) pair.
119     */
120    private static DetailAST getLastNode(final DetailAST node) {
121        DetailAST currentNode = node;
122        DetailAST child = node.getFirstChild();
123        while (child != null) {
124            final DetailAST newNode = getLastNode(child);
125            if (newNode.getLineNo() > currentNode.getLineNo()
126                || newNode.getLineNo() == currentNode.getLineNo()
127                    && newNode.getColumnNo() > currentNode.getColumnNo()) {
128                currentNode = newNode;
129            }
130            child = child.getNextSibling();
131        }
132
133        return currentNode;
134    }
135
136}