001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2024 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.utils.CodePointUtil;
028import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
029
030/**
031 * <p>Abstract class for checking the padding of parentheses. That is whether a
032 * space is required after a left parenthesis and before a right parenthesis,
033 * or such spaces are forbidden.
034 * </p>
035 */
036@StatelessCheck
037public abstract class AbstractParenPadCheck
038    extends AbstractCheck {
039
040    /**
041     * A key is pointing to the warning message text in "messages.properties"
042     * file.
043     */
044    public static final String MSG_WS_FOLLOWED = "ws.followed";
045
046    /**
047     * A key is pointing to the warning message text in "messages.properties"
048     * file.
049     */
050    public static final String MSG_WS_NOT_FOLLOWED = "ws.notFollowed";
051
052    /**
053     * A key is pointing to the warning message text in "messages.properties"
054     * file.
055     */
056    public static final String MSG_WS_PRECEDED = "ws.preceded";
057
058    /**
059     * A key is pointing to the warning message text in "messages.properties"
060     * file.
061     */
062    public static final String MSG_WS_NOT_PRECEDED = "ws.notPreceded";
063
064    /** Open parenthesis literal. */
065    private static final char OPEN_PARENTHESIS = '(';
066
067    /** Close parenthesis literal. */
068    private static final char CLOSE_PARENTHESIS = ')';
069
070    /** The policy to enforce. */
071    private PadOption option = PadOption.NOSPACE;
072
073    /**
074     * Specify policy on how to pad parentheses.
075     *
076     * @param optionStr string to decode option from
077     * @throws IllegalArgumentException if unable to decode
078     */
079    public void setOption(String optionStr) {
080        option = PadOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH));
081    }
082
083    /**
084     * Process a token representing a left parentheses.
085     *
086     * @param ast the token representing a left parentheses
087     */
088    protected void processLeft(DetailAST ast) {
089        final int[] line = getLineCodePoints(ast.getLineNo() - 1);
090        final int after = ast.getColumnNo() + 1;
091
092        if (after < line.length) {
093            final boolean hasWhitespaceAfter =
094                    CommonUtil.isCodePointWhitespace(line, after);
095            if (option == PadOption.NOSPACE && hasWhitespaceAfter) {
096                log(ast, MSG_WS_FOLLOWED, OPEN_PARENTHESIS);
097            }
098            else if (option == PadOption.SPACE && !hasWhitespaceAfter
099                     && line[after] != CLOSE_PARENTHESIS) {
100                log(ast, MSG_WS_NOT_FOLLOWED, OPEN_PARENTHESIS);
101            }
102        }
103    }
104
105    /**
106     * Process a token representing a right parentheses.
107     *
108     * @param ast the token representing a right parentheses
109     */
110    protected void processRight(DetailAST ast) {
111        final int before = ast.getColumnNo() - 1;
112        if (before >= 0) {
113            final int[] line = getLineCodePoints(ast.getLineNo() - 1);
114            final boolean hasPrecedingWhitespace =
115                    CommonUtil.isCodePointWhitespace(line, before);
116
117            if (option == PadOption.NOSPACE && hasPrecedingWhitespace
118                && !CodePointUtil.hasWhitespaceBefore(before, line)) {
119                log(ast, MSG_WS_PRECEDED, CLOSE_PARENTHESIS);
120            }
121            else if (option == PadOption.SPACE && !hasPrecedingWhitespace
122                && line[before] != OPEN_PARENTHESIS) {
123                log(ast, MSG_WS_NOT_PRECEDED, CLOSE_PARENTHESIS);
124            }
125        }
126    }
127
128}