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
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    /**
078     * Creates a new {@code MethodParamPadCheck} instance.
079     */
080    public MethodParamPadCheck() {
081        // no code by default
082    }
083
084    @Override
085    public int[] getDefaultTokens() {
086        return getAcceptableTokens();
087    }
088
089    @Override
090    public int[] getAcceptableTokens() {
091        return new int[] {
092            TokenTypes.CTOR_DEF,
093            TokenTypes.CTOR_CALL,
094            TokenTypes.LITERAL_NEW,
095            TokenTypes.METHOD_CALL,
096            TokenTypes.METHOD_DEF,
097            TokenTypes.SUPER_CTOR_CALL,
098            TokenTypes.ENUM_CONSTANT_DEF,
099            TokenTypes.RECORD_DEF,
100            TokenTypes.RECORD_PATTERN_DEF,
101        };
102    }
103
104    @Override
105    public int[] getRequiredTokens() {
106        return CommonUtil.EMPTY_INT_ARRAY;
107    }
108
109    @Override
110    public void visitToken(DetailAST ast) {
111        final DetailAST parenAST;
112        if (ast.getType() == TokenTypes.METHOD_CALL) {
113            parenAST = ast;
114        }
115        else {
116            parenAST = ast.findFirstToken(TokenTypes.LPAREN);
117            // array construction => parenAST == null
118        }
119
120        if (parenAST != null) {
121            final int[] line = getLineCodePoints(parenAST.getLineNo() - 1);
122            if (CodePointUtil.hasWhitespaceBefore(parenAST.getColumnNo(), line)) {
123                if (!allowLineBreaks) {
124                    log(parenAST, MSG_LINE_PREVIOUS, parenAST.getText());
125                }
126            }
127            else {
128                final int before = parenAST.getColumnNo() - 1;
129                if (option == PadOption.NOSPACE
130                    && CommonUtil.isCodePointWhitespace(line, before)) {
131                    log(parenAST, MSG_WS_PRECEDED, parenAST.getText());
132                }
133                else if (option == PadOption.SPACE
134                         && !CommonUtil.isCodePointWhitespace(line, before)) {
135                    log(parenAST, MSG_WS_NOT_PRECEDED, parenAST.getText());
136                }
137            }
138        }
139    }
140
141    /**
142     * Setter to allow a line break between the identifier and left parenthesis.
143     *
144     * @param allowLineBreaks whether whitespace should be
145     *     flagged at line breaks.
146     * @since 3.4
147     */
148    public void setAllowLineBreaks(boolean allowLineBreaks) {
149        this.allowLineBreaks = allowLineBreaks;
150    }
151
152    /**
153     * Setter to specify policy on how to pad method parameter.
154     *
155     * @param optionStr string to decode option from
156     * @throws IllegalArgumentException if unable to decode
157     * @since 3.4
158     */
159    public void setOption(String optionStr) {
160        option = PadOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH));
161    }
162
163}