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