001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2020 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 * <p> 032 * Checks for illegal tokens. By default labels are prohibited. 033 * </p> 034 * <p> 035 * Rationale: Certain language features can harm readability, lead to 036 * confusion or are not obvious to novice developers. Other features 037 * may be discouraged in certain frameworks, such as not having 038 * native methods in Enterprise JavaBeans components. 039 * </p> 040 * <ul> 041 * <li> 042 * Property {@code tokens} - tokens to check 043 * Default value is: 044 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LABELED_STAT"> 045 * LABELED_STAT</a>. 046 * </li> 047 * </ul> 048 * <p> 049 * To configure the check: 050 * </p> 051 * <pre> 052 * <module name="IllegalToken"/> 053 * </pre> 054 * <p> 055 * To configure the check to find token LITERAL_NATIVE: 056 * </p> 057 * <pre> 058 * <module name="IllegalToken"> 059 * <property name="tokens" value="LITERAL_NATIVE"/> 060 * </module> 061 * </pre> 062 * 063 * @since 3.2 064 */ 065@StatelessCheck 066public class IllegalTokenCheck 067 extends AbstractCheck { 068 069 /** 070 * A key is pointing to the warning message text in "messages.properties" 071 * file. 072 */ 073 public static final String MSG_KEY = "illegal.token"; 074 075 @Override 076 public int[] getDefaultTokens() { 077 return new int[] { 078 TokenTypes.LABELED_STAT, 079 }; 080 } 081 082 @Override 083 public int[] getAcceptableTokens() { 084 return TokenUtil.getAllTokenIds(); 085 } 086 087 @Override 088 public int[] getRequiredTokens() { 089 return CommonUtil.EMPTY_INT_ARRAY; 090 } 091 092 @Override 093 public boolean isCommentNodesRequired() { 094 return true; 095 } 096 097 @Override 098 public void visitToken(DetailAST ast) { 099 log( 100 ast, 101 MSG_KEY, 102 convertToString(ast) 103 ); 104 } 105 106 /** 107 * Converts given AST node to string representation. 108 * @param ast node to be represented as string 109 * @return string representation of AST node 110 */ 111 private static String convertToString(DetailAST ast) { 112 final String tokenText; 113 switch (ast.getType()) { 114 case TokenTypes.LABELED_STAT: 115 tokenText = ast.getFirstChild().getText() + ast.getText(); 116 break; 117 // multiline tokens need to become singlelined 118 case TokenTypes.COMMENT_CONTENT: 119 tokenText = JavadocUtil.escapeAllControlChars(ast.getText()); 120 break; 121 default: 122 tokenText = ast.getText(); 123 break; 124 } 125 return tokenText; 126 } 127 128}