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