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