1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
package com.jeantessier.metrics; |
34 | |
|
35 | |
import java.lang.reflect.*; |
36 | |
import java.util.*; |
37 | |
|
38 | |
import org.apache.log4j.*; |
39 | |
import org.apache.oro.text.perl.*; |
40 | |
|
41 | |
public class MetricsFactory { |
42 | 2 | private static final Perl5Util perl = new Perl5Util(); |
43 | |
|
44 | |
private String projectName; |
45 | |
private MetricsConfiguration configuration; |
46 | |
|
47 | 54 | private Map<String, Metrics> projects = new HashMap<String, Metrics>(); |
48 | 54 | private Map<String, Metrics> groups = new HashMap<String, Metrics>(); |
49 | 54 | private Map<String, Metrics> classes = new HashMap<String, Metrics>(); |
50 | 54 | private Map<String, Metrics> methods = new HashMap<String, Metrics>(); |
51 | |
|
52 | 54 | private Map<String, Metrics> includedProjects = new HashMap<String, Metrics>(); |
53 | 54 | private Map<String, Metrics> includedGroups = new HashMap<String, Metrics>(); |
54 | 54 | private Map<String, Metrics> includedClasses = new HashMap<String, Metrics>(); |
55 | 54 | private Map<String, Metrics> includedMethods = new HashMap<String, Metrics>(); |
56 | |
|
57 | 54 | private WordCounter counter = new WordCounter(); |
58 | |
|
59 | 54 | public MetricsFactory(String projectName, MetricsConfiguration configuration) { |
60 | 54 | this.projectName = projectName; |
61 | 54 | this.configuration = configuration; |
62 | 54 | } |
63 | |
|
64 | |
public String getProjectName() { |
65 | 196 | return projectName; |
66 | |
} |
67 | |
|
68 | |
public MetricsConfiguration getConfiguration() { |
69 | 1624 | return configuration; |
70 | |
} |
71 | |
|
72 | |
public Metrics createProjectMetrics() { |
73 | 196 | return createProjectMetrics(getProjectName()); |
74 | |
} |
75 | |
|
76 | |
public Metrics createProjectMetrics(String name) { |
77 | 201 | Metrics result = projects.get(name); |
78 | |
|
79 | 201 | if (result == null) { |
80 | 54 | result = buildProjectMetrics(name); |
81 | 54 | projects.put(name, result); |
82 | |
} |
83 | |
|
84 | 201 | return result; |
85 | |
} |
86 | |
|
87 | |
private Metrics buildProjectMetrics(String name) { |
88 | 54 | Metrics result = new Metrics(name); |
89 | |
|
90 | 54 | populateMetrics(result, getConfiguration().getProjectMeasurements()); |
91 | |
|
92 | 54 | return result; |
93 | |
} |
94 | |
|
95 | |
public void includeProjectMetrics(Metrics metrics) { |
96 | 593 | includedProjects.put(metrics.getName(), metrics); |
97 | 593 | } |
98 | |
|
99 | |
public Collection<String> getProjectNames() { |
100 | 1 | return Collections.unmodifiableCollection(includedProjects.keySet()); |
101 | |
} |
102 | |
|
103 | |
public Collection<Metrics> getProjectMetrics() { |
104 | 3 | return Collections.unmodifiableCollection(includedProjects.values()); |
105 | |
} |
106 | |
|
107 | |
public Collection<String> getAllProjectNames() { |
108 | 1 | return Collections.unmodifiableCollection(projects.keySet()); |
109 | |
} |
110 | |
|
111 | |
public Collection<Metrics> getAllProjectMetrics() { |
112 | 0 | return Collections.unmodifiableCollection(projects.values()); |
113 | |
} |
114 | |
|
115 | |
public Metrics createGroupMetrics(String name) { |
116 | 400 | Metrics result = groups.get(name); |
117 | |
|
118 | 400 | if (result == null) { |
119 | 154 | result = buildGroupMetrics(name); |
120 | 154 | groups.put(name, result); |
121 | |
} |
122 | |
|
123 | 400 | return result; |
124 | |
} |
125 | |
|
126 | |
private Metrics buildGroupMetrics(String name) { |
127 | 154 | Metrics projectMetrics = createProjectMetrics(); |
128 | 154 | Metrics result = new Metrics(projectMetrics, name); |
129 | |
|
130 | 154 | populateMetrics(result, getConfiguration().getGroupMeasurements()); |
131 | 154 | initializeGroupMetrics(name, result); |
132 | |
|
133 | 154 | return result; |
134 | |
} |
135 | |
|
136 | |
private void initializeGroupMetrics(String packageName, Metrics metrics) { |
137 | 154 | computePackageNameCharacterCount(packageName, metrics); |
138 | 154 | computePackageNameWordCount(packageName, metrics); |
139 | 154 | } |
140 | |
|
141 | |
private void computePackageNameCharacterCount(String packageName, Metrics metrics) { |
142 | 154 | metrics.addToMeasurement(BasicMeasurements.GROUP_NAME_CHARACTER_COUNT, packageName.length()); |
143 | 154 | } |
144 | |
|
145 | |
private void computePackageNameWordCount(String packageName, Metrics metrics) { |
146 | 154 | metrics.addToMeasurement(BasicMeasurements.GROUP_NAME_WORD_COUNT, counter.countPackageName(packageName)); |
147 | 154 | } |
148 | |
|
149 | |
public void includeGroupMetrics(Metrics metrics) { |
150 | 592 | includedGroups.put(metrics.getName(), metrics); |
151 | 592 | metrics.getParent().addSubMetrics(metrics); |
152 | 592 | includeProjectMetrics(metrics.getParent()); |
153 | 592 | } |
154 | |
|
155 | |
public Collection<String> getGroupNames() { |
156 | 9 | return Collections.unmodifiableCollection(includedGroups.keySet()); |
157 | |
} |
158 | |
|
159 | |
public Collection<Metrics> getGroupMetrics() { |
160 | 12 | return Collections.unmodifiableCollection(includedGroups.values()); |
161 | |
} |
162 | |
|
163 | |
public Collection<String> getAllGroupNames() { |
164 | 13 | return Collections.unmodifiableCollection(groups.keySet()); |
165 | |
} |
166 | |
|
167 | |
public Collection<Metrics> getAllGroupMetrics() { |
168 | 3 | return Collections.unmodifiableCollection(groups.values()); |
169 | |
} |
170 | |
|
171 | |
public Metrics createClassMetrics(String name) { |
172 | 1635 | Metrics result = classes.get(name); |
173 | |
|
174 | 1635 | if (result == null) { |
175 | 373 | result = buildClassMetrics(name); |
176 | 373 | classes.put(name, result); |
177 | |
} |
178 | |
|
179 | 1635 | return result; |
180 | |
} |
181 | |
|
182 | |
private Metrics buildClassMetrics(String name) { |
183 | 373 | String packageName = ""; |
184 | 373 | int pos = name.lastIndexOf('.'); |
185 | 373 | if (pos != -1) { |
186 | 357 | packageName = name.substring(0, pos); |
187 | |
} |
188 | 373 | String className = name.substring(pos + 1); |
189 | 373 | Metrics packageMetrics = createGroupMetrics(packageName); |
190 | 373 | Metrics result = new Metrics(packageMetrics, name); |
191 | |
|
192 | 373 | populateMetrics(result, getConfiguration().getClassMeasurements()); |
193 | 373 | initializeClassMetrics(className, result); |
194 | |
|
195 | 373 | return result; |
196 | |
} |
197 | |
|
198 | |
private void initializeClassMetrics(String className, Metrics metrics) { |
199 | 373 | computeClassNameCharacterCount(className, metrics); |
200 | 373 | computeClassNameWordCount(className, metrics); |
201 | 373 | } |
202 | |
|
203 | |
private void computeClassNameCharacterCount(String className, Metrics metrics) { |
204 | 373 | metrics.addToMeasurement(BasicMeasurements.CLASS_NAME_CHARACTER_COUNT, className.length()); |
205 | 373 | } |
206 | |
|
207 | |
private void computeClassNameWordCount(String className, Metrics metrics) { |
208 | 373 | metrics.addToMeasurement(BasicMeasurements.CLASS_NAME_WORD_COUNT, counter.countIdentifier(className)); |
209 | 373 | } |
210 | |
|
211 | |
public void includeClassMetrics(Metrics metrics) { |
212 | 588 | includedClasses.put(metrics.getName(), metrics); |
213 | 588 | metrics.getParent().addSubMetrics(metrics); |
214 | 588 | includeGroupMetrics(metrics.getParent()); |
215 | |
|
216 | 588 | for (String name : getConfiguration().getGroups(metrics.getName())) { |
217 | 3 | Metrics groupMetrics = createGroupMetrics(name); |
218 | 3 | groupMetrics.addSubMetrics(metrics); |
219 | 3 | includeGroupMetrics(groupMetrics); |
220 | 3 | } |
221 | 588 | } |
222 | |
|
223 | |
public Collection<String> getClassNames() { |
224 | 1 | return Collections.unmodifiableCollection(includedClasses.keySet()); |
225 | |
} |
226 | |
|
227 | |
public Collection<Metrics> getClassMetrics() { |
228 | 12 | return Collections.unmodifiableCollection(includedClasses.values()); |
229 | |
} |
230 | |
|
231 | |
public Collection<String> getAllClassNames() { |
232 | 1 | return Collections.unmodifiableCollection(classes.keySet()); |
233 | |
} |
234 | |
|
235 | |
public Collection<Metrics> getAllClassMetrics() { |
236 | 0 | return Collections.unmodifiableCollection(classes.values()); |
237 | |
} |
238 | |
|
239 | |
public Metrics createMethodMetrics(String name) { |
240 | 682 | Metrics result = methods.get(name); |
241 | |
|
242 | 682 | if (result == null) { |
243 | 455 | result = buildMethodMetrics(name); |
244 | 455 | methods.put(name, result); |
245 | |
} |
246 | |
|
247 | 682 | return result; |
248 | |
} |
249 | |
|
250 | |
private Metrics buildMethodMetrics(String name) { |
251 | 455 | String className = ""; |
252 | 455 | String featureName = ""; |
253 | 455 | if (perl.match("/^(.*)\\.([^\\.]*)\\(.*\\)$/", name)) { |
254 | 452 | className = perl.group(1); |
255 | 452 | featureName = perl.group(2); |
256 | 3 | } else if (perl.match("/^(.*)\\.(static) {}$/", name)) { |
257 | 1 | className = perl.group(1); |
258 | 1 | featureName = perl.group(2); |
259 | 2 | } else if (perl.match("/^(.*)\\.([\\^.]*)$/", name)) { |
260 | 0 | className = perl.group(1); |
261 | 0 | featureName = perl.group(2); |
262 | |
} |
263 | 455 | Metrics classMetrics = createClassMetrics(className); |
264 | 455 | Metrics result = new Metrics(classMetrics, name); |
265 | 455 | classMetrics.addSubMetrics(result); |
266 | |
|
267 | 455 | populateMetrics(result, getConfiguration().getMethodMeasurements()); |
268 | 455 | initializeMethodMetrics(featureName, result); |
269 | |
|
270 | 455 | return result; |
271 | |
} |
272 | |
|
273 | |
private void initializeMethodMetrics(String featureName, Metrics metrics) { |
274 | 455 | computeMethodNameCharacterCount(featureName, metrics); |
275 | 455 | computeMethodNameWordCount(featureName, metrics); |
276 | 455 | } |
277 | |
|
278 | |
private void computeMethodNameCharacterCount(String featureName, Metrics metrics) { |
279 | 455 | metrics.addToMeasurement(BasicMeasurements.METHOD_NAME_CHARACTER_COUNT, featureName.length()); |
280 | 455 | } |
281 | |
|
282 | |
private void computeMethodNameWordCount(String featureName, Metrics metrics) { |
283 | 455 | metrics.addToMeasurement(BasicMeasurements.METHOD_NAME_WORD_COUNT, counter.countIdentifier(featureName)); |
284 | 455 | } |
285 | |
|
286 | |
public void includeMethodMetrics(Metrics metrics) { |
287 | 379 | includedMethods.put(metrics.getName(), metrics); |
288 | 379 | metrics.getParent().addSubMetrics(metrics); |
289 | 379 | includeClassMetrics(metrics.getParent()); |
290 | 379 | } |
291 | |
|
292 | |
public Collection<String> getMethodNames() { |
293 | 1 | return Collections.unmodifiableCollection(includedMethods.keySet()); |
294 | |
} |
295 | |
|
296 | |
public Collection<Metrics> getMethodMetrics() { |
297 | 22 | return Collections.unmodifiableCollection(includedMethods.values()); |
298 | |
} |
299 | |
|
300 | |
public Collection<String> getAllMethodNames() { |
301 | 1 | return Collections.unmodifiableCollection(methods.keySet()); |
302 | |
} |
303 | |
|
304 | |
public Collection<Metrics> getAllMethodMetrics() { |
305 | 0 | return Collections.unmodifiableCollection(methods.values()); |
306 | |
} |
307 | |
|
308 | |
public void clear() { |
309 | 0 | projects.clear(); |
310 | 0 | groups.clear(); |
311 | 0 | classes.clear(); |
312 | 0 | methods.clear(); |
313 | |
|
314 | 0 | includedProjects.clear(); |
315 | 0 | includedGroups.clear(); |
316 | 0 | includedClasses.clear(); |
317 | 0 | includedMethods.clear(); |
318 | 0 | } |
319 | |
|
320 | |
private void populateMetrics(Metrics metrics, Collection<MeasurementDescriptor> descriptors) { |
321 | 1036 | for (MeasurementDescriptor descriptor : descriptors) { |
322 | |
try { |
323 | 36742 | metrics.track(descriptor.createMeasurement(metrics)); |
324 | 0 | } catch (InstantiationException ex) { |
325 | 0 | Logger.getLogger(getClass()).warn("Unable to create measurement \"" + descriptor.getShortName() + "\"", ex); |
326 | 0 | } catch (IllegalAccessException ex) { |
327 | 0 | Logger.getLogger(getClass()).warn("Unable to create measurement \"" + descriptor.getShortName() + "\"", ex); |
328 | 0 | } catch (NoSuchMethodException ex) { |
329 | 0 | Logger.getLogger(getClass()).warn("Unable to create measurement \"" + descriptor.getShortName() + "\"", ex); |
330 | 0 | } catch (InvocationTargetException ex) { |
331 | 0 | Logger.getLogger(getClass()).warn("Unable to create measurement \"" + descriptor.getShortName() + "\"", ex); |
332 | 73484 | } |
333 | |
} |
334 | 1036 | } |
335 | |
|
336 | |
public String toString() { |
337 | 0 | StringBuffer result = new StringBuffer(); |
338 | |
|
339 | 0 | result.append("Factory for project \"").append(getProjectName()).append("\"").append(System.getProperty("line.separator", "\n")); |
340 | |
|
341 | 0 | result.append("projects:").append(System.getProperty("line.separator", "\n")); |
342 | 0 | for (Map.Entry<String, Metrics> entry : projects.entrySet()) { |
343 | 0 | result.append(" ").append(entry.getKey()).append(" -> ").append(entry.getValue().getName()).append("").append(System.getProperty("line.separator", "\n")); |
344 | |
} |
345 | |
|
346 | 0 | result.append("groups:").append(System.getProperty("line.separator", "\n")); |
347 | 0 | for (Map.Entry<String, Metrics> entry : groups.entrySet()) { |
348 | 0 | result.append(" ").append(entry.getKey()).append(" -> ").append(entry.getValue().getName()).append("").append(System.getProperty("line.separator", "\n")); |
349 | |
} |
350 | |
|
351 | 0 | result.append("classes:").append(System.getProperty("line.separator", "\n")); |
352 | 0 | for (Map.Entry<String, Metrics> entry : classes.entrySet()) { |
353 | 0 | result.append(" ").append(entry.getKey()).append(" -> ").append(entry.getValue().getName()).append("").append(System.getProperty("line.separator", "\n")); |
354 | |
} |
355 | |
|
356 | 0 | result.append("methods:").append(System.getProperty("line.separator", "\n")); |
357 | 0 | for (Map.Entry<String, Metrics> entry : methods.entrySet()) { |
358 | 0 | result.append(" ").append(entry.getKey()).append(" -> ").append(entry.getValue().getName()).append("").append(System.getProperty("line.separator", "\n")); |
359 | |
} |
360 | |
|
361 | 0 | return result.toString(); |
362 | |
} |
363 | |
} |