1 //////////////////////////////////////////////////////////////////////////////// 2 // checkstyle: Checks Java source code for adherence to a set of rules. 3 // Copyright (C) 2001-2019 the original author or authors. 4 // 5 // This library is free software; you can redistribute it and/or 6 // modify it under the terms of the GNU Lesser General Public 7 // License as published by the Free Software Foundation; either 8 // version 2.1 of the License, or (at your option) any later version. 9 // 10 // This library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 // Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public 16 // License along with this library; if not, write to the Free Software 17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 //////////////////////////////////////////////////////////////////////////////// 19 20 package com.puppycrawl.tools.checkstyle.checks.whitespace; 21 22 import java.util.Locale; 23 24 import com.puppycrawl.tools.checkstyle.StatelessCheck; 25 import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 26 import com.puppycrawl.tools.checkstyle.api.DetailAST; 27 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 28 import com.puppycrawl.tools.checkstyle.utils.CommonUtil; 29 30 /** 31 * <p> 32 * Checks the padding between the identifier of a method definition, 33 * constructor definition, method call, or constructor invocation; 34 * and the left parenthesis of the parameter list. 35 * That is, if the identifier and left parenthesis are on the same line, 36 * checks whether a space is required immediately after the identifier or 37 * such a space is forbidden. 38 * If they are not on the same line, reports an error, unless configured to 39 * allow line breaks. 40 * </p> 41 * <p> By default the check will check the following tokens: 42 * {@link TokenTypes#CTOR_DEF CTOR_DEF}, 43 * {@link TokenTypes#LITERAL_NEW LITERAL_NEW}, 44 * {@link TokenTypes#METHOD_CALL METHOD_CALL}, 45 * {@link TokenTypes#METHOD_DEF METHOD_DEF}, 46 * {@link TokenTypes#SUPER_CTOR_CALL SUPER_CTOR_CALL}. 47 * </p> 48 * <p> 49 * An example of how to configure the check is: 50 * </p> 51 * <pre> 52 * <module name="MethodParamPad"/> 53 * </pre> 54 * <p> An example of how to configure the check to require a space 55 * after the identifier of a method definition, except if the left 56 * parenthesis occurs on a new line, is: 57 * </p> 58 * <pre> 59 * <module name="MethodParamPad"> 60 * <property name="tokens" value="METHOD_DEF"/> 61 * <property name="option" value="space"/> 62 * <property name="allowLineBreaks" value="true"/> 63 * </module> 64 * </pre> 65 */ 66 67 @StatelessCheck 68 public class MethodParamPadCheck 69 extends AbstractCheck { 70 71 /** 72 * A key is pointing to the warning message text in "messages.properties" 73 * file. 74 */ 75 public static final String MSG_LINE_PREVIOUS = "line.previous"; 76 77 /** 78 * A key is pointing to the warning message text in "messages.properties" 79 * file. 80 */ 81 public static final String MSG_WS_PRECEDED = "ws.preceded"; 82 83 /** 84 * A key is pointing to the warning message text in "messages.properties" 85 * file. 86 */ 87 public static final String MSG_WS_NOT_PRECEDED = "ws.notPreceded"; 88 89 /** 90 * Whether whitespace is allowed if the method identifier is at a 91 * linebreak. 92 */ 93 private boolean allowLineBreaks; 94 95 /** The policy to enforce. */ 96 private PadOption option = PadOption.NOSPACE; 97 98 @Override 99 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 option = PadOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH)); 168 } 169 170 }