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.google.common.collect.Sets;
022import com.puppycrawl.tools.checkstyle.api.Check;
023import java.util.Set;
024
025/**
026 * Support for checks that look for usage of illegal types.
027 * @author Oliver Burn
028 */
029public abstract class AbstractIllegalCheck extends Check
030{
031    /** Illegal class names */
032    private final Set<String> illegalClassNames = Sets.newHashSet();
033
034    /**
035     * Constructs an object.
036     * @param initialNames the initial class names to treat as illegal
037     */
038    protected AbstractIllegalCheck(final String[] initialNames)
039    {
040        assert initialNames != null;
041        setIllegalClassNames(initialNames);
042    }
043
044    /**
045     * Checks if given class is illegal.
046     *
047     * @param ident
048     *            ident to check.
049     * @return true if given ident is illegal.
050     */
051    protected final boolean isIllegalClassName(final String ident)
052    {
053        return illegalClassNames.contains(ident);
054    }
055
056    /**
057     * Set the list of illegal classes.
058     *
059     * @param classNames
060     *            array of illegal exception classes
061     */
062    public final void setIllegalClassNames(final String[] classNames)
063    {
064        assert classNames != null;
065        illegalClassNames.clear();
066        for (final String name : classNames) {
067            illegalClassNames.add(name);
068            final int lastDot = name.lastIndexOf(".");
069            if ((lastDot > 0) && (lastDot < (name.length() - 1))) {
070                final String shortName = name
071                        .substring(name.lastIndexOf(".") + 1);
072                illegalClassNames.add(shortName);
073            }
074        }
075    }
076}