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