1 | /* |
2 | * Copyright (c) 2001-2009, Jean Tessier |
3 | * All rights reserved. |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
8 | * |
9 | * * Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * |
12 | * * Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. |
15 | * |
16 | * * Neither the name of Jean Tessier nor the names of his contributors |
17 | * may be used to endorse or promote products derived from this software |
18 | * without specific prior written permission. |
19 | * |
20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR |
24 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
26 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
27 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
28 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
29 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
30 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
31 | */ |
32 | |
33 | package com.jeantessier.dependencyfinder.ant; |
34 | |
35 | import java.io.*; |
36 | import java.util.*; |
37 | import javax.xml.parsers.*; |
38 | |
39 | import com.jeantessier.classreader.*; |
40 | import com.jeantessier.metrics.*; |
41 | import org.apache.tools.ant.*; |
42 | import org.apache.tools.ant.types.*; |
43 | import org.xml.sax.*; |
44 | |
45 | public class OOMetrics extends Task { |
46 | public static final String DEFAULT_PROJECT_NAME = "Project"; |
47 | public static final String DEFAULT_SORT = "name"; |
48 | |
49 | private String projectName = DEFAULT_PROJECT_NAME; |
50 | private File configuration; |
51 | private boolean csv = false; |
52 | private boolean txt = false; |
53 | private boolean xml = false; |
54 | private boolean validate = false; |
55 | private String encoding = com.jeantessier.metrics.XMLPrinter.DEFAULT_ENCODING; |
56 | private String dtdPrefix = com.jeantessier.metrics.XMLPrinter.DEFAULT_DTD_PREFIX; |
57 | private String indentText; |
58 | private boolean projectMetrics = false; |
59 | private boolean groupMetrics = false; |
60 | private boolean classMetrics = false; |
61 | private boolean methodMetrics = false; |
62 | private Path scopeIncludesList; |
63 | private Path scopeExcludesList; |
64 | private Path filterIncludesList; |
65 | private Path filterExcludesList; |
66 | private boolean showAllMetrics = false; |
67 | private boolean showEmptyMetrics = false; |
68 | private boolean showHiddenMeasurements = false; |
69 | private String sort = DEFAULT_SORT; |
70 | private boolean expand = false; |
71 | private boolean reverse = false; |
72 | private boolean enableCrossClassMeasurements = false; |
73 | private File destprefix; |
74 | private Path path; |
75 | |
76 | public String getProjectname() { |
77 | return projectName; |
78 | } |
79 | |
80 | public void setProjectname(String projectName) { |
81 | this.projectName = projectName; |
82 | } |
83 | |
84 | public File getConfiguration() { |
85 | return configuration; |
86 | } |
87 | |
88 | public void setConfiguration(File configuration) { |
89 | this.configuration = configuration; |
90 | } |
91 | |
92 | public boolean getCsv() { |
93 | return csv; |
94 | } |
95 | |
96 | public void setCsv(boolean csv) { |
97 | this.csv = csv; |
98 | } |
99 | |
100 | public boolean getTxt() { |
101 | return txt; |
102 | } |
103 | |
104 | public void setTxt(boolean txt) { |
105 | this.txt = txt; |
106 | } |
107 | |
108 | public boolean getXml() { |
109 | return xml; |
110 | } |
111 | |
112 | public void setXml(boolean xml) { |
113 | this.xml = xml; |
114 | } |
115 | |
116 | public boolean getValidate() { |
117 | return validate; |
118 | } |
119 | |
120 | public void setValidate(boolean validate) { |
121 | this.validate = validate; |
122 | } |
123 | |
124 | public String getEncoding() { |
125 | return encoding; |
126 | } |
127 | |
128 | public void setEncoding(String encoding) { |
129 | this.encoding = encoding; |
130 | } |
131 | |
132 | public String getDtdprefix() { |
133 | return dtdPrefix; |
134 | } |
135 | |
136 | public void setDtdprefix(String dtdPrefix) { |
137 | this.dtdPrefix = dtdPrefix; |
138 | } |
139 | |
140 | public String getIndenttext() { |
141 | return indentText; |
142 | } |
143 | |
144 | public void setIntenttext(String indentText) { |
145 | this.indentText = indentText; |
146 | } |
147 | |
148 | public boolean getProjectmetrics() { |
149 | return projectMetrics; |
150 | } |
151 | |
152 | public void setProjectmetrics(boolean projectMetrics) { |
153 | this.projectMetrics = projectMetrics; |
154 | } |
155 | |
156 | public boolean getGroupmetrics() { |
157 | return groupMetrics; |
158 | } |
159 | |
160 | public void setGroupmetrics(boolean groupMetrics) { |
161 | this.groupMetrics = groupMetrics; |
162 | } |
163 | |
164 | public boolean getClassmetrics() { |
165 | return classMetrics; |
166 | } |
167 | |
168 | public void setClassmetrics(boolean classMetrics) { |
169 | this.classMetrics = classMetrics; |
170 | } |
171 | |
172 | public boolean getMethodmetrics() { |
173 | return methodMetrics; |
174 | } |
175 | |
176 | public void setMethodmetrics(boolean methodMetrics) { |
177 | this.methodMetrics = methodMetrics; |
178 | } |
179 | |
180 | public void setAllmetrics(boolean allMetrics) { |
181 | setProjectmetrics(allMetrics); |
182 | setGroupmetrics(allMetrics); |
183 | setClassmetrics(allMetrics); |
184 | setMethodmetrics(allMetrics); |
185 | } |
186 | |
187 | public Path createScopeincludeslist() { |
188 | if (scopeIncludesList == null) { |
189 | scopeIncludesList = new Path(getProject()); |
190 | } |
191 | |
192 | return scopeIncludesList; |
193 | } |
194 | |
195 | public Path getScopeincludeslist() { |
196 | return scopeIncludesList; |
197 | } |
198 | |
199 | public Path createScopeexcludeslist() { |
200 | if (scopeExcludesList == null) { |
201 | scopeExcludesList = new Path(getProject()); |
202 | } |
203 | |
204 | return scopeExcludesList; |
205 | } |
206 | |
207 | public Path getScopeexcludeslist() { |
208 | return scopeExcludesList; |
209 | } |
210 | |
211 | public Path createFilterincludeslist() { |
212 | if (filterIncludesList == null) { |
213 | filterIncludesList = new Path(getProject()); |
214 | } |
215 | |
216 | return filterIncludesList; |
217 | } |
218 | |
219 | public Path getFilterincludeslist() { |
220 | return filterIncludesList; |
221 | } |
222 | |
223 | public Path createFilterexcludeslist() { |
224 | if (filterExcludesList == null) { |
225 | filterExcludesList = new Path(getProject()); |
226 | } |
227 | |
228 | return filterExcludesList; |
229 | } |
230 | |
231 | public Path getFilterexcludeslist() { |
232 | return filterExcludesList; |
233 | } |
234 | |
235 | public boolean getShowallmetrics() { |
236 | return showAllMetrics; |
237 | } |
238 | |
239 | public void setShowallmetrics(boolean showAllMetrics) { |
240 | this.showAllMetrics = showAllMetrics; |
241 | } |
242 | |
243 | public boolean getShowemptymetrics() { |
244 | return showEmptyMetrics; |
245 | } |
246 | |
247 | public void setShowemptymetrics(boolean showEmptyMetrics) { |
248 | this.showEmptyMetrics = showEmptyMetrics; |
249 | } |
250 | |
251 | public boolean getShowhiddenmeasurements() { |
252 | return showHiddenMeasurements; |
253 | } |
254 | |
255 | public void setShowhiddenmeasurements(boolean showHiddenMeasurements) { |
256 | this.showHiddenMeasurements = showHiddenMeasurements; |
257 | } |
258 | |
259 | public String getSort() { |
260 | return sort; |
261 | } |
262 | |
263 | public void setSort(String sort) { |
264 | this.sort = sort; |
265 | } |
266 | |
267 | public boolean getExpand() { |
268 | return expand; |
269 | } |
270 | |
271 | public void setExpand(boolean expand) { |
272 | this.expand = expand; |
273 | } |
274 | |
275 | public boolean getReverse() { |
276 | return reverse; |
277 | } |
278 | |
279 | public void setReverse(boolean reverse) { |
280 | this.reverse = reverse; |
281 | } |
282 | |
283 | public boolean getEnablecrossclassmeasurements() { |
284 | return enableCrossClassMeasurements; |
285 | } |
286 | |
287 | public void setEnablecrossclassmeasurements(boolean enableCrossClassMeasurements) { |
288 | this.enableCrossClassMeasurements = enableCrossClassMeasurements; |
289 | } |
290 | |
291 | public File getDestprefix() { |
292 | return destprefix; |
293 | } |
294 | |
295 | public void setDestprefix(File destprefix) { |
296 | this.destprefix = destprefix; |
297 | } |
298 | |
299 | public Path createPath() { |
300 | if (path == null) { |
301 | path = new Path(getProject()); |
302 | } |
303 | |
304 | return path; |
305 | } |
306 | |
307 | public Path getPath() { |
308 | return path; |
309 | } |
310 | |
311 | public void execute() throws BuildException { |
312 | // first off, make sure that we've got what we need |
313 | |
314 | if (getConfiguration() == null) { |
315 | throw new BuildException("configuration must be set!"); |
316 | } |
317 | |
318 | if (!getConfiguration().exists()) { |
319 | throw new BuildException("configuration does not exist!"); |
320 | } |
321 | |
322 | if (!getConfiguration().isFile()) { |
323 | throw new BuildException("configuration is not a file!"); |
324 | } |
325 | |
326 | if (getPath() == null) { |
327 | throw new BuildException("path must be set!"); |
328 | } |
329 | |
330 | if (getDestprefix() == null) { |
331 | throw new BuildException("destprefix must be set!"); |
332 | } |
333 | |
334 | try { |
335 | VerboseListener verboseListener = new VerboseListener(this); |
336 | |
337 | log("Reading configuration ..."); |
338 | MetricsFactory factory = new MetricsFactory(getProjectname(), new MetricsConfigurationLoader(getValidate()).load(getConfiguration().getAbsolutePath())); |
339 | |
340 | com.jeantessier.metrics.MetricsGatherer gatherer = new com.jeantessier.metrics.MetricsGatherer(factory); |
341 | if (getScopeincludeslist() != null || getScopeexcludeslist() != null) { |
342 | gatherer.setScopeIncludes(createCollection(getScopeincludeslist(), getScopeexcludeslist())); |
343 | } |
344 | if (getFilterincludeslist() != null || getFilterexcludeslist() != null) { |
345 | gatherer.setFilterIncludes(createCollection(getFilterincludeslist(), getFilterexcludeslist())); |
346 | } |
347 | gatherer.addMetricsListener(verboseListener); |
348 | |
349 | if (getEnablecrossclassmeasurements()) { |
350 | log("Reading in all classes from path " + getPath()); |
351 | ClassfileLoader loader = new AggregatingClassfileLoader(); |
352 | loader.addLoadListener(verboseListener); |
353 | loader.load(Arrays.asList(getPath().list())); |
354 | |
355 | log("Computing metrics ..."); |
356 | gatherer.visitClassfiles(loader.getAllClassfiles()); |
357 | } else { |
358 | ClassfileLoader loader = new TransientClassfileLoader(); |
359 | loader.addLoadListener(verboseListener); |
360 | loader.addLoadListener(new LoadListenerVisitorAdapter(gatherer)); |
361 | |
362 | log("Reading classes and computing metrics as we go ..."); |
363 | loader.load(Arrays.asList(getPath().list())); |
364 | } |
365 | |
366 | if (getShowallmetrics()) { |
367 | for (Metrics metrics : gatherer.getMetricsFactory().getAllClassMetrics()) { |
368 | gatherer.getMetricsFactory().includeClassMetrics(metrics); |
369 | } |
370 | |
371 | for (Metrics metrics : gatherer.getMetricsFactory().getAllMethodMetrics()) { |
372 | gatherer.getMetricsFactory().includeMethodMetrics(metrics); |
373 | } |
374 | } |
375 | |
376 | if (getCsv()) { |
377 | printCSVFiles(gatherer.getMetricsFactory()); |
378 | } else if (getTxt()) { |
379 | printTextFile(gatherer.getMetricsFactory()); |
380 | } else if (getXml()) { |
381 | printXMLFile(gatherer.getMetricsFactory()); |
382 | } |
383 | } catch (SAXException ex) { |
384 | throw new BuildException(ex); |
385 | } catch (ParserConfigurationException ex) { |
386 | throw new BuildException(ex); |
387 | } catch (IOException ex) { |
388 | throw new BuildException(ex); |
389 | } |
390 | } |
391 | |
392 | private Collection<String> createCollection(Path includes, Path excludes) throws IOException { |
393 | Collection<String> result = new HashSet<String>(); |
394 | |
395 | if (includes != null) { |
396 | String[] filenames = includes.list(); |
397 | for (String filename : filenames) { |
398 | BufferedReader reader = new BufferedReader(new FileReader(filename)); |
399 | String line; |
400 | while ((line = reader.readLine()) != null) { |
401 | result.add(line); |
402 | } |
403 | reader.close(); |
404 | } |
405 | } |
406 | |
407 | if (excludes != null) { |
408 | String[] filenames = excludes.list(); |
409 | for (String filename : filenames) { |
410 | BufferedReader reader = new BufferedReader(new FileReader(filename)); |
411 | String line; |
412 | while ((line = reader.readLine()) != null) { |
413 | result.remove(line); |
414 | } |
415 | reader.close(); |
416 | } |
417 | } |
418 | |
419 | return result; |
420 | } |
421 | |
422 | private void printCSVFiles(MetricsFactory factory) throws IOException { |
423 | MetricsComparator comparator = new MetricsComparator(getSort()); |
424 | if (getReverse()) { |
425 | comparator.reverse(); |
426 | } |
427 | |
428 | List<Metrics> metrics; |
429 | com.jeantessier.metrics.Printer printer; |
430 | |
431 | if (getProjectmetrics()) { |
432 | String filename = getDestprefix().getAbsolutePath() + "_project.csv"; |
433 | log("Saving metrics to " + filename); |
434 | |
435 | PrintWriter out = new PrintWriter(new FileWriter(filename)); |
436 | |
437 | metrics = new ArrayList<Metrics>(factory.getProjectMetrics()); |
438 | Collections.sort(metrics, comparator); |
439 | printer = new com.jeantessier.metrics.CSVPrinter(out, factory.getConfiguration().getProjectMeasurements()); |
440 | printer.setShowEmptyMetrics(getShowemptymetrics()); |
441 | printer.setShowHiddenMeasurements(getShowhiddenmeasurements()); |
442 | if (getIndenttext() != null) { |
443 | printer.setIndentText(getIndenttext()); |
444 | } |
445 | |
446 | printer.visitMetrics(metrics); |
447 | |
448 | out.close(); |
449 | } |
450 | |
451 | if (getGroupmetrics()) { |
452 | String filename = getDestprefix().getAbsolutePath() + "_groups.csv"; |
453 | log("Saving metrics to " + filename); |
454 | |
455 | PrintWriter out = new PrintWriter(new FileWriter(filename)); |
456 | |
457 | metrics = new ArrayList<Metrics>(factory.getGroupMetrics()); |
458 | Collections.sort(metrics, comparator); |
459 | printer = new com.jeantessier.metrics.CSVPrinter(out, factory.getConfiguration().getGroupMeasurements()); |
460 | printer.setShowEmptyMetrics(getShowemptymetrics()); |
461 | printer.setShowHiddenMeasurements(getShowhiddenmeasurements()); |
462 | if (getIndenttext() != null) { |
463 | printer.setIndentText(getIndenttext()); |
464 | } |
465 | |
466 | printer.visitMetrics(metrics); |
467 | |
468 | out.close(); |
469 | } |
470 | |
471 | if (getClassmetrics()) { |
472 | String filename = getDestprefix().getAbsolutePath() + "_classes.csv"; |
473 | log("Saving metrics to " + filename); |
474 | |
475 | PrintWriter out = new PrintWriter(new FileWriter(filename)); |
476 | |
477 | metrics = new ArrayList<Metrics>(factory.getClassMetrics()); |
478 | Collections.sort(metrics, comparator); |
479 | printer = new com.jeantessier.metrics.CSVPrinter(out, factory.getConfiguration().getClassMeasurements()); |
480 | printer.setShowEmptyMetrics(getShowemptymetrics()); |
481 | printer.setShowHiddenMeasurements(getShowhiddenmeasurements()); |
482 | if (getIndenttext() != null) { |
483 | printer.setIndentText(getIndenttext()); |
484 | } |
485 | |
486 | printer.visitMetrics(metrics); |
487 | |
488 | out.close(); |
489 | } |
490 | |
491 | if (getMethodmetrics()) { |
492 | String filename = getDestprefix().getAbsolutePath() + "_methods.csv"; |
493 | log("Saving metrics to " + filename); |
494 | |
495 | PrintWriter out = new PrintWriter(new FileWriter(filename)); |
496 | |
497 | metrics = new ArrayList<Metrics>(factory.getMethodMetrics()); |
498 | Collections.sort(metrics, comparator); |
499 | printer = new com.jeantessier.metrics.CSVPrinter(out, factory.getConfiguration().getMethodMeasurements()); |
500 | printer.setShowEmptyMetrics(getShowemptymetrics()); |
501 | printer.setShowHiddenMeasurements(getShowhiddenmeasurements()); |
502 | if (getIndenttext() != null) { |
503 | printer.setIndentText(getIndenttext()); |
504 | } |
505 | |
506 | printer.visitMetrics(metrics); |
507 | |
508 | out.close(); |
509 | } |
510 | } |
511 | |
512 | private void printTextFile(MetricsFactory factory) throws IOException { |
513 | MetricsComparator comparator = new MetricsComparator(getSort()); |
514 | if (getReverse()) { |
515 | comparator.reverse(); |
516 | } |
517 | |
518 | String filename = getDestprefix().getAbsolutePath() + ".txt"; |
519 | log("Saving metrics to " + filename); |
520 | |
521 | PrintWriter out = new PrintWriter(new FileWriter(filename)); |
522 | |
523 | List<Metrics> metrics; |
524 | |
525 | if (getProjectmetrics()) { |
526 | out.println("Project metrics"); |
527 | out.println("---------------"); |
528 | metrics = new ArrayList<Metrics>(factory.getProjectMetrics()); |
529 | Collections.sort(metrics, comparator); |
530 | com.jeantessier.metrics.TextPrinter printer = new com.jeantessier.metrics.TextPrinter(out, factory.getConfiguration().getProjectMeasurements()); |
531 | printer.setShowEmptyMetrics(getShowemptymetrics()); |
532 | printer.setShowHiddenMeasurements(getShowhiddenmeasurements()); |
533 | printer.setExpandCollectionMeasurements(getExpand()); |
534 | if (getIndenttext() != null) { |
535 | printer.setIndentText(getIndenttext()); |
536 | } |
537 | |
538 | printer.visitMetrics(metrics); |
539 | |
540 | out.println(); |
541 | } |
542 | |
543 | if (getGroupmetrics()) { |
544 | out.println("Group metrics"); |
545 | out.println("-------------"); |
546 | metrics = new ArrayList<Metrics>(factory.getGroupMetrics()); |
547 | Collections.sort(metrics, comparator); |
548 | com.jeantessier.metrics.TextPrinter printer = new com.jeantessier.metrics.TextPrinter(out, factory.getConfiguration().getGroupMeasurements()); |
549 | printer.setShowEmptyMetrics(getShowemptymetrics()); |
550 | printer.setShowHiddenMeasurements(getShowhiddenmeasurements()); |
551 | printer.setExpandCollectionMeasurements(getExpand()); |
552 | if (getIndenttext() != null) { |
553 | printer.setIndentText(getIndenttext()); |
554 | } |
555 | |
556 | printer.visitMetrics(metrics); |
557 | |
558 | out.println(); |
559 | } |
560 | |
561 | if (getClassmetrics()) { |
562 | out.println("Class metrics"); |
563 | out.println("-------------"); |
564 | metrics = new ArrayList<Metrics>(factory.getClassMetrics()); |
565 | Collections.sort(metrics, comparator); |
566 | com.jeantessier.metrics.TextPrinter printer = new com.jeantessier.metrics.TextPrinter(out, factory.getConfiguration().getClassMeasurements()); |
567 | printer.setShowEmptyMetrics(getShowemptymetrics()); |
568 | printer.setShowHiddenMeasurements(getShowhiddenmeasurements()); |
569 | printer.setExpandCollectionMeasurements(getExpand()); |
570 | if (getIndenttext() != null) { |
571 | printer.setIndentText(getIndenttext()); |
572 | } |
573 | |
574 | printer.visitMetrics(metrics); |
575 | |
576 | out.println(); |
577 | } |
578 | |
579 | if (getMethodmetrics()) { |
580 | out.println("Method metrics"); |
581 | out.println("--------------"); |
582 | metrics = new ArrayList<Metrics>(factory.getMethodMetrics()); |
583 | Collections.sort(metrics, comparator); |
584 | com.jeantessier.metrics.TextPrinter printer = new com.jeantessier.metrics.TextPrinter(out, factory.getConfiguration().getMethodMeasurements()); |
585 | printer.setShowEmptyMetrics(getShowemptymetrics()); |
586 | printer.setShowHiddenMeasurements(getShowhiddenmeasurements()); |
587 | printer.setExpandCollectionMeasurements(getExpand()); |
588 | if (getIndenttext() != null) { |
589 | printer.setIndentText(getIndenttext()); |
590 | } |
591 | |
592 | printer.visitMetrics(metrics); |
593 | |
594 | out.println(); |
595 | } |
596 | |
597 | out.close(); |
598 | } |
599 | |
600 | private void printXMLFile(MetricsFactory factory) throws IOException { |
601 | MetricsComparator comparator = new MetricsComparator(getSort()); |
602 | if (getReverse()) { |
603 | comparator.reverse(); |
604 | } |
605 | |
606 | String filename = getDestprefix().getAbsolutePath() + ".xml"; |
607 | log("Saving metrics to " + filename); |
608 | |
609 | PrintWriter out = new PrintWriter(new FileWriter(filename)); |
610 | |
611 | List<Metrics> metrics = new ArrayList<Metrics>(factory.getProjectMetrics()); |
612 | Collections.sort(metrics, comparator); |
613 | com.jeantessier.metrics.Printer printer = new com.jeantessier.metrics.XMLPrinter(out, factory.getConfiguration(), getEncoding(), getDtdprefix()); |
614 | printer.setShowEmptyMetrics(getShowemptymetrics()); |
615 | printer.setShowHiddenMeasurements(getShowhiddenmeasurements()); |
616 | if (getIndenttext() != null) { |
617 | printer.setIndentText(getIndenttext()); |
618 | } |
619 | |
620 | printer.visitMetrics(metrics); |
621 | |
622 | out.close(); |
623 | } |
624 | } |