Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
PackageObjectFactory |
|
| 5.0;5 |
1 | //////////////////////////////////////////////////////////////////////////////// | |
2 | // checkstyle: Checks Java source code for adherence to a set of rules. | |
3 | // Copyright (C) 2001-2014 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 com.google.common.collect.Sets; | |
22 | import com.puppycrawl.tools.checkstyle.api.CheckstyleException; | |
23 | import java.util.Set; | |
24 | ||
25 | /** | |
26 | * A factory for creating objects from package names and names. | |
27 | * @author Rick Giles | |
28 | * @author lkuehne | |
29 | */ | |
30 | class PackageObjectFactory implements ModuleFactory | |
31 | { | |
32 | /** a list of package names to prepend to class names */ | |
33 | private final Set<String> mPackages; | |
34 | ||
35 | /** the class loader used to load Checkstyle core and custom modules. */ | |
36 | private final ClassLoader mModuleClassLoader; | |
37 | ||
38 | /** | |
39 | * Creates a new <code>PackageObjectFactory</code> instance. | |
40 | * @param aPackageNames the list of package names to use | |
41 | * @param aModuleClassLoader class loader used to load Checkstyle | |
42 | * core and custom modules | |
43 | */ | |
44 | public PackageObjectFactory(Set<String> aPackageNames, | |
45 | ClassLoader aModuleClassLoader) | |
46 | 597 | { |
47 | 597 | if (aModuleClassLoader == null) { |
48 | 0 | throw new IllegalArgumentException( |
49 | "aModuleClassLoader must not be null"); | |
50 | } | |
51 | ||
52 | //create a copy of the given set, but retain ordering | |
53 | 597 | mPackages = Sets.newLinkedHashSet(aPackageNames); |
54 | 597 | mModuleClassLoader = aModuleClassLoader; |
55 | 597 | } |
56 | ||
57 | /** | |
58 | * Registers a package name to use for shortName resolution. | |
59 | * @param aPackageName the package name | |
60 | */ | |
61 | void addPackage(String aPackageName) | |
62 | { | |
63 | 1 | mPackages.add(aPackageName); |
64 | 1 | } |
65 | ||
66 | /** | |
67 | * Creates a new instance of a class from a given name. If the name is | |
68 | * a classname, creates an instance of the named class. Otherwise, creates | |
69 | * an instance of a classname obtained by concatenating the given | |
70 | * to a package name from a given list of package names. | |
71 | * @param aName the name of a class. | |
72 | * @return the <code>Object</code> | |
73 | * @throws CheckstyleException if an error occurs. | |
74 | */ | |
75 | private Object doMakeObject(String aName) | |
76 | throws CheckstyleException | |
77 | { | |
78 | //try aName first | |
79 | try { | |
80 | 1220 | return createObject(aName); |
81 | } | |
82 | 4 | catch (final CheckstyleException ex) { |
83 | ; // keep looking | |
84 | } | |
85 | ||
86 | //now try packages | |
87 | 4 | for (String packageName : mPackages) { |
88 | ||
89 | 1 | final String className = packageName + aName; |
90 | try { | |
91 | 1 | return createObject(className); |
92 | } | |
93 | 0 | catch (final CheckstyleException ex) { |
94 | ; // keep looking | |
95 | } | |
96 | 0 | } |
97 | ||
98 | 3 | throw new CheckstyleException("Unable to instantiate " + aName); |
99 | } | |
100 | ||
101 | /** | |
102 | * Creates a new instance of a named class. | |
103 | * @param aClassName the name of the class to instantiate. | |
104 | * @return the <code>Object</code> created by mLoader. | |
105 | * @throws CheckstyleException if an error occurs. | |
106 | */ | |
107 | private Object createObject(String aClassName) | |
108 | throws CheckstyleException | |
109 | { | |
110 | try { | |
111 | 1221 | final Class<?> clazz = Class.forName(aClassName, true, |
112 | mModuleClassLoader); | |
113 | 1217 | return clazz.newInstance(); |
114 | } | |
115 | 4 | catch (final ClassNotFoundException e) { |
116 | 4 | throw new CheckstyleException( |
117 | "Unable to find class for " + aClassName, e); | |
118 | } | |
119 | 0 | catch (final InstantiationException e) { |
120 | ///CLOVER:OFF | |
121 | 0 | throw new CheckstyleException( |
122 | "Unable to instantiate " + aClassName, e); | |
123 | ///CLOVER:ON | |
124 | } | |
125 | 0 | catch (final IllegalAccessException e) { |
126 | ///CLOVER:OFF | |
127 | 0 | throw new CheckstyleException( |
128 | "Unable to instantiate " + aClassName, e); | |
129 | ///CLOVER:ON | |
130 | } | |
131 | } | |
132 | ||
133 | /** | |
134 | * Creates a new instance of a class from a given name, or that name | |
135 | * concatenated with "Check". If the name is | |
136 | * a classname, creates an instance of the named class. Otherwise, creates | |
137 | * an instance of a classname obtained by concatenating the given name | |
138 | * to a package name from a given list of package names. | |
139 | * @param aName the name of a class. | |
140 | * @return the <code>Object</code> created by aLoader. | |
141 | * @throws CheckstyleException if an error occurs. | |
142 | */ | |
143 | public Object createModule(String aName) | |
144 | throws CheckstyleException | |
145 | { | |
146 | try { | |
147 | 1218 | return doMakeObject(aName); |
148 | } | |
149 | 2 | catch (final CheckstyleException ex) { |
150 | //try again with suffix "Check" | |
151 | try { | |
152 | 2 | return doMakeObject(aName + "Check"); |
153 | } | |
154 | 1 | catch (final CheckstyleException ex2) { |
155 | 1 | throw new CheckstyleException( |
156 | "Unable to instantiate " + aName, ex2); | |
157 | } | |
158 | } | |
159 | } | |
160 | } |