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.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 * <p>
32 * Checks for illegal tokens. By default, labels are prohibited.
33 * </p>
34 * <p>
35 * Rationale: Certain language features can harm readability, lead to
36 * confusion or are not obvious to novice developers. Other features
37 * may be discouraged in certain frameworks, such as not having
38 * native methods in Enterprise JavaBeans components.
39 * </p>
40 * <ul>
41 * <li>
42 * Property {@code tokens} - tokens to check
43 * Type is {@code anyTokenTypesSet}.
44 * Default value is
45 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LABELED_STAT">
46 * LABELED_STAT</a>.
47 * </li>
48 * </ul>
49 * <p>
50 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
51 * </p>
52 * <p>
53 * Violation Message Keys:
54 * </p>
55 * <ul>
56 * <li>
57 * {@code illegal.token}
58 * </li>
59 * </ul>
60 *
61 * @since 3.2
62 */
63 @StatelessCheck
64 public class IllegalTokenCheck
65 extends AbstractCheck {
66
67 /**
68 * A key is pointing to the warning message text in "messages.properties"
69 * file.
70 */
71 public static final String MSG_KEY = "illegal.token";
72
73 @Override
74 public int[] getDefaultTokens() {
75 return new int[] {
76 TokenTypes.LABELED_STAT,
77 };
78 }
79
80 @Override
81 public int[] getAcceptableTokens() {
82 return TokenUtil.getAllTokenIds();
83 }
84
85 @Override
86 public int[] getRequiredTokens() {
87 return CommonUtil.EMPTY_INT_ARRAY;
88 }
89
90 @Override
91 public boolean isCommentNodesRequired() {
92 return true;
93 }
94
95 @Override
96 public void visitToken(DetailAST ast) {
97 log(
98 ast,
99 MSG_KEY,
100 convertToString(ast)
101 );
102 }
103
104 /**
105 * Converts given AST node to string representation.
106 *
107 * @param ast node to be represented as string
108 * @return string representation of AST node
109 */
110 private static String convertToString(DetailAST ast) {
111 final String tokenText;
112 switch (ast.getType()) {
113 case TokenTypes.LABELED_STAT:
114 tokenText = ast.getFirstChild().getText() + ast.getText();
115 break;
116 // multiline tokens need to become singlelined
117 case TokenTypes.COMMENT_CONTENT:
118 tokenText = JavadocUtil.escapeAllControlChars(ast.getText());
119 break;
120 default:
121 tokenText = ast.getText();
122 break;
123 }
124 return tokenText;
125 }
126
127 }