001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2025 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018/////////////////////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle.checks.coding; 021 022import com.puppycrawl.tools.checkstyle.StatelessCheck; 023import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 024import com.puppycrawl.tools.checkstyle.api.DetailAST; 025import com.puppycrawl.tools.checkstyle.api.TokenTypes; 026import com.puppycrawl.tools.checkstyle.utils.CommonUtil; 027import com.puppycrawl.tools.checkstyle.utils.JavadocUtil; 028import com.puppycrawl.tools.checkstyle.utils.TokenUtil; 029 030/** 031 * <div> 032 * Checks for illegal tokens. By default, labels are prohibited. 033 * </div> 034 * 035 * <p> 036 * Rationale: Certain language features can harm readability, lead to 037 * confusion or are not obvious to novice developers. Other features 038 * may be discouraged in certain frameworks, such as not having 039 * native methods in Enterprise JavaBeans components. 040 * </p> 041 * 042 * @since 3.2 043 */ 044@StatelessCheck 045public class IllegalTokenCheck 046 extends AbstractCheck { 047 048 /** 049 * A key is pointing to the warning message text in "messages.properties" 050 * file. 051 */ 052 public static final String MSG_KEY = "illegal.token"; 053 054 @Override 055 public int[] getDefaultTokens() { 056 return new int[] { 057 TokenTypes.LABELED_STAT, 058 }; 059 } 060 061 @Override 062 public int[] getAcceptableTokens() { 063 return TokenUtil.getAllTokenIds(); 064 } 065 066 @Override 067 public int[] getRequiredTokens() { 068 return CommonUtil.EMPTY_INT_ARRAY; 069 } 070 071 @Override 072 public boolean isCommentNodesRequired() { 073 return true; 074 } 075 076 @Override 077 public void visitToken(DetailAST ast) { 078 log( 079 ast, 080 MSG_KEY, 081 convertToString(ast) 082 ); 083 } 084 085 /** 086 * Converts given AST node to string representation. 087 * 088 * @param ast node to be represented as string 089 * @return string representation of AST node 090 */ 091 private static String convertToString(DetailAST ast) { 092 return switch (ast.getType()) { 093 case TokenTypes.LABELED_STAT -> ast.getFirstChild().getText() + ast.getText(); 094 // multiline tokens need to become singlelined 095 case TokenTypes.COMMENT_CONTENT -> JavadocUtil.escapeAllControlChars(ast.getText()); 096 default -> ast.getText(); 097 }; 098 } 099 100}