01: ////////////////////////////////////////////////////////////////////////////////
02: // checkstyle: Checks Java source code for adherence to a set of rules.
03: // Copyright (C) 2001-2007 Oliver Burn
04: //
05: // This library is free software; you can redistribute it and/or
06: // modify it under the terms of the GNU Lesser General Public
07: // License as published by the Free Software Foundation; either
08: // version 2.1 of the License, or (at your option) any later version.
09: //
10: // This library is distributed in the hope that it will be useful,
11: // but WITHOUT ANY WARRANTY; without even the implied warranty of
12: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: // Lesser General Public License for more details.
14: //
15: // You should have received a copy of the GNU Lesser General Public
16: // License along with this library; if not, write to the Free Software
17: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18: ////////////////////////////////////////////////////////////////////////////////
19: package com.puppycrawl.tools.checkstyle.checks.coding;
20:
21: import com.puppycrawl.tools.checkstyle.api.Check;
22: import java.util.HashSet;
23: import java.util.Set;
24:
25: /**
26: * Support for checks that look for usage of illegal types.
27: * @author Oliver Burn
28: */
29: public abstract class AbstractIllegalCheck extends Check {
30: /** Illegal class names */
31: private final Set mIllegalClassNames = new HashSet();
32:
33: /**
34: * Constructs an object.
35: * @param aInitialNames the initial class names to treat as illegal
36: */
37: protected AbstractIllegalCheck(final String[] aInitialNames) {
38: assert aInitialNames != null;
39: setIllegalClassNames(aInitialNames);
40: }
41:
42: /**
43: * Checks if given class is illegal.
44: *
45: * @param aIdent
46: * ident to check.
47: * @return true if given ident is illegal.
48: */
49: protected final boolean isIllegalClassName(final String aIdent) {
50: return mIllegalClassNames.contains(aIdent);
51: }
52:
53: /**
54: * Set the list of illegal classes.
55: *
56: * @param aClassNames
57: * array of illegal exception classes
58: */
59: public final void setIllegalClassNames(final String[] aClassNames) {
60: assert aClassNames != null;
61: mIllegalClassNames.clear();
62: for (int i = 0; i < aClassNames.length; i++) {
63: final String name = aClassNames[i];
64: mIllegalClassNames.add(name);
65: final int lastDot = name.lastIndexOf(".");
66: if ((lastDot > 0) && (lastDot < (name.length() - 1))) {
67: final String shortName = name.substring(name
68: .lastIndexOf(".") + 1);
69: mIllegalClassNames.add(shortName);
70: }
71: }
72: }
73: }
|