View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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.AnnotationUtil;
27  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
28  import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
29  
30  /**
31   * <div>Checks that chosen statements are not line-wrapped.
32   * By default, this Check restricts wrapping import and package statements,
33   * but it's possible to check any statement.
34   * </div>
35   *
36   * @since 5.8
37   */
38  @StatelessCheck
39  public class NoLineWrapCheck extends AbstractCheck {
40  
41      /**
42       * A key is pointing to the warning message text in "messages.properties"
43       * file.
44       */
45      public static final String MSG_KEY = "no.line.wrap";
46  
47      /**
48       * Property that defines whether annotations on the previous line should be
49       * checked as violation.
50       */
51      private boolean skipAnnotations = true;
52  
53      /**
54       * Creates a new {@code NoLineWrapCheck} instance.
55       */
56      public NoLineWrapCheck() {
57          // no code by default
58      }
59  
60      /**
61       * Setter to specify whether to skip annotations to be part of target token.
62       *
63       * @param shouldSkipAnnotations whether to skip annotations to be part of target token.
64       * @since 13.9.0
65       */
66      public void setSkipAnnotations(boolean shouldSkipAnnotations) {
67          skipAnnotations = shouldSkipAnnotations;
68      }
69  
70      @Override
71      public int[] getDefaultTokens() {
72          return new int[] {
73              TokenTypes.PACKAGE_DEF,
74              TokenTypes.IMPORT,
75              TokenTypes.STATIC_IMPORT,
76              TokenTypes.MODULE_IMPORT,
77              TokenTypes.MODULE_DEF,
78          };
79      }
80  
81      @Override
82      public int[] getAcceptableTokens() {
83          return new int[] {
84              TokenTypes.IMPORT,
85              TokenTypes.STATIC_IMPORT,
86              TokenTypes.MODULE_IMPORT,
87              TokenTypes.PACKAGE_DEF,
88              TokenTypes.CLASS_DEF,
89              TokenTypes.METHOD_DEF,
90              TokenTypes.CTOR_DEF,
91              TokenTypes.ENUM_DEF,
92              TokenTypes.INTERFACE_DEF,
93              TokenTypes.RECORD_DEF,
94              TokenTypes.COMPACT_CTOR_DEF,
95              TokenTypes.MODULE_DEF,
96          };
97      }
98  
99      @Override
100     public int[] getRequiredTokens() {
101         return CommonUtil.EMPTY_INT_ARRAY;
102     }
103 
104     @Override
105     public void visitToken(DetailAST ast) {
106         DetailAST detailAST = ast;
107         if (skipAnnotations && AnnotationUtil.containsAnnotation(ast)) {
108             detailAST = AnnotationUtil.getAnnotationHolder(ast).getNextSibling();
109         }
110         if (!TokenUtil.areOnSameLine(detailAST, ast.getLastChild())) {
111             log(ast, MSG_KEY, ast.getText());
112         }
113     }
114 
115 }