001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2017 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.whitespace; 021 022import java.util.Locale; 023 024import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 025import com.puppycrawl.tools.checkstyle.api.DetailAST; 026import com.puppycrawl.tools.checkstyle.api.TokenTypes; 027import com.puppycrawl.tools.checkstyle.utils.CommonUtils; 028 029/** 030 * <p> 031 * Checks the padding between the identifier of a method definition, 032 * constructor definition, method call, or constructor invocation; 033 * and the left parenthesis of the parameter list. 034 * That is, if the identifier and left parenthesis are on the same line, 035 * checks whether a space is required immediately after the identifier or 036 * such a space is forbidden. 037 * If they are not on the same line, reports an error, unless configured to 038 * allow line breaks. 039 * </p> 040 * <p> By default the check will check the following tokens: 041 * {@link TokenTypes#CTOR_DEF CTOR_DEF}, 042 * {@link TokenTypes#LITERAL_NEW LITERAL_NEW}, 043 * {@link TokenTypes#METHOD_CALL METHOD_CALL}, 044 * {@link TokenTypes#METHOD_DEF METHOD_DEF}, 045 * {@link TokenTypes#SUPER_CTOR_CALL SUPER_CTOR_CALL}. 046 * </p> 047 * <p> 048 * An example of how to configure the check is: 049 * </p> 050 * <pre> 051 * <module name="MethodParamPad"/> 052 * </pre> 053 * <p> An example of how to configure the check to require a space 054 * after the identifier of a method definition, except if the left 055 * parenthesis occurs on a new line, is: 056 * </p> 057 * <pre> 058 * <module name="MethodParamPad"> 059 * <property name="tokens" value="METHOD_DEF"/> 060 * <property name="option" value="space"/> 061 * <property name="allowLineBreaks" value="true"/> 062 * </module> 063 * </pre> 064 * @author Rick Giles 065 */ 066 067public class MethodParamPadCheck 068 extends AbstractCheck { 069 070 /** 071 * A key is pointing to the warning message text in "messages.properties" 072 * file. 073 */ 074 public static final String MSG_LINE_PREVIOUS = "line.previous"; 075 076 /** 077 * A key is pointing to the warning message text in "messages.properties" 078 * file. 079 */ 080 public static final String MSG_WS_PRECEDED = "ws.preceded"; 081 082 /** 083 * A key is pointing to the warning message text in "messages.properties" 084 * file. 085 */ 086 public static final String MSG_WS_NOT_PRECEDED = "ws.notPreceded"; 087 088 /** 089 * Whether whitespace is allowed if the method identifier is at a 090 * linebreak. 091 */ 092 private boolean allowLineBreaks; 093 094 /** The policy to enforce. */ 095 private PadOption option = PadOption.NOSPACE; 096 097 @Override 098 public int[] getDefaultTokens() { 099 return getAcceptableTokens(); 100 } 101 102 @Override 103 public int[] getAcceptableTokens() { 104 return new int[] { 105 TokenTypes.CTOR_DEF, 106 TokenTypes.LITERAL_NEW, 107 TokenTypes.METHOD_CALL, 108 TokenTypes.METHOD_DEF, 109 TokenTypes.SUPER_CTOR_CALL, 110 TokenTypes.ENUM_CONSTANT_DEF, 111 }; 112 } 113 114 @Override 115 public int[] getRequiredTokens() { 116 return CommonUtils.EMPTY_INT_ARRAY; 117 } 118 119 @Override 120 public void visitToken(DetailAST ast) { 121 final DetailAST parenAST; 122 if (ast.getType() == TokenTypes.METHOD_CALL) { 123 parenAST = ast; 124 } 125 else { 126 parenAST = ast.findFirstToken(TokenTypes.LPAREN); 127 // array construction => parenAST == null 128 } 129 130 if (parenAST != null) { 131 final String line = getLines()[parenAST.getLineNo() - 1]; 132 if (CommonUtils.hasWhitespaceBefore(parenAST.getColumnNo(), line)) { 133 if (!allowLineBreaks) { 134 log(parenAST, MSG_LINE_PREVIOUS, parenAST.getText()); 135 } 136 } 137 else { 138 final int before = parenAST.getColumnNo() - 1; 139 if (option == PadOption.NOSPACE 140 && Character.isWhitespace(line.charAt(before))) { 141 log(parenAST, MSG_WS_PRECEDED, parenAST.getText()); 142 } 143 else if (option == PadOption.SPACE 144 && !Character.isWhitespace(line.charAt(before))) { 145 log(parenAST, MSG_WS_NOT_PRECEDED, parenAST.getText()); 146 } 147 } 148 } 149 } 150 151 /** 152 * Control whether whitespace is flagged at line breaks. 153 * @param allowLineBreaks whether whitespace should be 154 * flagged at line breaks. 155 */ 156 public void setAllowLineBreaks(boolean allowLineBreaks) { 157 this.allowLineBreaks = allowLineBreaks; 158 } 159 160 /** 161 * Set the option to enforce. 162 * @param optionStr string to decode option from 163 * @throws IllegalArgumentException if unable to decode 164 */ 165 public void setOption(String optionStr) { 166 try { 167 option = PadOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH)); 168 } 169 catch (IllegalArgumentException iae) { 170 throw new IllegalArgumentException("unable to parse " + optionStr, iae); 171 } 172 } 173}