View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.checks.whitespace;
21  
22  import com.puppycrawl.tools.checkstyle.StatelessCheck;
23  import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
24  import com.puppycrawl.tools.checkstyle.api.DetailAST;
25  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
26  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
27  
28  /**
29   * <p>Checks that chosen statements are not line-wrapped.
30   * By default this Check restricts wrapping import and package statements,
31   * but it's possible to check any statement.
32   * </p>
33   *
34   * <p>Examples of line-wrapped statements (bad case):
35   * <pre>{@code package com.puppycrawl.
36   *    tools.checkstyle.checks;
37   *
38   * import com.puppycrawl.tools.
39   *    checkstyle.api.AbstractCheck;
40   * }</pre>
41   *
42   * <p>
43   * To configure the check to force no line-wrapping
44   * in package and import statements (default values):
45   * </p>
46   * <pre class="body">
47   * &lt;module name=&quot;NoLineWrap&quot;/&gt;
48   * </pre>
49   *
50   * <p>
51   * To configure the check to force no line-wrapping only
52   * in import statements:
53   * </p>
54   * <pre class="body">
55   * &lt;module name=&quot;NoLineWrap&quot;&gt;
56   *     &lt;property name="tokens" value="IMPORT"/&gt;
57   * &lt;/module&gt;
58   * </pre>
59   *
60   * <p>Examples of not line-wrapped statements (good case):
61   * <pre>{@code import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
62   * }</pre>
63   *
64   * @author maxvetrenko
65   */
66  @StatelessCheck
67  public class NoLineWrapCheck extends AbstractCheck {
68  
69      /**
70       * A key is pointing to the warning message text in "messages.properties"
71       * file.
72       */
73      public static final String MSG_KEY = "no.line.wrap";
74  
75      @Override
76      public int[] getDefaultTokens() {
77          return new int[] {TokenTypes.PACKAGE_DEF, TokenTypes.IMPORT, TokenTypes.STATIC_IMPORT};
78      }
79  
80      @Override
81      public int[] getAcceptableTokens() {
82          return new int[] {
83              TokenTypes.IMPORT,
84              TokenTypes.STATIC_IMPORT,
85              TokenTypes.PACKAGE_DEF,
86              TokenTypes.CLASS_DEF,
87              TokenTypes.METHOD_DEF,
88              TokenTypes.CTOR_DEF,
89              TokenTypes.ENUM_DEF,
90              TokenTypes.INTERFACE_DEF,
91          };
92      }
93  
94      @Override
95      public int[] getRequiredTokens() {
96          return CommonUtils.EMPTY_INT_ARRAY;
97      }
98  
99      @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 }