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   * <p>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   * </p>
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   * <p>
49   * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
50   * </p>
51   * <p>
52   * Violation Message Keys:
53   * </p>
54   * <ul>
55   * <li>
56   * {@code no.line.wrap}
57   * </li>
58   * </ul>
59   *
60   * @since 5.8
61   */
62  @StatelessCheck
63  public class NoLineWrapCheck extends AbstractCheck {
64  
65      /**
66       * A key is pointing to the warning message text in "messages.properties"
67       * file.
68       */
69      public static final String MSG_KEY = "no.line.wrap";
70  
71      @Override
72      public int[] getDefaultTokens() {
73          return new int[] {TokenTypes.PACKAGE_DEF, TokenTypes.IMPORT, TokenTypes.STATIC_IMPORT};
74      }
75  
76      @Override
77      public int[] getAcceptableTokens() {
78          return new int[] {
79              TokenTypes.IMPORT,
80              TokenTypes.STATIC_IMPORT,
81              TokenTypes.PACKAGE_DEF,
82              TokenTypes.CLASS_DEF,
83              TokenTypes.METHOD_DEF,
84              TokenTypes.CTOR_DEF,
85              TokenTypes.ENUM_DEF,
86              TokenTypes.INTERFACE_DEF,
87              TokenTypes.RECORD_DEF,
88              TokenTypes.COMPACT_CTOR_DEF,
89          };
90      }
91  
92      @Override
93      public int[] getRequiredTokens() {
94          return CommonUtil.EMPTY_INT_ARRAY;
95      }
96  
97      @Override
98      public void visitToken(DetailAST ast) {
99          if (!TokenUtil.areOnSameLine(ast, ast.getLastChild())) {
100             log(ast, MSG_KEY, ast.getText());
101         }
102     }
103 
104 }