KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > PackageObjectFactory


1 ////////////////////////////////////////////////////////////////////////////////
2
// checkstyle: Checks Java source code for adherence to a set of rules.
3
// Copyright (C) 2001-2005 Oliver Burn
4
//
5
// This library is free software; you can redistribute it and/or
6
// modify it under the terms of the GNU Lesser General Public
7
// License as published by the Free Software Foundation; either
8
// version 2.1 of the License, or (at your option) any later version.
9
//
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;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.List JavaDoc;
23
24 import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
25
26 /**
27  * A factory for creating objects from package names and names.
28  * @author Rick Giles
29  * @author lkuehne
30  * @version $Revision$
31  */

32 class PackageObjectFactory implements ModuleFactory
33 {
34     /** a list of package names to prepend to class names */
35     private final List JavaDoc mPackages = new ArrayList JavaDoc();
36
37     /**
38      * Creates a new <code>PackageObjectFactory</code> instance.
39      */

40     PackageObjectFactory()
41     {
42     }
43
44     /**
45      * Helper for testing.
46      * @return the package names that have been added
47      */

48     String JavaDoc[] getPackages()
49     {
50         return (String JavaDoc[]) mPackages.toArray(new String JavaDoc[mPackages.size()]);
51     }
52
53     /**
54      * Registers a package name to use for shortName resolution.
55      * @param aPackageName the package name
56      */

57     void addPackage(String JavaDoc aPackageName)
58     {
59         mPackages.add(aPackageName);
60     }
61
62     /**
63      * Creates a new instance of a class from a given name. If the name is
64      * a classname, creates an instance of the named class. Otherwise, creates
65      * an instance of a classname obtained by concatenating the given
66      * to a package name from a given list of package names.
67      * @param aName the name of a class.
68      * @return the <code>Object</code>
69      * @throws CheckstyleException if an error occurs.
70      */

71     private Object JavaDoc doMakeObject(String JavaDoc aName)
72         throws CheckstyleException
73     {
74         //try aName first
75
try {
76             return createObject(aName);
77         }
78         catch (final CheckstyleException ex) {
79             ; // keep looking
80
}
81
82         //now try packages
83
for (int i = 0; i < mPackages.size(); i++) {
84             final String JavaDoc packageName = (String JavaDoc) mPackages.get(i);
85             final String JavaDoc className = packageName + aName;
86             try {
87                 return createObject(className);
88             }
89             catch (final CheckstyleException ex) {
90                 ; // keep looking
91
}
92         }
93
94         throw new CheckstyleException("Unable to instantiate " + aName);
95     }
96
97     /**
98      * Creates a new instance of a named class.
99      * @param aClassName the name of the class to instantiate.
100      * @return the <code>Object</code> created by mLoader.
101      * @throws CheckstyleException if an error occurs.
102      */

103     private Object JavaDoc createObject(String JavaDoc aClassName)
104         throws CheckstyleException
105     {
106         try {
107             final ClassLoader JavaDoc loader = Thread.currentThread()
108                     .getContextClassLoader();
109             final Class JavaDoc clazz = Class.forName(aClassName, true, loader);
110             return clazz.newInstance();
111         }
112         catch (final ClassNotFoundException JavaDoc e) {
113             throw new CheckstyleException(
114                 "Unable to find class for " + aClassName, e);
115         }
116         catch (final InstantiationException JavaDoc e) {
117             ///CLOVER:OFF
118
throw new CheckstyleException(
119                 "Unable to instantiate " + aClassName, e);
120             ///CLOVER:ON
121
}
122         catch (final IllegalAccessException JavaDoc e) {
123             ///CLOVER:OFF
124
throw new CheckstyleException(
125                 "Unable to instantiate " + aClassName, e);
126             ///CLOVER:ON
127
}
128     }
129
130     /**
131      * Creates a new instance of a class from a given name, or that name
132      * concatenated with &quot;Check&quot;. If the name is
133      * a classname, creates an instance of the named class. Otherwise, creates
134      * an instance of a classname obtained by concatenating the given name
135      * to a package name from a given list of package names.
136      * @param aName the name of a class.
137      * @return the <code>Object</code> created by aLoader.
138      * @throws CheckstyleException if an error occurs.
139      */

140     public Object JavaDoc createModule(String JavaDoc aName)
141         throws CheckstyleException
142     {
143         try {
144             return doMakeObject(aName);
145         }
146         catch (final CheckstyleException ex) {
147             //try again with suffix "Check"
148
try {
149                 return doMakeObject(aName + "Check");
150             }
151             catch (final CheckstyleException ex2) {
152                 throw new CheckstyleException(
153                     "Unable to instantiate " + aName, ex2);
154             }
155         }
156     }
157 }
158
Popular Tags