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.CommonUtils;
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 * @author maxvetrenko
065 */
066@StatelessCheck
067public class NoLineWrapCheck extends AbstractCheck {
068
069    /**
070     * A key is pointing to the warning message text in "messages.properties"
071     * file.
072     */
073    public static final String MSG_KEY = "no.line.wrap";
074
075    @Override
076    public int[] getDefaultTokens() {
077        return new int[] {TokenTypes.PACKAGE_DEF, TokenTypes.IMPORT, TokenTypes.STATIC_IMPORT};
078    }
079
080    @Override
081    public int[] getAcceptableTokens() {
082        return new int[] {
083            TokenTypes.IMPORT,
084            TokenTypes.STATIC_IMPORT,
085            TokenTypes.PACKAGE_DEF,
086            TokenTypes.CLASS_DEF,
087            TokenTypes.METHOD_DEF,
088            TokenTypes.CTOR_DEF,
089            TokenTypes.ENUM_DEF,
090            TokenTypes.INTERFACE_DEF,
091        };
092    }
093
094    @Override
095    public int[] getRequiredTokens() {
096        return CommonUtils.EMPTY_INT_ARRAY;
097    }
098
099    @Override
100    public void visitToken(DetailAST ast) {
101        if (ast.getLineNo() != ast.getLastChild().getLineNo()) {
102            log(ast.getLineNo(), MSG_KEY, ast.getText());
103        }
104    }
105
106}