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