PageRenderTime 15ms CodeModel.GetById 7ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/testability-explorer/src/main/java/com/google/test/metric/ConfigModule.java

http://testability-explorer.googlecode.com/
Java | 72 lines | 54 code | 10 blank | 8 comment | 0 complexity | 9d6465f6e28d620c86186a3cca326834 MD5 | raw file
 1// Copyright 2009 Google Inc. All Rights Reserved.
 2
 3package com.google.test.metric;
 4
 5import com.google.classpath.ClassPath;
 6import com.google.classpath.ClassPathFactory;
 7import com.google.inject.AbstractModule;
 8import com.google.inject.BindingAnnotation;
 9import com.google.inject.TypeLiteral;
10import com.google.inject.name.Names;
11import com.google.test.metric.ReportGeneratorProvider.ReportFormat;
12import com.google.test.metric.report.ReportOptions;
13
14import org.kohsuke.args4j.CmdLineException;
15import org.kohsuke.args4j.CmdLineParser;
16
17import java.io.PrintStream;
18import java.lang.annotation.Retention;
19import static java.lang.annotation.RetentionPolicy.RUNTIME;
20import java.util.List;
21
22/**
23 * @author alexeagle@google.com (Alex Eagle)
24 */
25public class ConfigModule extends AbstractModule {
26
27  @Retention(RUNTIME) @BindingAnnotation public @interface Error {}
28  @Retention(RUNTIME) @BindingAnnotation public @interface Output {}
29
30  private final String[] args;
31  private final PrintStream out;
32  private final PrintStream err;
33
34  public ConfigModule(String[] args, PrintStream out, PrintStream err) {
35    this.args = args;
36    this.out = out;
37    this.err = err;
38  }
39
40  @Override
41  protected void configure() {
42    // For printing errors to the caller
43    bind(PrintStream.class).annotatedWith(Output.class).toInstance(System.out);
44    bind(PrintStream.class).annotatedWith(Error.class).toInstance(System.err);
45    CommandLineConfig config = new CommandLineConfig(out, err);
46    CmdLineParser parser = new CmdLineParser(config);
47    // We actually do this work in configure, since we want to bind the parsed command line
48    // options at this time
49    try {
50      parser.parseArgument(args);
51      config.validate();
52      bind(ClassPath.class).toInstance(new ClassPathFactory().createFromPath(config.cp));
53      bind(ReportFormat.class).toInstance(config.format);      
54    } catch (CmdLineException e) {
55      err.println(e.getMessage() + "\n");
56      parser.setUsageWidth(120);
57      parser.printUsage(err);
58      err.println("Exiting...");
59    }
60    bind(CommandLineConfig.class).toInstance(config);
61    bind(ReportOptions.class).toInstance(new ReportOptions(
62        config.cyclomaticMultiplier, config.globalMultiplier, config.constructorMultiplier,
63        config.maxExcellentCost, config.maxAcceptableCost, config.worstOffenderCount,
64        config.maxMethodCount, config.maxLineCount, config.printDepth, config.minCost,
65        config.srcFileLineUrl, config.srcFileUrl));
66    bindConstant().annotatedWith(Names.named("printDepth")).to(config.printDepth);
67    bind(new TypeLiteral<List<String>>() {}).toInstance(config.entryList);
68
69    //TODO: install the appropriate language-specific module
70    install(new JavaTestabilityModule(config));
71  }
72}