001: ////////////////////////////////////////////////////////////////////////////////
002: // checkstyle: Checks Java source code for adherence to a set of rules.
003: // Copyright (C) 2001-2007 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: ////////////////////////////////////////////////////////////////////////////////
019: package com.puppycrawl.tools.checkstyle;
020:
021: import java.util.ArrayList;
022: import java.util.List;
023:
024: import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
025:
026: /**
027: * A factory for creating objects from package names and names.
028: * @author Rick Giles
029: * @author lkuehne
030: * @version $Revision$
031: */
032: class PackageObjectFactory implements ModuleFactory {
033: /** a list of package names to prepend to class names */
034: private final List mPackages = new ArrayList();
035:
036: /**
037: * Creates a new <code>PackageObjectFactory</code> instance.
038: */
039: PackageObjectFactory() {
040: }
041:
042: /**
043: * Helper for testing.
044: * @return the package names that have been added
045: */
046: String[] getPackages() {
047: return (String[]) mPackages
048: .toArray(new String[mPackages.size()]);
049: }
050:
051: /**
052: * Registers a package name to use for shortName resolution.
053: * @param aPackageName the package name
054: */
055: void addPackage(String aPackageName) {
056: mPackages.add(aPackageName);
057: }
058:
059: /**
060: * Creates a new instance of a class from a given name. If the name is
061: * a classname, creates an instance of the named class. Otherwise, creates
062: * an instance of a classname obtained by concatenating the given
063: * to a package name from a given list of package names.
064: * @param aName the name of a class.
065: * @return the <code>Object</code>
066: * @throws CheckstyleException if an error occurs.
067: */
068: private Object doMakeObject(String aName)
069: throws CheckstyleException {
070: //try aName first
071: try {
072: return createObject(aName);
073: } catch (final CheckstyleException ex) {
074: ; // keep looking
075: }
076:
077: //now try packages
078: for (int i = 0; i < mPackages.size(); i++) {
079: final String packageName = (String) mPackages.get(i);
080: final String className = packageName + aName;
081: try {
082: return createObject(className);
083: } catch (final CheckstyleException ex) {
084: ; // keep looking
085: }
086: }
087:
088: throw new CheckstyleException("Unable to instantiate " + aName);
089: }
090:
091: /**
092: * Creates a new instance of a named class.
093: * @param aClassName the name of the class to instantiate.
094: * @return the <code>Object</code> created by mLoader.
095: * @throws CheckstyleException if an error occurs.
096: */
097: private Object createObject(String aClassName)
098: throws CheckstyleException {
099: try {
100: final ClassLoader loader = Thread.currentThread()
101: .getContextClassLoader();
102: final Class clazz = Class.forName(aClassName, true, loader);
103: return clazz.newInstance();
104: } catch (final ClassNotFoundException e) {
105: throw new CheckstyleException("Unable to find class for "
106: + aClassName, e);
107: } catch (final InstantiationException e) {
108: ///CLOVER:OFF
109: throw new CheckstyleException("Unable to instantiate "
110: + aClassName, e);
111: ///CLOVER:ON
112: } catch (final IllegalAccessException e) {
113: ///CLOVER:OFF
114: throw new CheckstyleException("Unable to instantiate "
115: + aClassName, e);
116: ///CLOVER:ON
117: }
118: }
119:
120: /**
121: * Creates a new instance of a class from a given name, or that name
122: * concatenated with "Check". If the name is
123: * a classname, creates an instance of the named class. Otherwise, creates
124: * an instance of a classname obtained by concatenating the given name
125: * to a package name from a given list of package names.
126: * @param aName the name of a class.
127: * @return the <code>Object</code> created by aLoader.
128: * @throws CheckstyleException if an error occurs.
129: */
130: public Object createModule(String aName) throws CheckstyleException {
131: try {
132: return doMakeObject(aName);
133: } catch (final CheckstyleException ex) {
134: //try again with suffix "Check"
135: try {
136: return doMakeObject(aName + "Check");
137: } catch (final CheckstyleException ex2) {
138: throw new CheckstyleException("Unable to instantiate "
139: + aName, ex2);
140: }
141: }
142: }
143: }
|