001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2014  Oliver Burn
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////////////////////////////////////////////////////////////////////////////////
019package com.puppycrawl.tools.checkstyle.checks.whitespace;
020
021import com.puppycrawl.tools.checkstyle.api.Check;
022import com.puppycrawl.tools.checkstyle.api.DetailAST;
023import com.puppycrawl.tools.checkstyle.api.TokenTypes;
024
025/**
026 * <p>Checks that chosen statements are not line-wrapped.
027 * By default this Check restricts wrapping import and package statements,
028 * but it's possible to check any statement.
029 * </p>
030 *
031 * Examples
032 * <p class="body">
033 *
034 * 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 * </code></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 * Examples of not line-wrapped statements (good case):
061 * <pre><code> import com.puppycrawl.tools.checkstyle.api.Check;
062 * </code></pre>
063 *
064 * @author maxvetrenko
065 */
066public class NoLineWrapCheck extends Check
067{
068
069    @Override
070    public int[] getDefaultTokens()
071    {
072        return new int[] {TokenTypes.PACKAGE_DEF, TokenTypes.IMPORT};
073    }
074
075    @Override
076    public int[] getAcceptableTokens()
077    {
078        return new int[] {
079            TokenTypes.IMPORT,
080            TokenTypes.PACKAGE_DEF,
081            TokenTypes.CLASS_DEF,
082            TokenTypes.METHOD_DEF,
083            TokenTypes.CTOR_DEF,
084            TokenTypes.ENUM_DEF,
085            TokenTypes.INTERFACE_DEF,
086        };
087    }
088
089    @Override
090    public void visitToken(DetailAST ast)
091    {
092        if (ast.getLineNo() != ast.getLastChild().getLineNo()) {
093            log(ast.getLineNo(), "no.line.wrap", ast.getText());
094        }
095    }
096}