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.coding;
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.JavadocUtil;
28 import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
29
30 /**
31 * <div>
32 * Checks for illegal tokens. By default, labels are prohibited.
33 * </div>
34 *
35 * <p>
36 * Rationale: Certain language features can harm readability, lead to
37 * confusion or are not obvious to novice developers. Other features
38 * may be discouraged in certain frameworks, such as not having
39 * native methods in Enterprise JavaBeans components.
40 * </p>
41 *
42 * @since 3.2
43 */
44 @StatelessCheck
45 public class IllegalTokenCheck
46 extends AbstractCheck {
47
48 /**
49 * A key is pointing to the warning message text in "messages.properties"
50 * file.
51 */
52 public static final String MSG_KEY = "illegal.token";
53
54 /**
55 * Creates a new {@code IllegalTokenCheck} instance.
56 */
57 public IllegalTokenCheck() {
58 // no code by default
59 }
60
61 @Override
62 public int[] getDefaultTokens() {
63 return new int[] {
64 TokenTypes.LABELED_STAT,
65 };
66 }
67
68 @Override
69 public int[] getAcceptableTokens() {
70 return TokenUtil.getAllTokenIds();
71 }
72
73 @Override
74 public int[] getRequiredTokens() {
75 return CommonUtil.EMPTY_INT_ARRAY;
76 }
77
78 @Override
79 public boolean isCommentNodesRequired() {
80 return true;
81 }
82
83 @Override
84 public void visitToken(DetailAST ast) {
85 log(
86 ast,
87 MSG_KEY,
88 convertToString(ast)
89 );
90 }
91
92 /**
93 * Converts given AST node to string representation.
94 *
95 * @param ast node to be represented as string
96 * @return string representation of AST node
97 */
98 private static String convertToString(DetailAST ast) {
99 return switch (ast.getType()) {
100 case TokenTypes.LABELED_STAT -> ast.getFirstChild().getText() + ast.getText();
101 // multiline tokens need to become singlelined
102 case TokenTypes.COMMENT_CONTENT -> JavadocUtil.escapeAllControlChars(ast.getText());
103 default -> ast.getText();
104 };
105 }
106
107 }