001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2015 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 org.apache.commons.lang3.ArrayUtils;
023
024import com.puppycrawl.tools.checkstyle.api.Check;
025import com.puppycrawl.tools.checkstyle.api.DetailAST;
026import com.puppycrawl.tools.checkstyle.api.TokenTypes;
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.Check;
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.Check;
062 * }</pre>
063 *
064 * @author maxvetrenko
065 */
066public class NoLineWrapCheck extends Check {
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};
077    }
078
079    @Override
080    public int[] getAcceptableTokens() {
081        return new int[] {
082            TokenTypes.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 ArrayUtils.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}