001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2024 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 * <p>
043 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
044 * </p>
045 *
046 * <p>
047 * Violation Message Keys:
048 * </p>
049 * <ul>
050 * <li>
051 * {@code multiple.variable.declarations}
052 * </li>
053 * <li>
054 * {@code multiple.variable.declarations.comma}
055 * </li>
056 * </ul>
057 *
058 * @since 3.4
059 */
060@StatelessCheck
061public class MultipleVariableDeclarationsCheck extends AbstractCheck {
062
063    /**
064     * A key is pointing to the warning message text in "messages.properties"
065     * file.
066     */
067    public static final String MSG_MULTIPLE = "multiple.variable.declarations";
068
069    /**
070     * A key is pointing to the warning message text in "messages.properties"
071     * file.
072     */
073    public static final String MSG_MULTIPLE_COMMA = "multiple.variable.declarations.comma";
074
075    @Override
076    public int[] getAcceptableTokens() {
077        return getRequiredTokens();
078    }
079
080    @Override
081    public int[] getDefaultTokens() {
082        return getRequiredTokens();
083    }
084
085    @Override
086    public int[] getRequiredTokens() {
087        return new int[] {TokenTypes.VARIABLE_DEF};
088    }
089
090    @Override
091    public void visitToken(DetailAST ast) {
092        DetailAST nextNode = ast.getNextSibling();
093
094        if (nextNode != null) {
095            final boolean isCommaSeparated = nextNode.getType() == TokenTypes.COMMA;
096
097            if (isCommaSeparated
098                || nextNode.getType() == TokenTypes.SEMI) {
099                nextNode = nextNode.getNextSibling();
100            }
101
102            if (nextNode != null
103                    && nextNode.getType() == TokenTypes.VARIABLE_DEF) {
104                final DetailAST firstNode = CheckUtil.getFirstNode(ast);
105                if (isCommaSeparated) {
106                    // Check if the multiple variable declarations are in a
107                    // for loop initializer. If they are, then no warning
108                    // should be displayed. Declaring multiple variables in
109                    // a for loop initializer is a good way to minimize
110                    // variable scope. Refer Feature Request Id - 2895985
111                    // for more details
112                    if (ast.getParent().getType() != TokenTypes.FOR_INIT) {
113                        log(firstNode, MSG_MULTIPLE_COMMA);
114                    }
115                }
116                else {
117                    final DetailAST lastNode = getLastNode(ast);
118                    final DetailAST firstNextNode = CheckUtil.getFirstNode(nextNode);
119
120                    if (TokenUtil.areOnSameLine(firstNextNode, lastNode)) {
121                        log(firstNode, MSG_MULTIPLE);
122                    }
123                }
124            }
125        }
126    }
127
128    /**
129     * Finds sub-node for given node maximum (line, column) pair.
130     *
131     * @param node the root of tree for search.
132     * @return sub-node with maximum (line, column) pair.
133     */
134    private static DetailAST getLastNode(final DetailAST node) {
135        DetailAST currentNode = node;
136        final DetailAST child = node.getLastChild();
137        if (child != null) {
138            currentNode = getLastNode(child);
139        }
140
141        return currentNode;
142    }
143
144}