1 32 33 package com.jeantessier.dependencyfinder.cli; 34 35 import java.io.*; 36 import java.util.*; 37 38 import org.apache.log4j.*; 39 40 import com.jeantessier.classreader.*; 41 import com.jeantessier.commandline.*; 42 import com.jeantessier.dependencyfinder.*; 43 import com.jeantessier.metrics.*; 44 45 public class OOMetrics { 46 public static final String DEFAULT_PROJECT_NAME = "Project"; 47 public static final String DEFAULT_SORT = "name"; 48 public static final String DEFAULT_LOGFILE = "System.out"; 49 50 public static void showError(CommandLineUsage clu, String msg) { 51 System.err.println(msg); 52 showError(clu); 53 } 54 55 public static void showError(CommandLineUsage clu) { 56 System.err.println(clu); 57 System.err.println(); 58 System.err.println("If no files are specified, it processes the current directory."); 59 System.err.println(); 60 System.err.println("If file is a directory, it is recusively scanned for files"); 61 System.err.println("ending in \".class\"."); 62 System.err.println(); 63 System.err.println("Defaults is text output to the console."); 64 System.err.println(); 65 } 66 67 public static void showVersion() throws IOException { 68 Version version = new Version(); 69 70 System.err.print(version.getImplementationTitle()); 71 System.err.print(" "); 72 System.err.print(version.getImplementationVersion()); 73 System.err.print(" (c) "); 74 System.err.print(version.getCopyrightDate()); 75 System.err.print(" "); 76 System.err.print(version.getCopyrightHolder()); 77 System.err.println(); 78 79 System.err.print(version.getImplementationURL()); 80 System.err.println(); 81 82 System.err.print("Compiled on "); 83 System.err.print(version.getImplementationDate()); 84 System.err.println(); 85 } 86 87 public static void main(String [] args) throws Exception { 88 CommandLine commandLine = new CommandLine(); 90 commandLine.addSingleValueSwitch("project-name", DEFAULT_PROJECT_NAME); 91 commandLine.addSingleValueSwitch("default-configuration", true); 92 commandLine.addSingleValueSwitch("configuration"); 93 commandLine.addToggleSwitch("csv"); 94 commandLine.addToggleSwitch("txt"); 95 commandLine.addToggleSwitch("xml"); 96 commandLine.addToggleSwitch("validate"); 97 commandLine.addSingleValueSwitch("encoding", com.jeantessier.metrics.XMLPrinter.DEFAULT_ENCODING); 98 commandLine.addSingleValueSwitch("dtd-prefix", com.jeantessier.metrics.XMLPrinter.DEFAULT_DTD_PREFIX); 99 commandLine.addSingleValueSwitch("indent-text"); 100 commandLine.addToggleSwitch("all"); 101 commandLine.addToggleSwitch("project"); 102 commandLine.addToggleSwitch("groups"); 103 commandLine.addToggleSwitch("classes"); 104 commandLine.addToggleSwitch("methods"); 105 commandLine.addMultipleValuesSwitch("scope-includes-list"); 106 commandLine.addMultipleValuesSwitch("scope-excludes-list"); 107 commandLine.addMultipleValuesSwitch("filter-includes-list"); 108 commandLine.addMultipleValuesSwitch("filter-excludes-list"); 109 commandLine.addToggleSwitch("show-all-metrics"); 110 commandLine.addToggleSwitch("show-empty-metrics"); 111 commandLine.addToggleSwitch("show-hidden-measurements"); 112 commandLine.addSingleValueSwitch("sort", DEFAULT_SORT); 113 commandLine.addToggleSwitch("expand"); 114 commandLine.addToggleSwitch("reverse"); 115 commandLine.addToggleSwitch("time"); 116 commandLine.addSingleValueSwitch("out"); 117 commandLine.addToggleSwitch("help"); 118 commandLine.addOptionalValueSwitch("verbose", DEFAULT_LOGFILE); 119 commandLine.addToggleSwitch("version"); 120 121 CommandLineUsage usage = new CommandLineUsage("OOMetrics"); 122 commandLine.accept(usage); 123 124 try { 125 commandLine.parse(args); 126 } catch (IllegalArgumentException ex) { 127 showError(usage, ex.toString()); 128 System.exit(1); 129 } catch (CommandLineException ex) { 130 showError(usage, ex.toString()); 131 System.exit(1); 132 } 133 134 if (commandLine.getToggleSwitch("help")) { 135 showError(usage); 136 } 137 138 if (commandLine.getToggleSwitch("version")) { 139 showVersion(); 140 } 141 142 if (commandLine.getToggleSwitch("help") || commandLine.getToggleSwitch("version")) { 143 System.exit(1); 144 } 145 146 if (!commandLine.getToggleSwitch("all") && !commandLine.getToggleSwitch("project") && !commandLine.getToggleSwitch("groups") && !commandLine.getToggleSwitch("classes") && !commandLine.getToggleSwitch("methods")) { 147 showError(usage, "Must have at least one of -all, -project, -groups, -classes, or -methods"); 148 System.exit(1); 149 } 150 151 int modeSwitch = 0; 152 153 if (commandLine.getToggleSwitch("csv")) { 154 modeSwitch++; 155 } 156 if (commandLine.getToggleSwitch("txt")) { 157 modeSwitch++; 158 } 159 if (commandLine.getToggleSwitch("xml")) { 160 modeSwitch++; 161 } 162 if (modeSwitch != 1) { 163 showError(usage, "Must have one and only one of -csv, -txt, or -xml"); 164 System.exit(1); 165 } 166 167 VerboseListener verboseListener = new VerboseListener(); 168 if (commandLine.isPresent("verbose")) { 169 if ("System.out".equals(commandLine.getOptionalSwitch("verbose"))) { 170 verboseListener.setWriter(System.out); 171 } else { 172 verboseListener.setWriter(new FileWriter(commandLine.getOptionalSwitch("verbose"))); 173 } 174 } 175 176 179 180 Date start = new Date(); 181 182 Logger.getLogger(OOMetrics.class).debug("Reading sources ..."); 183 184 List parameters = commandLine.getParameters(); 185 if (parameters.size() == 0) { 186 parameters.add("."); 187 } 188 189 ClassfileLoader loader = new AggregatingClassfileLoader(); 190 loader.addLoadListener(verboseListener); 191 loader.load(parameters); 192 193 Logger.getLogger(OOMetrics.class).debug("Reading configuration ..."); 194 195 String projectName = commandLine.getSingleSwitch("project-name"); 196 197 MetricsFactory factory; 198 199 if (commandLine.isPresent("configuration")) { 200 factory = new MetricsFactory(projectName, new MetricsConfigurationLoader(commandLine.getToggleSwitch("validate")).load(commandLine.getSingleSwitch("configuration"))); 201 } else { 202 factory = new MetricsFactory(projectName, new MetricsConfigurationLoader(commandLine.getToggleSwitch("validate")).load(commandLine.getSingleSwitch("default-configuration"))); 203 } 204 205 Logger.getLogger(OOMetrics.class).debug("Computing metrics ..."); 206 207 com.jeantessier.metrics.MetricsGatherer gatherer = new com.jeantessier.metrics.MetricsGatherer(projectName, factory); 208 if (commandLine.isPresent("scope-includes-list") || commandLine.isPresent("scope-excludes-list")) { 209 gatherer.setScopeIncludes(createCollection(commandLine.getMultipleSwitch("scope-includes-list"), commandLine.getMultipleSwitch("scope-excludes-list"))); 210 } 211 if (commandLine.isPresent("filter-includes-list") || commandLine.isPresent("filter-excludes-list")) { 212 gatherer.setFilterIncludes(createCollection(commandLine.getMultipleSwitch("filter-includes-list"), commandLine.getMultipleSwitch("filter-excludes-list"))); 213 } 214 gatherer.addMetricsListener(verboseListener); 215 gatherer.visitClassfiles(loader.getAllClassfiles()); 216 217 if (commandLine.isPresent("show-all-metrics")) { 218 Iterator i; 219 220 i = gatherer.getMetricsFactory().getAllClassMetrics().iterator(); 221 while (i.hasNext()) { 222 gatherer.getMetricsFactory().includeClassMetrics((Metrics) i.next()); 223 } 224 225 i = gatherer.getMetricsFactory().getAllMethodMetrics().iterator(); 226 while (i.hasNext()) { 227 gatherer.getMetricsFactory().includeMethodMetrics((Metrics) i.next()); 228 } 229 } 230 231 Logger.getLogger(OOMetrics.class).debug("Printing results ..."); 232 verboseListener.print("Printing results ..."); 233 234 if (commandLine.isPresent("csv")) { 235 printCSVFiles(start, commandLine, gatherer.getMetricsFactory()); 236 } else if (commandLine.isPresent("txt")) { 237 printTextFile(start, commandLine, gatherer.getMetricsFactory()); 238 } else if (commandLine.isPresent("xml")) { 239 printXMLFile(start, commandLine, gatherer.getMetricsFactory()); 240 } 241 242 Logger.getLogger(OOMetrics.class).debug("Done."); 243 244 Date end = new Date(); 245 246 if (commandLine.getToggleSwitch("time")) { 247 System.err.println(OOMetrics.class.getName() + ": " + ((end.getTime() - (double) start.getTime()) / 1000) + " secs."); 248 } 249 250 verboseListener.close(); 251 } 252 253 private static Collection createCollection(Collection includes, Collection excludes) throws IOException { 254 Collection result = new HashSet(); 255 Iterator i; 256 257 i = includes.iterator(); 258 while (i.hasNext()) { 259 BufferedReader reader = new BufferedReader(new FileReader(i.next().toString())); 260 String line; 261 while ((line = reader.readLine()) != null) { 262 result.add(line); 263 } 264 reader.close(); 265 } 266 267 i = excludes.iterator(); 268 while (i.hasNext()) { 269 BufferedReader reader = new BufferedReader(new FileReader(i.next().toString())); 270 String line; 271 while ((line = reader.readLine()) != null) { 272 result.remove(line); 273 } 274 reader.close(); 275 } 276 277 return result; 278 } 279 280 private static void printCSVFiles(Date start, CommandLine commandLine, MetricsFactory factory) throws IOException { 281 MetricsComparator comparator = new MetricsComparator(commandLine.getSingleSwitch("sort")); 282 if (commandLine.getToggleSwitch("reverse")) { 283 comparator.reverse(); 284 } 285 286 List metrics; 287 Iterator i; 288 com.jeantessier.metrics.Printer printer; 289 PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); 290 291 if (commandLine.getToggleSwitch("project") || commandLine.getToggleSwitch("all")) { 292 if (commandLine.isPresent("out")) { 293 out = new PrintWriter(new FileWriter(commandLine.getSingleSwitch("out") + "_project.csv")); 294 } else { 295 out.println("Project:"); 296 } 297 298 metrics = new ArrayList(factory.getProjectMetrics()); 299 Collections.sort(metrics, comparator); 300 printer = new com.jeantessier.metrics.CSVPrinter(out, factory.getConfiguration().getProjectMeasurements()); 301 printer.setShowEmptyMetrics(commandLine.isPresent("show-empty-metrics")); 302 printer.setShowHiddenMeasurements(commandLine.isPresent("show-hidden-measurements")); 303 if (commandLine.isPresent("indent-text")) { 304 printer.setIndentText(commandLine.getSingleSwitch("indent-text")); 305 } 306 307 printer.visitMetrics(metrics); 308 309 if (commandLine.isPresent("out")) { 310 out.close(); 311 } else { 312 out.println(); 313 } 314 } 315 316 if (commandLine.getToggleSwitch("groups") || commandLine.getToggleSwitch("all")) { 317 if (commandLine.isPresent("out")) { 318 out = new PrintWriter(new FileWriter(commandLine.getSingleSwitch("out") + "_groups.csv")); 319 } else { 320 out.println("Packages:"); 321 } 322 323 metrics = new ArrayList(factory.getGroupMetrics()); 324 Collections.sort(metrics, comparator); 325 printer = new com.jeantessier.metrics.CSVPrinter(out, factory.getConfiguration().getGroupMeasurements()); 326 printer.setShowEmptyMetrics(commandLine.isPresent("show-empty-metrics")); 327 printer.setShowHiddenMeasurements(commandLine.isPresent("show-hidden-measurements")); 328 if (commandLine.isPresent("indent-text")) { 329 printer.setIndentText(commandLine.getSingleSwitch("indent-text")); 330 } 331 332 printer.visitMetrics(metrics); 333 334 if (commandLine.isPresent("out")) { 335 out.close(); 336 } else { 337 out.println(); 338 } 339 } 340 341 if (commandLine.getToggleSwitch("classes") || commandLine.getToggleSwitch("all")) { 342 if (commandLine.isPresent("out")) { 343 out = new PrintWriter(new FileWriter(commandLine.getSingleSwitch("out") + "_classes.csv")); 344 } else { 345 out.println("Classes:"); 346 } 347 348 metrics = new ArrayList(factory.getClassMetrics()); 349 Collections.sort(metrics, comparator); 350 printer = new com.jeantessier.metrics.CSVPrinter(out, factory.getConfiguration().getClassMeasurements()); 351 printer.setShowEmptyMetrics(commandLine.isPresent("show-empty-metrics")); 352 printer.setShowHiddenMeasurements(commandLine.isPresent("show-hidden-measurements")); 353 if (commandLine.isPresent("indent-text")) { 354 printer.setIndentText(commandLine.getSingleSwitch("indent-text")); 355 } 356 357 printer.visitMetrics(metrics); 358 359 if (commandLine.isPresent("out")) { 360 out.close(); 361 } else { 362 out.println(); 363 } 364 } 365 366 if (commandLine.getToggleSwitch("methods") || commandLine.getToggleSwitch("all")) { 367 if (commandLine.isPresent("out")) { 368 out = new PrintWriter(new FileWriter(commandLine.getSingleSwitch("out") + "_methods.csv")); 369 } else { 370 out.println("Methods:"); 371 } 372 373 metrics = new ArrayList(factory.getMethodMetrics()); 374 Collections.sort(metrics, comparator); 375 printer = new com.jeantessier.metrics.CSVPrinter(out, factory.getConfiguration().getMethodMeasurements()); 376 printer.setShowEmptyMetrics(commandLine.isPresent("show-empty-metrics")); 377 printer.setShowHiddenMeasurements(commandLine.isPresent("show-hidden-measurements")); 378 if (commandLine.isPresent("indent-text")) { 379 printer.setIndentText(commandLine.getSingleSwitch("indent-text")); 380 } 381 382 printer.visitMetrics(metrics); 383 384 if (commandLine.isPresent("out")) { 385 out.close(); 386 } 387 } 388 } 389 390 private static void printTextFile(Date start, CommandLine commandLine, MetricsFactory factory) throws IOException { 391 PrintWriter out; 392 if (commandLine.isPresent("out")) { 393 out = new PrintWriter(new FileWriter(commandLine.getSingleSwitch("out") + ".txt")); 394 } else { 395 out = new PrintWriter(new OutputStreamWriter(System.out)); 396 } 397 398 MetricsComparator comparator = new MetricsComparator(commandLine.getSingleSwitch("sort")); 399 if (commandLine.getToggleSwitch("reverse")) { 400 comparator.reverse(); 401 } 402 403 List metrics; 404 Iterator i; 405 406 if (commandLine.getToggleSwitch("project") || commandLine.getToggleSwitch("all")) { 407 out.println("Project metrics"); 408 out.println("---------------"); 409 metrics = new ArrayList(factory.getProjectMetrics()); 410 Collections.sort(metrics, comparator); 411 com.jeantessier.metrics.TextPrinter printer = new com.jeantessier.metrics.TextPrinter(out, factory.getConfiguration().getProjectMeasurements()); 412 printer.setExpandCollectionMeasurements(commandLine.getToggleSwitch("expand")); 413 printer.setShowEmptyMetrics(commandLine.isPresent("show-empty-metrics")); 414 printer.setShowHiddenMeasurements(commandLine.isPresent("show-hidden-measurements")); 415 if (commandLine.isPresent("indent-text")) { 416 printer.setIndentText(commandLine.getSingleSwitch("indent-text")); 417 } 418 419 printer.visitMetrics(metrics); 420 421 out.println(); 422 } 423 424 if (commandLine.getToggleSwitch("groups") || commandLine.getToggleSwitch("all")) { 425 out.println("Group metrics"); 426 out.println("-------------"); 427 metrics = new ArrayList(factory.getGroupMetrics()); 428 Collections.sort(metrics, comparator); 429 com.jeantessier.metrics.TextPrinter printer = new com.jeantessier.metrics.TextPrinter(out, factory.getConfiguration().getGroupMeasurements()); 430 printer.setExpandCollectionMeasurements(commandLine.getToggleSwitch("expand")); 431 printer.setShowEmptyMetrics(commandLine.isPresent("show-empty-metrics")); 432 printer.setShowHiddenMeasurements(commandLine.isPresent("show-hidden-measurements")); 433 if (commandLine.isPresent("indent-text")) { 434 printer.setIndentText(commandLine.getSingleSwitch("indent-text")); 435 } 436 437 printer.visitMetrics(metrics); 438 439 out.println(); 440 } 441 442 if (commandLine.getToggleSwitch("classes") || commandLine.getToggleSwitch("all")) { 443 out.println("Class metrics"); 444 out.println("-------------"); 445 metrics = new ArrayList(factory.getClassMetrics()); 446 Collections.sort(metrics, comparator); 447 com.jeantessier.metrics.TextPrinter printer = new com.jeantessier.metrics.TextPrinter(out, factory.getConfiguration().getClassMeasurements()); 448 printer.setExpandCollectionMeasurements(commandLine.getToggleSwitch("expand")); 449 printer.setShowEmptyMetrics(commandLine.isPresent("show-empty-metrics")); 450 printer.setShowHiddenMeasurements(commandLine.isPresent("show-hidden-measurements")); 451 if (commandLine.isPresent("indent-text")) { 452 printer.setIndentText(commandLine.getSingleSwitch("indent-text")); 453 } 454 455 printer.visitMetrics(metrics); 456 457 out.println(); 458 } 459 460 if (commandLine.getToggleSwitch("methods") || commandLine.getToggleSwitch("all")) { 461 out.println("Method metrics"); 462 out.println("--------------"); 463 metrics = new ArrayList(factory.getMethodMetrics()); 464 Collections.sort(metrics, comparator); 465 com.jeantessier.metrics.TextPrinter printer = new com.jeantessier.metrics.TextPrinter(out, factory.getConfiguration().getMethodMeasurements()); 466 printer.setExpandCollectionMeasurements(commandLine.getToggleSwitch("expand")); 467 printer.setShowEmptyMetrics(commandLine.isPresent("show-empty-metrics")); 468 printer.setShowHiddenMeasurements(commandLine.isPresent("show-hidden-measurements")); 469 if (commandLine.isPresent("indent-text")) { 470 printer.setIndentText(commandLine.getSingleSwitch("indent-text")); 471 } 472 473 printer.visitMetrics(metrics); 474 475 out.println(); 476 } 477 478 out.close(); 479 } 480 481 private static void printXMLFile(Date start, CommandLine commandLine, MetricsFactory factory) throws IOException { 482 PrintWriter out; 483 if (commandLine.isPresent("out")) { 484 out = new PrintWriter(new FileWriter(commandLine.getSingleSwitch("out") + ".xml")); 485 } else { 486 out = new PrintWriter(System.out); 487 } 488 489 MetricsComparator comparator = new MetricsComparator(commandLine.getSingleSwitch("sort")); 490 if (commandLine.getToggleSwitch("reverse")) { 491 comparator.reverse(); 492 } 493 494 List metrics; 495 Iterator i; 496 com.jeantessier.metrics.Printer printer; 497 498 metrics = new ArrayList(factory.getProjectMetrics()); 499 Collections.sort(metrics, comparator); 500 printer = new com.jeantessier.metrics.XMLPrinter(out, factory.getConfiguration(), commandLine.getSingleSwitch("encoding"), commandLine.getSingleSwitch("dtd-prefix")); 501 printer.setShowEmptyMetrics(commandLine.isPresent("show-empty-metrics")); 502 printer.setShowHiddenMeasurements(commandLine.isPresent("show-hidden-measurements")); 503 if (commandLine.isPresent("indent-text")) { 504 printer.setIndentText(commandLine.getSingleSwitch("indent-text")); 505 } 506 507 printer.visitMetrics(metrics); 508 509 out.close(); 510 } 511 } 512 | Popular Tags |