001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2024 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 * <ul>
042 * <li>
043 * Property {@code tokens} - tokens to check
044 * Type is {@code anyTokenTypesSet}.
045 * Default value is
046 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LABELED_STAT">
047 * LABELED_STAT</a>.
048 * </li>
049 * </ul>
050 *
051 * <p>
052 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
053 * </p>
054 *
055 * <p>
056 * Violation Message Keys:
057 * </p>
058 * <ul>
059 * <li>
060 * {@code illegal.token}
061 * </li>
062 * </ul>
063 *
064 * @since 3.2
065 */
066@StatelessCheck
067public class IllegalTokenCheck
068    extends AbstractCheck {
069
070    /**
071     * A key is pointing to the warning message text in "messages.properties"
072     * file.
073     */
074    public static final String MSG_KEY = "illegal.token";
075
076    @Override
077    public int[] getDefaultTokens() {
078        return new int[] {
079            TokenTypes.LABELED_STAT,
080        };
081    }
082
083    @Override
084    public int[] getAcceptableTokens() {
085        return TokenUtil.getAllTokenIds();
086    }
087
088    @Override
089    public int[] getRequiredTokens() {
090        return CommonUtil.EMPTY_INT_ARRAY;
091    }
092
093    @Override
094    public boolean isCommentNodesRequired() {
095        return true;
096    }
097
098    @Override
099    public void visitToken(DetailAST ast) {
100        log(
101            ast,
102            MSG_KEY,
103            convertToString(ast)
104        );
105    }
106
107    /**
108     * Converts given AST node to string representation.
109     *
110     * @param ast node to be represented as string
111     * @return string representation of AST node
112     */
113    private static String convertToString(DetailAST ast) {
114        final String tokenText;
115        switch (ast.getType()) {
116            case TokenTypes.LABELED_STAT:
117                tokenText = ast.getFirstChild().getText() + ast.getText();
118                break;
119            // multiline tokens need to become singlelined
120            case TokenTypes.COMMENT_CONTENT:
121                tokenText = JavadocUtil.escapeAllControlChars(ast.getText());
122                break;
123            default:
124                tokenText = ast.getText();
125                break;
126        }
127        return tokenText;
128    }
129
130}