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