001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2015 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.Check; 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 * <module name="MultipleVariableDeclarations"/> 043 * </pre> 044 * @author o_sukhodolsky 045 */ 046public class MultipleVariableDeclarationsCheck extends Check { 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 // no next statement - no check 081 return; 082 } 083 084 final boolean isCommaSeparated = nextNode.getType() == TokenTypes.COMMA; 085 086 if (isCommaSeparated 087 || nextNode.getType() == TokenTypes.SEMI) { 088 nextNode = nextNode.getNextSibling(); 089 } 090 091 if (nextNode != null 092 && nextNode.getType() == TokenTypes.VARIABLE_DEF) { 093 final DetailAST firstNode = CheckUtils.getFirstNode(ast); 094 if (isCommaSeparated) { 095 // Check if the multiple variable declarations are in a 096 // for loop initializer. If they are, then no warning 097 // should be displayed. Declaring multiple variables in 098 // a for loop initializer is a good way to minimize 099 // variable scope. Refer Feature Request Id - 2895985 100 // for more details 101 if (ast.getParent().getType() != TokenTypes.FOR_INIT) { 102 log(firstNode, MSG_MULTIPLE_COMMA); 103 } 104 return; 105 } 106 107 final DetailAST lastNode = getLastNode(ast); 108 final DetailAST firstNextNode = CheckUtils.getFirstNode(nextNode); 109 110 if (firstNextNode.getLineNo() == lastNode.getLineNo()) { 111 log(firstNode, MSG_MULTIPLE); 112 } 113 } 114 115 } 116 117 /** 118 * Finds sub-node for given node maximum (line, column) pair. 119 * @param node the root of tree for search. 120 * @return sub-node with maximum (line, column) pair. 121 */ 122 private static DetailAST getLastNode(final DetailAST node) { 123 DetailAST currentNode = node; 124 DetailAST child = node.getFirstChild(); 125 while (child != null) { 126 final DetailAST newNode = getLastNode(child); 127 if (newNode.getLineNo() > currentNode.getLineNo() 128 || newNode.getLineNo() == currentNode.getLineNo() 129 && newNode.getColumnNo() > currentNode.getColumnNo()) { 130 currentNode = newNode; 131 } 132 child = child.getNextSibling(); 133 } 134 135 return currentNode; 136 } 137}