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.CommonUtil;
027import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
028
029/**
030 * <div>Checks that chosen statements are not line-wrapped.
031 * By default, this Check restricts wrapping import and package statements,
032 * but it's possible to check any statement.
033 * </div>
034 *
035 * @since 5.8
036 */
037@StatelessCheck
038public class NoLineWrapCheck 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_KEY = "no.line.wrap";
045
046    @Override
047    public int[] getDefaultTokens() {
048        return new int[] {TokenTypes.PACKAGE_DEF, TokenTypes.IMPORT, TokenTypes.STATIC_IMPORT};
049    }
050
051    @Override
052    public int[] getAcceptableTokens() {
053        return new int[] {
054            TokenTypes.IMPORT,
055            TokenTypes.STATIC_IMPORT,
056            TokenTypes.PACKAGE_DEF,
057            TokenTypes.CLASS_DEF,
058            TokenTypes.METHOD_DEF,
059            TokenTypes.CTOR_DEF,
060            TokenTypes.ENUM_DEF,
061            TokenTypes.INTERFACE_DEF,
062            TokenTypes.RECORD_DEF,
063            TokenTypes.COMPACT_CTOR_DEF,
064        };
065    }
066
067    @Override
068    public int[] getRequiredTokens() {
069        return CommonUtil.EMPTY_INT_ARRAY;
070    }
071
072    @Override
073    public void visitToken(DetailAST ast) {
074        if (!TokenUtil.areOnSameLine(ast, ast.getLastChild())) {
075            log(ast, MSG_KEY, ast.getText());
076        }
077    }
078
079}