001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2019 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;
027
028/**
029 * <p>Checks that chosen statements are not line-wrapped.
030 * By default this Check restricts wrapping import and package statements,
031 * but it's possible to check any statement.
032 * </p>
033 * <ul>
034 * <li>
035 * Property {@code tokens} - tokens to check
036 * Default value is:
037 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#PACKAGE_DEF">
038 * PACKAGE_DEF</a>,
039 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#IMPORT">
040 * IMPORT</a>,
041 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#STATIC_IMPORT">
042 * STATIC_IMPORT</a>.
043 * </li>
044 * </ul>
045 * <p>Examples of line-wrapped statements (bad case):
046 * </p>
047 * <pre>
048 * package com.puppycrawl. //violation
049 *     tools.checkstyle.checks;
050 *
051 * import com.puppycrawl.tools. //violation
052 *     checkstyle.api.AbstractCheck;
053 *
054 * import static java.math. //violation
055 *     BigInteger.ZERO;
056 * </pre>
057 *
058 * <p>
059 * To configure the check to force no line-wrapping
060 * in package and import statements (default values):
061 * </p>
062 * <pre>
063 * &lt;module name=&quot;NoLineWrap&quot;/&gt;
064 * </pre>
065 *
066 * <p>
067 * To configure the check to force no line-wrapping only
068 * in import statements:
069 * </p>
070 * <pre>
071 * &lt;module name=&quot;NoLineWrap&quot;&gt;
072 *   &lt;property name="tokens" value="IMPORT"/&gt;
073 * &lt;/module&gt;
074 * </pre>
075 *
076 * <p>Examples of not line-wrapped statements (good case):
077 * </p>
078 * <pre>
079 * import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
080 * import static java.math.BigInteger.ZERO;
081 * </pre>
082 *
083 * @since 5.8
084 */
085@StatelessCheck
086public class NoLineWrapCheck extends AbstractCheck {
087
088    /**
089     * A key is pointing to the warning message text in "messages.properties"
090     * file.
091     */
092    public static final String MSG_KEY = "no.line.wrap";
093
094    @Override
095    public int[] getDefaultTokens() {
096        return new int[] {TokenTypes.PACKAGE_DEF, TokenTypes.IMPORT, TokenTypes.STATIC_IMPORT};
097    }
098
099    @Override
100    public int[] getAcceptableTokens() {
101        return new int[] {
102            TokenTypes.IMPORT,
103            TokenTypes.STATIC_IMPORT,
104            TokenTypes.PACKAGE_DEF,
105            TokenTypes.CLASS_DEF,
106            TokenTypes.METHOD_DEF,
107            TokenTypes.CTOR_DEF,
108            TokenTypes.ENUM_DEF,
109            TokenTypes.INTERFACE_DEF,
110        };
111    }
112
113    @Override
114    public int[] getRequiredTokens() {
115        return CommonUtil.EMPTY_INT_ARRAY;
116    }
117
118    @Override
119    public void visitToken(DetailAST ast) {
120        if (ast.getLineNo() != ast.getLastChild().getLineNo()) {
121            log(ast.getLineNo(), MSG_KEY, ast.getText());
122        }
123    }
124
125}