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.cli; |
34 | |
35 | import java.io.*; |
36 | import java.util.*; |
37 | |
38 | import com.jeantessier.classreader.*; |
39 | import com.jeantessier.commandline.*; |
40 | import com.jeantessier.metrics.*; |
41 | import org.apache.log4j.*; |
42 | |
43 | public class OOMetrics extends DirectoryExplorerCommand { |
44 | public static final String DEFAULT_PROJECT_NAME = "Project"; |
45 | public static final String DEFAULT_SORT = "name"; |
46 | |
47 | protected void populateCommandLineSwitches() { |
48 | super.populateCommandLineSwitches(); |
49 | populateCommandLineSwitchesForXMLOutput(com.jeantessier.metrics.XMLPrinter.DEFAULT_ENCODING, com.jeantessier.metrics.XMLPrinter.DEFAULT_DTD_PREFIX, com.jeantessier.metrics.XMLPrinter.DEFAULT_INDENT_TEXT); |
50 | |
51 | getCommandLine().addSingleValueSwitch("project-name", DEFAULT_PROJECT_NAME); |
52 | getCommandLine().addSingleValueSwitch("default-configuration", true); |
53 | getCommandLine().addSingleValueSwitch("configuration"); |
54 | getCommandLine().addToggleSwitch("csv"); |
55 | getCommandLine().addToggleSwitch("txt"); |
56 | getCommandLine().addToggleSwitch("xml"); |
57 | getCommandLine().addToggleSwitch("validate"); |
58 | getCommandLine().addToggleSwitch("project"); |
59 | getCommandLine().addToggleSwitch("groups"); |
60 | getCommandLine().addToggleSwitch("classes"); |
61 | getCommandLine().addToggleSwitch("methods"); |
62 | getCommandLine().addMultipleValuesSwitch("scope-includes-list"); |
63 | getCommandLine().addMultipleValuesSwitch("scope-excludes-list"); |
64 | getCommandLine().addMultipleValuesSwitch("filter-includes-list"); |
65 | getCommandLine().addMultipleValuesSwitch("filter-excludes-list"); |
66 | getCommandLine().addToggleSwitch("show-all-metrics"); |
67 | getCommandLine().addToggleSwitch("show-empty-metrics"); |
68 | getCommandLine().addToggleSwitch("show-hidden-measurements"); |
69 | getCommandLine().addSingleValueSwitch("sort", DEFAULT_SORT); |
70 | getCommandLine().addToggleSwitch("expand"); |
71 | getCommandLine().addToggleSwitch("reverse"); |
72 | getCommandLine().addToggleSwitch("enable-cross-class-measurements"); |
73 | } |
74 | |
75 | protected Collection<CommandLineException> parseCommandLine(String[] args) { |
76 | Collection<CommandLineException> exceptions = super.parseCommandLine(args); |
77 | |
78 | if (!getCommandLine().isPresent("project") && !getCommandLine().isPresent("groups") && !getCommandLine().isPresent("classes") && !getCommandLine().isPresent("methods")) { |
79 | getCommandLine().getSwitch("project").setValue(true); |
80 | getCommandLine().getSwitch("groups").setValue(true); |
81 | getCommandLine().getSwitch("classes").setValue(true); |
82 | getCommandLine().getSwitch("methods").setValue(true); |
83 | } |
84 | |
85 | int modeSwitch = 0; |
86 | |
87 | if (getCommandLine().getToggleSwitch("csv")) { |
88 | modeSwitch++; |
89 | } |
90 | if (getCommandLine().getToggleSwitch("txt")) { |
91 | modeSwitch++; |
92 | } |
93 | if (getCommandLine().getToggleSwitch("xml")) { |
94 | modeSwitch++; |
95 | } |
96 | if (modeSwitch != 1) { |
97 | exceptions.add(new CommandLineException("Must have one and only one of -csv, -txt, or -xml")); |
98 | } |
99 | |
100 | return exceptions; |
101 | } |
102 | |
103 | protected void doProcessing() throws Exception { |
104 | Logger.getLogger(OOMetrics.class).debug("Reading configuration ..."); |
105 | getVerboseListener().print("Reading configuration ..."); |
106 | |
107 | String projectName = getCommandLine().getSingleSwitch("project-name"); |
108 | |
109 | MetricsFactory factory; |
110 | if (getCommandLine().isPresent("configuration")) { |
111 | factory = new MetricsFactory(projectName, new MetricsConfigurationLoader(getCommandLine().getToggleSwitch("validate")).load(getCommandLine().getSingleSwitch("configuration"))); |
112 | } else { |
113 | factory = new MetricsFactory(projectName, new MetricsConfigurationLoader(getCommandLine().getToggleSwitch("validate")).load(getCommandLine().getSingleSwitch("default-configuration"))); |
114 | } |
115 | |
116 | com.jeantessier.metrics.MetricsGatherer gatherer = new com.jeantessier.metrics.MetricsGatherer(factory); |
117 | if (getCommandLine().isPresent("scope-includes-list") || getCommandLine().isPresent("scope-excludes-list")) { |
118 | gatherer.setScopeIncludes(createCollection(getCommandLine().getMultipleSwitch("scope-includes-list"), getCommandLine().getMultipleSwitch("scope-excludes-list"))); |
119 | } |
120 | if (getCommandLine().isPresent("filter-includes-list") || getCommandLine().isPresent("filter-excludes-list")) { |
121 | gatherer.setFilterIncludes(createCollection(getCommandLine().getMultipleSwitch("filter-includes-list"), getCommandLine().getMultipleSwitch("filter-excludes-list"))); |
122 | } |
123 | gatherer.addMetricsListener(getVerboseListener()); |
124 | |
125 | if (getCommandLine().isPresent("enable-cross-class-measurements")) { |
126 | Logger.getLogger(OOMetrics.class).debug("Reading in all classes ..."); |
127 | getVerboseListener().print("Reading in all classes ..."); |
128 | ClassfileLoader loader = new AggregatingClassfileLoader(); |
129 | loader.addLoadListener(getVerboseListener()); |
130 | loader.load(getCommandLine().getParameters()); |
131 | |
132 | Logger.getLogger(OOMetrics.class).debug("Computing metrics ..."); |
133 | getVerboseListener().print("Computing metrics ..."); |
134 | gatherer.visitClassfiles(loader.getAllClassfiles()); |
135 | } else { |
136 | ClassfileLoader loader = new TransientClassfileLoader(); |
137 | loader.addLoadListener(getVerboseListener()); |
138 | loader.addLoadListener(new LoadListenerVisitorAdapter(gatherer)); |
139 | |
140 | Logger.getLogger(OOMetrics.class).debug("Reading classes and computing metrics as we go ..."); |
141 | getVerboseListener().print("Reading classes and computing metrics as we go ..."); |
142 | loader.load(getCommandLine().getParameters()); |
143 | } |
144 | |
145 | if (getCommandLine().isPresent("show-all-metrics")) { |
146 | Iterator i; |
147 | |
148 | i = gatherer.getMetricsFactory().getAllClassMetrics().iterator(); |
149 | while (i.hasNext()) { |
150 | gatherer.getMetricsFactory().includeClassMetrics((Metrics) i.next()); |
151 | } |
152 | |
153 | i = gatherer.getMetricsFactory().getAllMethodMetrics().iterator(); |
154 | while (i.hasNext()) { |
155 | gatherer.getMetricsFactory().includeMethodMetrics((Metrics) i.next()); |
156 | } |
157 | } |
158 | |
159 | Logger.getLogger(OOMetrics.class).debug("Printing results ..."); |
160 | getVerboseListener().print("Printing results ..."); |
161 | |
162 | if (getCommandLine().isPresent("csv")) { |
163 | printCSVFiles(gatherer.getMetricsFactory()); |
164 | } else if (getCommandLine().isPresent("txt")) { |
165 | printTextFile(gatherer.getMetricsFactory()); |
166 | } else if (getCommandLine().isPresent("xml")) { |
167 | printXMLFile(gatherer.getMetricsFactory()); |
168 | } |
169 | |
170 | Logger.getLogger(OOMetrics.class).debug("Done."); |
171 | } |
172 | |
173 | private static Collection<String> createCollection(Collection<String> includes, Collection<String> excludes) throws IOException { |
174 | Collection<String> result = new HashSet<String>(); |
175 | |
176 | for (String include : includes) { |
177 | BufferedReader reader = new BufferedReader(new FileReader(include)); |
178 | String line; |
179 | while ((line = reader.readLine()) != null) { |
180 | result.add(line); |
181 | } |
182 | reader.close(); |
183 | } |
184 | |
185 | for (String exclude : excludes) { |
186 | BufferedReader reader = new BufferedReader(new FileReader(exclude)); |
187 | String line; |
188 | while ((line = reader.readLine()) != null) { |
189 | result.remove(line); |
190 | } |
191 | reader.close(); |
192 | } |
193 | |
194 | return result; |
195 | } |
196 | |
197 | private void printCSVFiles(MetricsFactory factory) throws IOException { |
198 | MetricsComparator comparator = new MetricsComparator(getCommandLine().getSingleSwitch("sort")); |
199 | if (getCommandLine().getToggleSwitch("reverse")) { |
200 | comparator.reverse(); |
201 | } |
202 | |
203 | List<Metrics> metrics; |
204 | com.jeantessier.metrics.Printer printer; |
205 | |
206 | if (getCommandLine().getToggleSwitch("project")) { |
207 | if (getCommandLine().isPresent("out")) { |
208 | setOut(new PrintWriter(new FileWriter(getCommandLine().getSingleSwitch("out") + "_project.csv"))); |
209 | } else { |
210 | getOut().println("Project:"); |
211 | } |
212 | |
213 | metrics = new ArrayList<Metrics>(factory.getProjectMetrics()); |
214 | Collections.sort(metrics, comparator); |
215 | printer = new com.jeantessier.metrics.CSVPrinter(getOut(), factory.getConfiguration().getProjectMeasurements()); |
216 | printer.setShowEmptyMetrics(getCommandLine().isPresent("show-empty-metrics")); |
217 | printer.setShowHiddenMeasurements(getCommandLine().isPresent("show-hidden-measurements")); |
218 | if (getCommandLine().isPresent("indent-text")) { |
219 | printer.setIndentText(getCommandLine().getSingleSwitch("indent-text")); |
220 | } |
221 | |
222 | printer.visitMetrics(metrics); |
223 | |
224 | if (getCommandLine().isPresent("out")) { |
225 | getOut().close(); |
226 | } else { |
227 | getOut().println(); |
228 | } |
229 | } |
230 | |
231 | if (getCommandLine().getToggleSwitch("groups")) { |
232 | if (getCommandLine().isPresent("out")) { |
233 | setOut(new PrintWriter(new FileWriter(getCommandLine().getSingleSwitch("out") + "_groups.csv"))); |
234 | } else { |
235 | getOut().println("Packages:"); |
236 | } |
237 | |
238 | metrics = new ArrayList<Metrics>(factory.getGroupMetrics()); |
239 | Collections.sort(metrics, comparator); |
240 | printer = new com.jeantessier.metrics.CSVPrinter(getOut(), factory.getConfiguration().getGroupMeasurements()); |
241 | printer.setShowEmptyMetrics(getCommandLine().isPresent("show-empty-metrics")); |
242 | printer.setShowHiddenMeasurements(getCommandLine().isPresent("show-hidden-measurements")); |
243 | if (getCommandLine().isPresent("indent-text")) { |
244 | printer.setIndentText(getCommandLine().getSingleSwitch("indent-text")); |
245 | } |
246 | |
247 | printer.visitMetrics(metrics); |
248 | |
249 | if (getCommandLine().isPresent("out")) { |
250 | getOut().close(); |
251 | } else { |
252 | getOut().println(); |
253 | } |
254 | } |
255 | |
256 | if (getCommandLine().getToggleSwitch("classes")) { |
257 | if (getCommandLine().isPresent("out")) { |
258 | setOut(new PrintWriter(new FileWriter(getCommandLine().getSingleSwitch("out") + "_classes.csv"))); |
259 | } else { |
260 | getOut().println("Classes:"); |
261 | } |
262 | |
263 | metrics = new ArrayList<Metrics>(factory.getClassMetrics()); |
264 | Collections.sort(metrics, comparator); |
265 | printer = new com.jeantessier.metrics.CSVPrinter(getOut(), factory.getConfiguration().getClassMeasurements()); |
266 | printer.setShowEmptyMetrics(getCommandLine().isPresent("show-empty-metrics")); |
267 | printer.setShowHiddenMeasurements(getCommandLine().isPresent("show-hidden-measurements")); |
268 | if (getCommandLine().isPresent("indent-text")) { |
269 | printer.setIndentText(getCommandLine().getSingleSwitch("indent-text")); |
270 | } |
271 | |
272 | printer.visitMetrics(metrics); |
273 | |
274 | if (getCommandLine().isPresent("out")) { |
275 | getOut().close(); |
276 | } else { |
277 | getOut().println(); |
278 | } |
279 | } |
280 | |
281 | if (getCommandLine().getToggleSwitch("methods")) { |
282 | if (getCommandLine().isPresent("out")) { |
283 | setOut(new PrintWriter(new FileWriter(getCommandLine().getSingleSwitch("out") + "_methods.csv"))); |
284 | } else { |
285 | getOut().println("Methods:"); |
286 | } |
287 | |
288 | metrics = new ArrayList<Metrics>(factory.getMethodMetrics()); |
289 | Collections.sort(metrics, comparator); |
290 | printer = new com.jeantessier.metrics.CSVPrinter(getOut(), factory.getConfiguration().getMethodMeasurements()); |
291 | printer.setShowEmptyMetrics(getCommandLine().isPresent("show-empty-metrics")); |
292 | printer.setShowHiddenMeasurements(getCommandLine().isPresent("show-hidden-measurements")); |
293 | if (getCommandLine().isPresent("indent-text")) { |
294 | printer.setIndentText(getCommandLine().getSingleSwitch("indent-text")); |
295 | } |
296 | |
297 | printer.visitMetrics(metrics); |
298 | |
299 | if (getCommandLine().isPresent("out")) { |
300 | getOut().close(); |
301 | } |
302 | } |
303 | } |
304 | |
305 | private void printTextFile(MetricsFactory factory) throws IOException { |
306 | MetricsComparator comparator = new MetricsComparator(getCommandLine().getSingleSwitch("sort")); |
307 | if (getCommandLine().getToggleSwitch("reverse")) { |
308 | comparator.reverse(); |
309 | } |
310 | |
311 | List<Metrics> metrics; |
312 | |
313 | if (getCommandLine().getToggleSwitch("project")) { |
314 | getOut().println("Project metrics"); |
315 | getOut().println("---------------"); |
316 | metrics = new ArrayList<Metrics>(factory.getProjectMetrics()); |
317 | Collections.sort(metrics, comparator); |
318 | com.jeantessier.metrics.TextPrinter printer = new com.jeantessier.metrics.TextPrinter(getOut(), factory.getConfiguration().getProjectMeasurements()); |
319 | printer.setExpandCollectionMeasurements(getCommandLine().getToggleSwitch("expand")); |
320 | printer.setShowEmptyMetrics(getCommandLine().isPresent("show-empty-metrics")); |
321 | printer.setShowHiddenMeasurements(getCommandLine().isPresent("show-hidden-measurements")); |
322 | if (getCommandLine().isPresent("indent-text")) { |
323 | printer.setIndentText(getCommandLine().getSingleSwitch("indent-text")); |
324 | } |
325 | |
326 | printer.visitMetrics(metrics); |
327 | |
328 | getOut().println(); |
329 | } |
330 | |
331 | if (getCommandLine().getToggleSwitch("groups")) { |
332 | getOut().println("Group metrics"); |
333 | getOut().println("-------------"); |
334 | metrics = new ArrayList<Metrics>(factory.getGroupMetrics()); |
335 | Collections.sort(metrics, comparator); |
336 | com.jeantessier.metrics.TextPrinter printer = new com.jeantessier.metrics.TextPrinter(getOut(), factory.getConfiguration().getGroupMeasurements()); |
337 | printer.setExpandCollectionMeasurements(getCommandLine().getToggleSwitch("expand")); |
338 | printer.setShowEmptyMetrics(getCommandLine().isPresent("show-empty-metrics")); |
339 | printer.setShowHiddenMeasurements(getCommandLine().isPresent("show-hidden-measurements")); |
340 | if (getCommandLine().isPresent("indent-text")) { |
341 | printer.setIndentText(getCommandLine().getSingleSwitch("indent-text")); |
342 | } |
343 | |
344 | printer.visitMetrics(metrics); |
345 | |
346 | getOut().println(); |
347 | } |
348 | |
349 | if (getCommandLine().getToggleSwitch("classes")) { |
350 | getOut().println("Class metrics"); |
351 | getOut().println("-------------"); |
352 | metrics = new ArrayList<Metrics>(factory.getClassMetrics()); |
353 | Collections.sort(metrics, comparator); |
354 | com.jeantessier.metrics.TextPrinter printer = new com.jeantessier.metrics.TextPrinter(getOut(), factory.getConfiguration().getClassMeasurements()); |
355 | printer.setExpandCollectionMeasurements(getCommandLine().getToggleSwitch("expand")); |
356 | printer.setShowEmptyMetrics(getCommandLine().isPresent("show-empty-metrics")); |
357 | printer.setShowHiddenMeasurements(getCommandLine().isPresent("show-hidden-measurements")); |
358 | if (getCommandLine().isPresent("indent-text")) { |
359 | printer.setIndentText(getCommandLine().getSingleSwitch("indent-text")); |
360 | } |
361 | |
362 | printer.visitMetrics(metrics); |
363 | |
364 | getOut().println(); |
365 | } |
366 | |
367 | if (getCommandLine().getToggleSwitch("methods")) { |
368 | getOut().println("Method metrics"); |
369 | getOut().println("--------------"); |
370 | metrics = new ArrayList<Metrics>(factory.getMethodMetrics()); |
371 | Collections.sort(metrics, comparator); |
372 | com.jeantessier.metrics.TextPrinter printer = new com.jeantessier.metrics.TextPrinter(getOut(), factory.getConfiguration().getMethodMeasurements()); |
373 | printer.setExpandCollectionMeasurements(getCommandLine().getToggleSwitch("expand")); |
374 | printer.setShowEmptyMetrics(getCommandLine().isPresent("show-empty-metrics")); |
375 | printer.setShowHiddenMeasurements(getCommandLine().isPresent("show-hidden-measurements")); |
376 | if (getCommandLine().isPresent("indent-text")) { |
377 | printer.setIndentText(getCommandLine().getSingleSwitch("indent-text")); |
378 | } |
379 | |
380 | printer.visitMetrics(metrics); |
381 | |
382 | getOut().println(); |
383 | } |
384 | |
385 | getOut().close(); |
386 | } |
387 | |
388 | private void printXMLFile(MetricsFactory factory) throws IOException { |
389 | MetricsComparator comparator = new MetricsComparator(getCommandLine().getSingleSwitch("sort")); |
390 | if (getCommandLine().getToggleSwitch("reverse")) { |
391 | comparator.reverse(); |
392 | } |
393 | |
394 | List<Metrics> metrics; |
395 | com.jeantessier.metrics.Printer printer; |
396 | |
397 | metrics = new ArrayList<Metrics>(factory.getProjectMetrics()); |
398 | Collections.sort(metrics, comparator); |
399 | printer = new com.jeantessier.metrics.XMLPrinter(getOut(), factory.getConfiguration(), getCommandLine().getSingleSwitch("encoding"), getCommandLine().getSingleSwitch("dtd-prefix")); |
400 | printer.setShowEmptyMetrics(getCommandLine().isPresent("show-empty-metrics")); |
401 | printer.setShowHiddenMeasurements(getCommandLine().isPresent("show-hidden-measurements")); |
402 | if (getCommandLine().isPresent("indent-text")) { |
403 | printer.setIndentText(getCommandLine().getSingleSwitch("indent-text")); |
404 | } |
405 | |
406 | printer.visitMetrics(metrics); |
407 | |
408 | getOut().close(); |
409 | } |
410 | |
411 | public static void main(String[] args) throws Exception { |
412 | new OOMetrics().run(args); |
413 | } |
414 | } |