001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2025 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.CheckUtil;
027import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
028
029/**
030 * <div>
031 * Checks that each variable declaration is in its own statement
032 * and on its own line.
033 * </div>
034 *
035 * <p>
036 * Rationale: <a
037 * href="https://checkstyle.org/styleguides/sun-code-conventions-19990420/CodeConventions.doc5.html#a2992">
038 * the Java code conventions chapter 6.1</a> recommends that
039 * declarations should be one per line/statement.
040 * </p>
041 *
042 * @since 3.4
043 */
044@StatelessCheck
045public class MultipleVariableDeclarationsCheck extends AbstractCheck {
046
047    /**
048     * A key is pointing to the warning message text in "messages.properties"
049     * file.
050     */
051    public static final String MSG_MULTIPLE = "multiple.variable.declarations";
052
053    /**
054     * A key is pointing to the warning message text in "messages.properties"
055     * file.
056     */
057    public static final String MSG_MULTIPLE_COMMA = "multiple.variable.declarations.comma";
058
059    @Override
060    public int[] getAcceptableTokens() {
061        return getRequiredTokens();
062    }
063
064    @Override
065    public int[] getDefaultTokens() {
066        return getRequiredTokens();
067    }
068
069    @Override
070    public int[] getRequiredTokens() {
071        return new int[] {TokenTypes.VARIABLE_DEF};
072    }
073
074    @Override
075    public void visitToken(DetailAST ast) {
076        DetailAST nextNode = ast.getNextSibling();
077
078        if (nextNode != null) {
079            final boolean isCommaSeparated = nextNode.getType() == TokenTypes.COMMA;
080
081            if (isCommaSeparated
082                || nextNode.getType() == TokenTypes.SEMI) {
083                nextNode = nextNode.getNextSibling();
084            }
085
086            if (nextNode != null
087                    && nextNode.getType() == TokenTypes.VARIABLE_DEF) {
088                final DetailAST firstNode = CheckUtil.getFirstNode(ast);
089                if (isCommaSeparated) {
090                    // Check if the multiple variable declarations are in a
091                    // for loop initializer. If they are, then no warning
092                    // should be displayed. Declaring multiple variables in
093                    // a for loop initializer is a good way to minimize
094                    // variable scope. Refer Feature Request Id - 2895985
095                    // for more details
096                    if (ast.getParent().getType() != TokenTypes.FOR_INIT) {
097                        log(firstNode, MSG_MULTIPLE_COMMA);
098                    }
099                }
100                else {
101                    final DetailAST lastNode = getLastNode(ast);
102                    final DetailAST firstNextNode = CheckUtil.getFirstNode(nextNode);
103
104                    if (TokenUtil.areOnSameLine(firstNextNode, lastNode)) {
105                        log(firstNode, MSG_MULTIPLE);
106                    }
107                }
108            }
109        }
110    }
111
112    /**
113     * Finds sub-node for given node maximum (line, column) pair.
114     *
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        final DetailAST child = node.getLastChild();
121        if (child != null) {
122            currentNode = getLastNode(child);
123        }
124
125        return currentNode;
126    }
127
128}