1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2025 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[] {TokenTypes.PACKAGE_DEF, TokenTypes.IMPORT, TokenTypes.STATIC_IMPORT};
49 }
50
51 @Override
52 public int[] getAcceptableTokens() {
53 return new int[] {
54 TokenTypes.IMPORT,
55 TokenTypes.STATIC_IMPORT,
56 TokenTypes.PACKAGE_DEF,
57 TokenTypes.CLASS_DEF,
58 TokenTypes.METHOD_DEF,
59 TokenTypes.CTOR_DEF,
60 TokenTypes.ENUM_DEF,
61 TokenTypes.INTERFACE_DEF,
62 TokenTypes.RECORD_DEF,
63 TokenTypes.COMPACT_CTOR_DEF,
64 };
65 }
66
67 @Override
68 public int[] getRequiredTokens() {
69 return CommonUtil.EMPTY_INT_ARRAY;
70 }
71
72 @Override
73 public void visitToken(DetailAST ast) {
74 if (!TokenUtil.areOnSameLine(ast, ast.getLastChild())) {
75 log(ast, MSG_KEY, ast.getText());
76 }
77 }
78
79 }