001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2026 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.CodePointUtil; 029import com.puppycrawl.tools.checkstyle.utils.CommonUtil; 030 031/** 032 * <div> 033 * Checks the padding between the identifier of a method definition, 034 * constructor definition, method call, constructor invocation, record, or record pattern; 035 * and the left parenthesis of the parameter list. 036 * That is, if the identifier and left parenthesis are on the same line, 037 * checks whether a space is required immediately after the identifier or such a space is forbidden. 038 * If they are not on the same line, reports a violation, unless configured to 039 * allow line breaks. To allow linebreaks after the identifier, set property 040 * {@code allowLineBreaks} to {@code true}. 041 * </div> 042 * 043 * @since 3.4 044 */ 045 046@StatelessCheck 047public class MethodParamPadCheck extends AbstractCheck { 048 049 /** 050 * A key is pointing to the warning message text in "messages.properties" file. 051 */ 052 public static final String MSG_LINE_PREVIOUS = "line.previous"; 053 054 /** 055 * A key is pointing to the warning message text in "messages.properties" file. 056 */ 057 public static final String MSG_WS_PRECEDED = "ws.preceded"; 058 059 /** 060 * A key is pointing to the warning message text in "messages.properties" file. 061 */ 062 public static final String MSG_WS_NOT_PRECEDED = "ws.notPreceded"; 063 064 /** 065 * Allow a line break between the identifier and left parenthesis. 066 */ 067 private boolean allowLineBreaks; 068 069 /** Specify policy on how to pad method parameter. */ 070 private PadOption option = PadOption.NOSPACE; 071 072 /** 073 * Creates a new {@code MethodParamPadCheck} instance. 074 */ 075 public MethodParamPadCheck() { 076 // no code by default 077 } 078 079 @Override 080 public int[] getDefaultTokens() { 081 return getAcceptableTokens(); 082 } 083 084 @Override 085 public int[] getAcceptableTokens() { 086 return new int[] { 087 TokenTypes.CTOR_DEF, 088 TokenTypes.CTOR_CALL, 089 TokenTypes.LITERAL_NEW, 090 TokenTypes.METHOD_CALL, 091 TokenTypes.METHOD_DEF, 092 TokenTypes.SUPER_CTOR_CALL, 093 TokenTypes.ENUM_CONSTANT_DEF, 094 TokenTypes.RECORD_DEF, 095 TokenTypes.RECORD_PATTERN_DEF, 096 }; 097 } 098 099 @Override 100 public int[] getRequiredTokens() { 101 return CommonUtil.EMPTY_INT_ARRAY; 102 } 103 104 @Override 105 public void visitToken(DetailAST ast) { 106 final DetailAST parenAST; 107 if (ast.getType() == TokenTypes.METHOD_CALL) { 108 parenAST = ast; 109 } 110 else { 111 parenAST = ast.findFirstToken(TokenTypes.LPAREN); 112 // array construction => parenAST == null 113 } 114 115 if (parenAST != null) { 116 final int[] line = getLineCodePoints(parenAST.getLineNo() - 1); 117 if (CodePointUtil.hasWhitespaceBefore(parenAST.getColumnNo(), line)) { 118 if (!allowLineBreaks) { 119 log(parenAST, MSG_LINE_PREVIOUS, parenAST.getText()); 120 } 121 } 122 else { 123 final int before = parenAST.getColumnNo() - 1; 124 if (option == PadOption.NOSPACE 125 && CommonUtil.isCodePointWhitespace(line, before)) { 126 log(parenAST, MSG_WS_PRECEDED, parenAST.getText()); 127 } 128 else if (option == PadOption.SPACE 129 && !CommonUtil.isCodePointWhitespace(line, before)) { 130 log(parenAST, MSG_WS_NOT_PRECEDED, parenAST.getText()); 131 } 132 } 133 } 134 } 135 136 /** 137 * Setter to allow a line break between the identifier and left parenthesis. 138 * 139 * @param allowLineBreaks whether whitespace should be flagged at line breaks. 140 * @since 3.4 141 */ 142 public void setAllowLineBreaks(boolean allowLineBreaks) { 143 this.allowLineBreaks = allowLineBreaks; 144 } 145 146 /** 147 * Setter to specify policy on how to pad method parameter. 148 * 149 * @param optionStr string to decode option from 150 * @throws IllegalArgumentException if unable to decode 151 * @since 3.4 152 */ 153 public void setOption(String optionStr) { 154 option = PadOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH)); 155 } 156 157}