001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2014  Oliver Burn
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////////////////////////////////////////////////////////////////////////////////
019package com.puppycrawl.tools.checkstyle.checks.coding;
020
021import com.puppycrawl.tools.checkstyle.api.Check;
022import com.puppycrawl.tools.checkstyle.api.DetailAST;
023import com.puppycrawl.tools.checkstyle.api.TokenTypes;
024import java.util.Set;
025
026/**
027 * <p>
028 * Checks for illegal tokens.
029 * </p>
030 * <p>
031 * Rational: Certain language features are often lead to hard to
032 * maintain code or are non-obvious to novice developers. Others
033 * may be discouraged in certain frameworks, such as not having
034 * native methods in EJB components.
035 * </p>
036 * <p>
037 * An example of how to configure the check is:
038 * </p>
039 * <pre>
040 * &lt;module name="IllegalToken"/&gt;
041 * </pre>
042 * <p> An example of how to configure the check to forbid
043 * a {@link TokenTypes#LITERAL_NATIVE LITERAL_NATIVE} token is:
044 * </p>
045 * <pre>
046 * &lt;module name="IllegalToken"&gt;
047 *     &lt;property name="tokens" value="LITERAL_NATIVE"/&gt;
048 * &lt;/module&gt;
049 * </pre>
050 * @author <a href="mailto:simon@redhillconsulting.com.au">Simon Harris</a>
051 * @author Rick Giles
052 */
053public class IllegalTokenCheck
054    extends Check
055{
056    @Override
057    public int[] getDefaultTokens()
058    {
059        return new int[] {
060            TokenTypes.LITERAL_SWITCH,
061            TokenTypes.POST_INC,
062            TokenTypes.POST_DEC,
063        };
064    }
065
066    @Override
067    public int[] getAcceptableTokens()
068    {
069        // Any tokens set by property 'tokens' are acceptable
070        int[] tokensToCopy = getDefaultTokens();
071        final Set<String> tokenNames = getTokenNames();
072        if (!tokenNames.isEmpty()) {
073            tokensToCopy = new int[tokenNames.size()];
074            int i = 0;
075            for (String name : tokenNames) {
076                tokensToCopy[i] = TokenTypes.getTokenId(name);
077                i++;
078            }
079        }
080        final int[] copy = new int[tokensToCopy.length];
081        System.arraycopy(tokensToCopy, 0, copy, 0, tokensToCopy.length);
082        return copy;
083    }
084
085    @Override
086    public void visitToken(DetailAST ast)
087    {
088        log(
089            ast.getLineNo(),
090            ast.getColumnNo(),
091            "illegal.token",
092            ast.getText());
093    }
094
095}