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