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