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.AnnotationUtil;
027import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
028import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
029
030/**
031 * <div>Checks that chosen statements are not line-wrapped.
032 * By default, this Check restricts wrapping import and package statements,
033 * but it's possible to check any statement.
034 * </div>
035 *
036 * @since 5.8
037 */
038@StatelessCheck
039public class NoLineWrapCheck extends AbstractCheck {
040
041    /**
042     * A key is pointing to the warning message text in "messages.properties"
043     * file.
044     */
045    public static final String MSG_KEY = "no.line.wrap";
046
047    /**
048     * Property that defines whether annotations on the previous line should be
049     * checked as errors.
050     */
051    private boolean skipAnnotations = true;
052
053    /**
054     * Setter to specify whether annotations on the previous line should be
055     * checked as errors.
056     *
057     * @param shouldSkipAnnotations whether to skip annotations on the previous line.
058     * @since 12.3.0
059     */
060    public void setSkipAnnotations(boolean shouldSkipAnnotations) {
061        skipAnnotations = shouldSkipAnnotations;
062    }
063
064    @Override
065    public int[] getDefaultTokens() {
066        return new int[] {TokenTypes.PACKAGE_DEF, TokenTypes.IMPORT, TokenTypes.STATIC_IMPORT};
067    }
068
069    @Override
070    public int[] getAcceptableTokens() {
071        return new int[] {
072            TokenTypes.IMPORT,
073            TokenTypes.STATIC_IMPORT,
074            TokenTypes.PACKAGE_DEF,
075            TokenTypes.CLASS_DEF,
076            TokenTypes.METHOD_DEF,
077            TokenTypes.CTOR_DEF,
078            TokenTypes.ENUM_DEF,
079            TokenTypes.INTERFACE_DEF,
080            TokenTypes.RECORD_DEF,
081            TokenTypes.COMPACT_CTOR_DEF,
082        };
083    }
084
085    @Override
086    public int[] getRequiredTokens() {
087        return CommonUtil.EMPTY_INT_ARRAY;
088    }
089
090    @Override
091    public void visitToken(DetailAST ast) {
092        final boolean isOnSameLine = TokenUtil.areOnSameLine(ast, ast.getLastChild());
093        final boolean containsAnnotation = AnnotationUtil.containsAnnotation(ast);
094
095        if (shouldReportViolation(isOnSameLine, containsAnnotation)) {
096            log(ast, MSG_KEY, ast.getText());
097        }
098    }
099
100    /**
101     * Determines whether a violation should be logged based on the AST node properties.
102     *
103     * @param isOnSameLine     indicates if the AST node and its last child on the same line
104     * @param containsAnnotation indicates if the AST node is annotated with annotation
105     *
106     * @return {@code true} if violation should be logged, {@code false} otherwise
107     */
108    private boolean shouldReportViolation(boolean isOnSameLine, boolean containsAnnotation) {
109        return !isOnSameLine && (!skipAnnotations || !containsAnnotation);
110    }
111
112}