001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2026 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 extends AbstractCheck {
046
047    /**
048     * A key is pointing to the warning message text in "messages.properties" file.
049     */
050    public static final String MSG_KEY = "illegal.token";
051
052    /**
053     * Creates a new {@code IllegalTokenCheck} instance.
054     */
055    public IllegalTokenCheck() {
056        // no code by default
057    }
058
059    @Override
060    public int[] getDefaultTokens() {
061        return new int[] {
062            TokenTypes.LABELED_STAT,
063        };
064    }
065
066    @Override
067    public int[] getAcceptableTokens() {
068        return TokenUtil.getAllTokenIds();
069    }
070
071    @Override
072    public int[] getRequiredTokens() {
073        return CommonUtil.EMPTY_INT_ARRAY;
074    }
075
076    @Override
077    public boolean isCommentNodesRequired() {
078        return true;
079    }
080
081    @Override
082    public void visitToken(DetailAST ast) {
083        log(
084            ast,
085            MSG_KEY,
086            convertToString(ast)
087        );
088    }
089
090    /**
091     * Converts given AST node to string representation.
092     *
093     * @param ast node to be represented as string
094     * @return string representation of AST node
095     */
096    private static String convertToString(DetailAST ast) {
097        return switch (ast.getType()) {
098            case TokenTypes.LABELED_STAT -> ast.getFirstChild().getText() + ast.getText();
099            // multiline tokens need to become singlelined
100            case TokenTypes.COMMENT_CONTENT -> JavadocUtil.escapeAllControlChars(ast.getText());
101            default -> ast.getText();
102        };
103    }
104
105}