View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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.CommonUtil;
27  import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
28  
29  /**
30   * <div>Checks that chosen statements are not line-wrapped.
31   * By default, this Check restricts wrapping import and package statements,
32   * but it's possible to check any statement.
33   * </div>
34   * <ul>
35   * <li>
36   * Property {@code tokens} - tokens to check
37   * Type is {@code java.lang.String[]}.
38   * Validation type is {@code tokenSet}.
39   * Default value is:
40   * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#PACKAGE_DEF">
41   * PACKAGE_DEF</a>,
42   * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#IMPORT">
43   * IMPORT</a>,
44   * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#STATIC_IMPORT">
45   * STATIC_IMPORT</a>.
46   * </li>
47   * </ul>
48   *
49   * <p>
50   * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
51   * </p>
52   *
53   * <p>
54   * Violation Message Keys:
55   * </p>
56   * <ul>
57   * <li>
58   * {@code no.line.wrap}
59   * </li>
60   * </ul>
61   *
62   * @since 5.8
63   */
64  @StatelessCheck
65  public class NoLineWrapCheck extends AbstractCheck {
66  
67      /**
68       * A key is pointing to the warning message text in "messages.properties"
69       * file.
70       */
71      public static final String MSG_KEY = "no.line.wrap";
72  
73      @Override
74      public int[] getDefaultTokens() {
75          return new int[] {TokenTypes.PACKAGE_DEF, TokenTypes.IMPORT, TokenTypes.STATIC_IMPORT};
76      }
77  
78      @Override
79      public int[] getAcceptableTokens() {
80          return new int[] {
81              TokenTypes.IMPORT,
82              TokenTypes.STATIC_IMPORT,
83              TokenTypes.PACKAGE_DEF,
84              TokenTypes.CLASS_DEF,
85              TokenTypes.METHOD_DEF,
86              TokenTypes.CTOR_DEF,
87              TokenTypes.ENUM_DEF,
88              TokenTypes.INTERFACE_DEF,
89              TokenTypes.RECORD_DEF,
90              TokenTypes.COMPACT_CTOR_DEF,
91          };
92      }
93  
94      @Override
95      public int[] getRequiredTokens() {
96          return CommonUtil.EMPTY_INT_ARRAY;
97      }
98  
99      @Override
100     public void visitToken(DetailAST ast) {
101         if (!TokenUtil.areOnSameLine(ast, ast.getLastChild())) {
102             log(ast, MSG_KEY, ast.getText());
103         }
104     }
105 
106 }