001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2026 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 /** 060 * Creates a new {@code MultipleVariableDeclarationsCheck} instance. 061 */ 062 public MultipleVariableDeclarationsCheck() { 063 // no code by default 064 } 065 066 @Override 067 public int[] getAcceptableTokens() { 068 return getRequiredTokens(); 069 } 070 071 @Override 072 public int[] getDefaultTokens() { 073 return getRequiredTokens(); 074 } 075 076 @Override 077 public int[] getRequiredTokens() { 078 return new int[] {TokenTypes.VARIABLE_DEF}; 079 } 080 081 @Override 082 public void visitToken(DetailAST ast) { 083 DetailAST nextNode = ast.getNextSibling(); 084 085 if (nextNode != null) { 086 final boolean isCommaSeparated = nextNode.getType() == TokenTypes.COMMA; 087 088 if (isCommaSeparated 089 || nextNode.getType() == TokenTypes.SEMI) { 090 nextNode = nextNode.getNextSibling(); 091 } 092 093 if (nextNode != null 094 && nextNode.getType() == TokenTypes.VARIABLE_DEF) { 095 final DetailAST firstNode = CheckUtil.getFirstNode(ast); 096 if (isCommaSeparated) { 097 // Check if the multiple variable declarations are in a 098 // for loop initializer. If they are, then no warning 099 // should be displayed. Declaring multiple variables in 100 // a for loop initializer is a good way to minimize 101 // variable scope. Refer Feature Request Id - 2895985 102 // for more details 103 if (ast.getParent().getType() != TokenTypes.FOR_INIT) { 104 log(firstNode, MSG_MULTIPLE_COMMA); 105 } 106 } 107 else { 108 final DetailAST lastNode = getLastNode(ast); 109 final DetailAST firstNextNode = CheckUtil.getFirstNode(nextNode); 110 111 if (TokenUtil.areOnSameLine(firstNextNode, lastNode)) { 112 log(firstNode, MSG_MULTIPLE); 113 } 114 } 115 } 116 } 117 } 118 119 /** 120 * Finds sub-node for given node maximum (line, column) pair. 121 * 122 * @param node the root of tree for search. 123 * @return sub-node with maximum (line, column) pair. 124 */ 125 private static DetailAST getLastNode(final DetailAST node) { 126 DetailAST currentNode = node; 127 final DetailAST child = node.getLastChild(); 128 if (child != null) { 129 currentNode = getLastNode(child); 130 } 131 132 return currentNode; 133 } 134 135}