001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2018 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 *
034 * <p>Examples of line-wrapped statements (bad case):
035 * <pre>{@code package com.puppycrawl.
036 *    tools.checkstyle.checks;
037 *
038 * import com.puppycrawl.tools.
039 *    checkstyle.api.AbstractCheck;
040 * }</pre>
041 *
042 * <p>
043 * To configure the check to force no line-wrapping
044 * in package and import statements (default values):
045 * </p>
046 * <pre class="body">
047 * &lt;module name=&quot;NoLineWrap&quot;/&gt;
048 * </pre>
049 *
050 * <p>
051 * To configure the check to force no line-wrapping only
052 * in import statements:
053 * </p>
054 * <pre class="body">
055 * &lt;module name=&quot;NoLineWrap&quot;&gt;
056 *     &lt;property name="tokens" value="IMPORT"/&gt;
057 * &lt;/module&gt;
058 * </pre>
059 *
060 * <p>Examples of not line-wrapped statements (good case):
061 * <pre>{@code import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
062 * }</pre>
063 *
064 */
065@StatelessCheck
066public class NoLineWrapCheck extends AbstractCheck {
067
068    /**
069     * A key is pointing to the warning message text in "messages.properties"
070     * file.
071     */
072    public static final String MSG_KEY = "no.line.wrap";
073
074    @Override
075    public int[] getDefaultTokens() {
076        return new int[] {TokenTypes.PACKAGE_DEF, TokenTypes.IMPORT, TokenTypes.STATIC_IMPORT};
077    }
078
079    @Override
080    public int[] getAcceptableTokens() {
081        return new int[] {
082            TokenTypes.IMPORT,
083            TokenTypes.STATIC_IMPORT,
084            TokenTypes.PACKAGE_DEF,
085            TokenTypes.CLASS_DEF,
086            TokenTypes.METHOD_DEF,
087            TokenTypes.CTOR_DEF,
088            TokenTypes.ENUM_DEF,
089            TokenTypes.INTERFACE_DEF,
090        };
091    }
092
093    @Override
094    public int[] getRequiredTokens() {
095        return CommonUtil.EMPTY_INT_ARRAY;
096    }
097
098    @Override
099    public void visitToken(DetailAST ast) {
100        if (ast.getLineNo() != ast.getLastChild().getLineNo()) {
101            log(ast.getLineNo(), MSG_KEY, ast.getText());
102        }
103    }
104
105}