001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2017 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 java.util.HashSet;
023import java.util.Set;
024
025import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
026
027/**
028 * Support for checks that look for usage of illegal types.
029 * @deprecated Checkstyle will not support abstract checks anymore. Use
030 *             {@link AbstractCheck} instead.
031 * @author Oliver Burn
032 * @noinspection AbstractClassNeverImplemented
033 */
034@Deprecated
035public abstract class AbstractIllegalCheck extends AbstractCheck {
036    /** Illegal class names. */
037    private final Set<String> illegalClassNames = new HashSet<>();
038
039    /**
040     * Constructs an object.
041     * @param initialNames the initial class names to treat as illegal
042     */
043    protected AbstractIllegalCheck(final String... initialNames) {
044        setIllegalClassNames(initialNames);
045    }
046
047    /**
048     * Checks if given class is illegal.
049     *
050     * @param ident
051     *            ident to check.
052     * @return true if given ident is illegal.
053     */
054    protected final boolean isIllegalClassName(final String ident) {
055        return illegalClassNames.contains(ident);
056    }
057
058    /**
059     * Set the list of illegal classes.
060     *
061     * @param classNames
062     *            array of illegal exception classes
063     */
064    public final void setIllegalClassNames(final String... classNames) {
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}