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.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 *
35 * @since 5.8
36 */
37 @StatelessCheck
38 public class NoLineWrapCheck extends AbstractCheck {
39
40 /**
41 * A key is pointing to the warning message text in "messages.properties"
42 * file.
43 */
44 public static final String MSG_KEY = "no.line.wrap";
45
46 @Override
47 public int[] getDefaultTokens() {
48 return new int[] {
49 TokenTypes.PACKAGE_DEF,
50 TokenTypes.IMPORT,
51 TokenTypes.STATIC_IMPORT,
52 TokenTypes.MODULE_IMPORT,
53 };
54 }
55
56 @Override
57 public int[] getAcceptableTokens() {
58 return new int[] {
59 TokenTypes.IMPORT,
60 TokenTypes.STATIC_IMPORT,
61 TokenTypes.MODULE_IMPORT,
62 TokenTypes.PACKAGE_DEF,
63 TokenTypes.CLASS_DEF,
64 TokenTypes.METHOD_DEF,
65 TokenTypes.CTOR_DEF,
66 TokenTypes.ENUM_DEF,
67 TokenTypes.INTERFACE_DEF,
68 TokenTypes.RECORD_DEF,
69 TokenTypes.COMPACT_CTOR_DEF,
70 };
71 }
72
73 @Override
74 public int[] getRequiredTokens() {
75 return CommonUtil.EMPTY_INT_ARRAY;
76 }
77
78 @Override
79 public void visitToken(DetailAST ast) {
80 if (!TokenUtil.areOnSameLine(ast, ast.getLastChild())) {
81 log(ast, MSG_KEY, ast.getText());
82 }
83 }
84
85 }