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 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 * <p>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 * </p>
034 * <ul>
035 * <li>
036 * Property {@code tokens} - tokens to check
037 * Type is {@code java.lang.String[]}.
038 * Validation type is {@code tokenSet}.
039 * Default value is:
040 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#PACKAGE_DEF">
041 * PACKAGE_DEF</a>,
042 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#IMPORT">
043 * IMPORT</a>,
044 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#STATIC_IMPORT">
045 * STATIC_IMPORT</a>.
046 * </li>
047 * </ul>
048 * <p>
049 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
050 * </p>
051 * <p>
052 * Violation Message Keys:
053 * </p>
054 * <ul>
055 * <li>
056 * {@code no.line.wrap}
057 * </li>
058 * </ul>
059 *
060 * @since 5.8
061 */
062@StatelessCheck
063public class NoLineWrapCheck extends AbstractCheck {
064
065    /**
066     * A key is pointing to the warning message text in "messages.properties"
067     * file.
068     */
069    public static final String MSG_KEY = "no.line.wrap";
070
071    @Override
072    public int[] getDefaultTokens() {
073        return new int[] {TokenTypes.PACKAGE_DEF, TokenTypes.IMPORT, TokenTypes.STATIC_IMPORT};
074    }
075
076    @Override
077    public int[] getAcceptableTokens() {
078        return new int[] {
079            TokenTypes.IMPORT,
080            TokenTypes.STATIC_IMPORT,
081            TokenTypes.PACKAGE_DEF,
082            TokenTypes.CLASS_DEF,
083            TokenTypes.METHOD_DEF,
084            TokenTypes.CTOR_DEF,
085            TokenTypes.ENUM_DEF,
086            TokenTypes.INTERFACE_DEF,
087            TokenTypes.RECORD_DEF,
088            TokenTypes.COMPACT_CTOR_DEF,
089        };
090    }
091
092    @Override
093    public int[] getRequiredTokens() {
094        return CommonUtil.EMPTY_INT_ARRAY;
095    }
096
097    @Override
098    public void visitToken(DetailAST ast) {
099        if (!TokenUtil.areOnSameLine(ast, ast.getLastChild())) {
100            log(ast, MSG_KEY, ast.getText());
101        }
102    }
103
104}