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