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 com.puppycrawl.tools.checkstyle.StatelessCheck;
023import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
024import com.puppycrawl.tools.checkstyle.api.DetailAST;
025import com.puppycrawl.tools.checkstyle.api.TokenTypes;
026import com.puppycrawl.tools.checkstyle.utils.CodePointUtil;
027import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
028
029/**
030 * <div>
031 * Checks that there is no whitespace before a token.
032 * More specifically, it checks that it is not preceded with whitespace,
033 * or (if linebreaks are allowed) all characters on the line before are
034 * whitespace. To allow linebreaks before a token, set property
035 * {@code allowLineBreaks} to {@code true}. No check occurs before semicolons in empty
036 * for loop initializers or conditions.
037 * </div>
038 *
039 * @since 3.0
040 */
041@StatelessCheck
042public class NoWhitespaceBeforeCheck
043    extends AbstractCheck {
044
045    /**
046     * A key is pointing to the warning message text in "messages.properties"
047     * file.
048     */
049    public static final String MSG_KEY = "ws.preceded";
050
051    /** Control whether whitespace is allowed if the token is at a linebreak. */
052    private boolean allowLineBreaks;
053
054    @Override
055    public int[] getDefaultTokens() {
056        return new int[] {
057            TokenTypes.COMMA,
058            TokenTypes.SEMI,
059            TokenTypes.POST_INC,
060            TokenTypes.POST_DEC,
061            TokenTypes.ELLIPSIS,
062            TokenTypes.LABELED_STAT,
063        };
064    }
065
066    @Override
067    public int[] getAcceptableTokens() {
068        return new int[] {
069            TokenTypes.COMMA,
070            TokenTypes.SEMI,
071            TokenTypes.POST_INC,
072            TokenTypes.POST_DEC,
073            TokenTypes.DOT,
074            TokenTypes.GENERIC_START,
075            TokenTypes.GENERIC_END,
076            TokenTypes.ELLIPSIS,
077            TokenTypes.LABELED_STAT,
078            TokenTypes.METHOD_REF,
079        };
080    }
081
082    @Override
083    public int[] getRequiredTokens() {
084        return CommonUtil.EMPTY_INT_ARRAY;
085    }
086
087    @Override
088    public void visitToken(DetailAST ast) {
089        final int[] line = getLineCodePoints(ast.getLineNo() - 1);
090        final int columnNoBeforeToken = ast.getColumnNo() - 1;
091        final boolean isFirstToken = columnNoBeforeToken == -1;
092
093        if ((isFirstToken || CommonUtil.isCodePointWhitespace(line, columnNoBeforeToken))
094                && !isInEmptyForInitializerOrCondition(ast)) {
095            final boolean isViolation = !allowLineBreaks
096                    || !isFirstToken
097                    && !CodePointUtil.hasWhitespaceBefore(columnNoBeforeToken, line);
098
099            if (isViolation) {
100                log(ast, MSG_KEY, ast.getText());
101            }
102        }
103    }
104
105    /**
106     * Checks that semicolon is in empty for initializer or condition.
107     *
108     * @param semicolonAst DetailAST of semicolon.
109     * @return true if semicolon is in empty for initializer or condition.
110     */
111    private static boolean isInEmptyForInitializerOrCondition(DetailAST semicolonAst) {
112        boolean result = false;
113        final DetailAST sibling = semicolonAst.getPreviousSibling();
114        if (sibling != null
115                && (sibling.getType() == TokenTypes.FOR_INIT
116                        || sibling.getType() == TokenTypes.FOR_CONDITION)
117                && !sibling.hasChildren()) {
118            result = true;
119        }
120        return result;
121    }
122
123    /**
124     * Setter to control whether whitespace is allowed if the token is at a linebreak.
125     *
126     * @param allowLineBreaks whether whitespace should be
127     *     flagged at line breaks.
128     * @since 3.0
129     */
130    public void setAllowLineBreaks(boolean allowLineBreaks) {
131        this.allowLineBreaks = allowLineBreaks;
132    }
133
134}