Convert the Bazel JUnit4 test runner from Guice to Dagger.

--
MOS_MIGRATED_REVID=124008772
diff --git a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/BUILD b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/BUILD
index 78ed2d1..b99b06c 100644
--- a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/BUILD
+++ b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/BUILD
@@ -18,8 +18,8 @@
         "//src/java_tools/junitrunner/java/com/google/testing/junit/runner/internal",
         "//src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4",
         "//src/java_tools/junitrunner/java/com/google/testing/junit/runner/model",
+        "//third_party:dagger",
         "//third_party:guava",
-        "//third_party:guice",
         "//third_party:joda_time",
         "//third_party:jsr305",
         "//third_party:junit4",
diff --git a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/BazelTestRunner.java b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/BazelTestRunner.java
index efde522..0e39d26 100644
--- a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/BazelTestRunner.java
+++ b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/BazelTestRunner.java
@@ -14,29 +14,30 @@
 
 package com.google.testing.junit.runner;
 
-import com.google.common.collect.ImmutableList;
 import com.google.common.util.concurrent.Uninterruptibles;
-import com.google.inject.AbstractModule;
-import com.google.inject.Guice;
-import com.google.inject.Injector;
-import com.google.inject.Provides;
-import com.google.inject.Singleton;
 import com.google.testing.junit.runner.internal.StackTraces;
 import com.google.testing.junit.runner.internal.Stderr;
 import com.google.testing.junit.runner.internal.Stdout;
+import com.google.testing.junit.runner.junit4.JUnit4InstanceModules.Config;
+import com.google.testing.junit.runner.junit4.JUnit4InstanceModules.SuiteClass;
 import com.google.testing.junit.runner.junit4.JUnit4Runner;
 import com.google.testing.junit.runner.junit4.JUnit4RunnerModule;
 import com.google.testing.junit.runner.model.AntXmlResultWriter;
 import com.google.testing.junit.runner.model.XmlResultWriter;
 
+import dagger.Component;
+import dagger.Module;
+import dagger.Provides;
+
 import org.joda.time.DateTime;
 import org.joda.time.format.DateTimeFormat;
 import org.joda.time.format.DateTimeFormatter;
 
 import java.io.PrintStream;
-import java.util.List;
 import java.util.concurrent.TimeUnit;
 
+import javax.inject.Singleton;
+
 /**
  * A class to run JUnit tests in a controlled environment.
  *
@@ -142,13 +143,21 @@
       }
     }
 
-    Injector injector = Guice.createInjector(
-        new BazelTestRunnerModule(suite, ImmutableList.copyOf(args)));
-
-    JUnit4Runner runner = injector.getInstance(JUnit4Runner.class);
+    JUnit4Runner runner =
+        DaggerBazelTestRunner_JUnit4Bazel.builder()
+            .suiteClass(new SuiteClass(suite))
+            .config(new Config(args))
+            .build()
+            .runner();
     return runner.run().wasSuccessful() ? 0 : 1;
   }
 
+  @Singleton
+  @Component(modules = {BazelTestRunnerModule.class})
+  interface JUnit4Bazel {
+    JUnit4Runner runner();
+  }
+
   private static Class<?> getTestClass(String name) {
     if (name == null) {
       return null;
@@ -181,29 +190,25 @@
     thread.start();
   }
 
-  static class BazelTestRunnerModule extends AbstractModule {
-    final Class<?> suite;
-    final List<String> args;
-
-    BazelTestRunnerModule(Class<?> suite, List<String> args) {
-      this.suite = suite;
-      this.args = args;
+  @Module(includes = JUnit4RunnerModule.class)
+  static class BazelTestRunnerModule {
+    @Provides
+    static XmlResultWriter resultWriter(AntXmlResultWriter impl) {
+      return impl;
     }
 
-    @Override
-    protected void configure() {
-      install(JUnit4RunnerModule.create(suite, args));
-      bind(XmlResultWriter.class).to(AntXmlResultWriter.class);
-    }
-
-    @Provides @Singleton @Stdout
-    PrintStream provideStdoutStream() {
+    @Provides
+    @Singleton
+    @Stdout
+    static PrintStream stdoutStream() {
       return System.out;
     }
 
-    @Provides @Singleton @Stderr
-    PrintStream provideStderrStream() {
+    @Provides
+    @Singleton
+    @Stderr
+    static PrintStream stderrStream() {
       return System.err;
     }
-  };
+  }
 }
diff --git a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/Args.java b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/Args.java
new file mode 100644
index 0000000..9093891
--- /dev/null
+++ b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/Args.java
@@ -0,0 +1,31 @@
+// Copyright 2016 The Bazel Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.testing.junit.runner.junit4;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+import javax.inject.Qualifier;
+
+/**
+ * Binding annotation that indicates that the given {@code Collection<String>} or
+ * {@code String[]} represents the command-line arguments of the runner.
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target({ElementType.PARAMETER, ElementType.METHOD})
+@Qualifier
+@interface Args {}
diff --git a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/BUILD b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/BUILD
index 0141b76..f5f2ecd 100644
--- a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/BUILD
+++ b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/BUILD
@@ -21,8 +21,8 @@
         "//src/java_tools/junitrunner/java/com/google/testing/junit/runner/sharding",
         "//src/java_tools/junitrunner/java/com/google/testing/junit/runner/sharding/api",
         "//src/java_tools/junitrunner/java/com/google/testing/junit/runner/util",
+        "//third_party:dagger",
         "//third_party:guava",
-        "//third_party:guice",
         "//third_party:joda_time",
         "//third_party:jsr305",
         "//third_party:jsr330_inject",
diff --git a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/CancellableRequestFactory.java b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/CancellableRequestFactory.java
index e899166..371f0bd 100644
--- a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/CancellableRequestFactory.java
+++ b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/CancellableRequestFactory.java
@@ -15,7 +15,6 @@
 package com.google.testing.junit.runner.junit4;
 
 import com.google.common.base.Preconditions;
-import com.google.inject.Singleton;
 import com.google.testing.junit.junit4.runner.MemoizingRequest;
 import com.google.testing.junit.junit4.runner.RunNotifierWrapper;
 
@@ -25,6 +24,9 @@
 import org.junit.runner.notification.RunNotifier;
 import org.junit.runner.notification.StoppedByUserException;
 
+import javax.inject.Inject;
+import javax.inject.Singleton;
+
 /**
  * Creates requests that can be cancelled.
  */
@@ -34,6 +36,9 @@
   private volatile ThreadSafeRunNotifier currentNotifier;
   private volatile boolean cancelRequested = false;
 
+  @Inject
+  CancellableRequestFactory() {}
+
   /**
    * Creates a request that can be cancelled. Can only be called once.
    *
diff --git a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4InstanceModules.java b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4InstanceModules.java
new file mode 100644
index 0000000..966478d
--- /dev/null
+++ b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4InstanceModules.java
@@ -0,0 +1,88 @@
+// Copyright 2012 The Bazel Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.testing.junit.runner.junit4;
+
+import com.google.common.base.Optional;
+import com.google.common.collect.ImmutableList;
+
+import dagger.Module;
+import dagger.Provides;
+
+import java.nio.file.Path;
+
+import javax.inject.Singleton;
+
+/**
+ * Dagger modules which hold state or are, for testing purposes, implemented with non-static
+ * provider methods.  These types are collected here so they can be cleanly named in the
+ * component builder, but still be obvious in module includes and component declarations.
+ */
+public final class JUnit4InstanceModules {
+
+  /**
+   * A stateful dagger module that holds the supplied test suite class.
+   */
+  @Module
+  public static final class SuiteClass {
+    private final Class<?> suiteClass;
+
+    public SuiteClass(Class<?> suiteClass) {
+      this.suiteClass = suiteClass;
+    }
+
+    @Provides
+    @TopLevelSuite
+    Class<?> topLevelSuite() {
+      return suiteClass;
+    }
+
+    @Provides
+    @TopLevelSuite
+    static String topLevelSuiteName(@TopLevelSuite Class<?> suite) {
+      return suite.getCanonicalName();
+    }
+  }
+
+  /**
+   * A module which supplies a JUnit4Config object, which can be overridden at test-time.
+   */
+  @Module
+  public static final class Config {
+    private final ImmutableList<String> args;
+
+    /**
+     * Creates a module that can provide a {@link JUnit4Config} from supplied command-line
+     * arguments
+     */
+    public Config(String... args) {
+      this.args = ImmutableList.copyOf(args);
+    }
+
+    @Provides
+    @Singleton
+    JUnit4Options options() {
+      return JUnit4Options.parse(System.getenv(), ImmutableList.copyOf(args));
+    }
+
+    @Provides
+    @Singleton
+    JUnit4Config config(JUnit4Options options) {
+      return new JUnit4Config(
+          options.getTestIncludeFilter(), options.getTestExcludeFilter(), Optional.<Path>absent());
+    }
+  }
+
+  private JUnit4InstanceModules() {}
+}
diff --git a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4Runner.java b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4Runner.java
index 4134131..10447a7 100644
--- a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4Runner.java
+++ b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4Runner.java
@@ -51,6 +51,7 @@
   private final PrintStream testRunnerOut;
   private final JUnit4Config config;
   private final Set<RunListener> runListeners;
+  private final Set<Initializer> initializers;
 
   private GoogleTestSecurityManager googleTestSecurityManager;
   private SecurityManager previousSecurityManager;
@@ -59,15 +60,21 @@
    * Creates a runner.
    */
   @Inject
-  private JUnit4Runner(Request request, CancellableRequestFactory requestFactory,
-      Supplier<TestSuiteModel> modelSupplier, @Stdout PrintStream testRunnerOut,
-      JUnit4Config config, Set<RunListener> runListeners) {
+  JUnit4Runner(
+      Request request,
+      CancellableRequestFactory requestFactory,
+      Supplier<TestSuiteModel> modelSupplier,
+      @Stdout PrintStream testRunnerOut,
+      JUnit4Config config,
+      Set<RunListener> runListeners,
+      Set<Initializer> initializers) {
     this.request = request;
     this.requestFactory = requestFactory;
     this.modelSupplier = modelSupplier;
     this.config = config;
     this.testRunnerOut = testRunnerOut;
     this.runListeners = runListeners;
+    this.initializers = initializers;
   }
 
   /**
@@ -79,6 +86,10 @@
     testRunnerOut.println("JUnit4 Test Runner");
     checkJUnitRunnerApiVersion();
 
+    for (Initializer init : initializers) {
+      init.initialize();
+    }
+
     // Sharding
     TestSuiteModel model = modelSupplier.get();
     Filter shardingFilter = model.getShardingFilter();
@@ -261,4 +272,15 @@
     public void run(RunNotifier notifier) {
     }
   }
+
+  /**
+   * A simple initializer which can be used to provide additional initialization logic in custom
+   * runners.
+   *
+   * <p>Initializers will be run in unspecified order. If an exception is thrown it will not be
+   * deemed recoverable and will cause the runner to error-out.
+   */
+  public interface Initializer {
+    void initialize();
+  }
 }
diff --git a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4RunnerBaseModule.java b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4RunnerBaseModule.java
index 28e6466..c674232 100644
--- a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4RunnerBaseModule.java
+++ b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4RunnerBaseModule.java
@@ -14,21 +14,21 @@
 
 package com.google.testing.junit.runner.junit4;
 
-import static com.google.inject.multibindings.Multibinder.newSetBinder;
 import static com.google.testing.junit.runner.sharding.ShardingFilters.DEFAULT_SHARDING_STRATEGY;
+import static dagger.Provides.Type.SET;
 
 import com.google.common.base.Supplier;
 import com.google.common.base.Suppliers;
-import com.google.inject.AbstractModule;
-import com.google.inject.Key;
-import com.google.inject.Provides;
-import com.google.inject.Singleton;
-import com.google.inject.multibindings.Multibinder;
 import com.google.testing.junit.junit4.runner.MemoizingRequest;
 import com.google.testing.junit.runner.internal.Stdout;
+import com.google.testing.junit.runner.junit4.JUnit4InstanceModules.SuiteClass;
 import com.google.testing.junit.runner.model.TestSuiteModel;
 import com.google.testing.junit.runner.sharding.api.ShardingFilterFactory;
 
+import dagger.Module;
+import dagger.Multibindings;
+import dagger.Provides;
+
 import org.junit.internal.TextListener;
 import org.junit.runner.Request;
 import org.junit.runner.notification.RunListener;
@@ -37,50 +37,44 @@
 import java.io.PrintStream;
 import java.io.UnsupportedEncodingException;
 import java.nio.charset.StandardCharsets;
+import java.util.Set;
+
+import javax.inject.Singleton;
 
 /**
- * Guice module for creating {@link JUnit4Runner}. This contains the common
+ * Dagger module for creating a {@link JUnit4Runner}. This contains the common
  * bindings used when either the runner runs actual tests or when we do
  * integration tests of the runner itself.
  *
- * <p>Note: we do not use {@code Modules.override()} to test the runner because
- * there are bindings that we use when the runner runs actual tests that set
- * global state, and we don't want to do that when we test the runner itself.
  */
-class JUnit4RunnerBaseModule extends AbstractModule {
-  private final Class<?> suiteClass;
+@Module(includes = SuiteClass.class)
+public final class JUnit4RunnerBaseModule {
 
-  public JUnit4RunnerBaseModule(Class<?> suiteClass) {
-    this.suiteClass = suiteClass;
+  @Multibindings
+  interface MultiBindings {
+    Set<JUnit4Runner.Initializer> initializers();
   }
 
-  @Override
-  protected void configure() {
-    requireBinding(Key.get(PrintStream.class, Stdout.class));
-    requireBinding(JUnit4Config.class);
-    requireBinding(TestSuiteModel.Builder.class);
-
-    // We require explicit bindings so we don't use an unexpected just-in-time binding
-    bind(JUnit4Runner.class);
-    bind(JUnit4TestModelBuilder.class);
-    bind(CancellableRequestFactory.class);
-
-    // Normal bindings
-    bind(ShardingFilterFactory.class).toInstance(DEFAULT_SHARDING_STRATEGY);
-    bindConstant().annotatedWith(TopLevelSuite.class).to(suiteClass.getCanonicalName());
-
-    // Bind listeners
-    Multibinder<RunListener> listenerBinder = newSetBinder(binder(), RunListener.class);
-    listenerBinder.addBinding().to(TextListener.class);
+  @Provides
+  static ShardingFilterFactory shardingFilterFactory() {
+    return DEFAULT_SHARDING_STRATEGY;
   }
 
-  @Provides @Singleton
-  Supplier<TestSuiteModel> provideTestSuiteModelSupplier(JUnit4TestModelBuilder builder) {
+  @Provides(type = SET)
+  static RunListener textListener(TextListener impl) {
+    return impl;
+  }
+
+
+  @Provides
+  @Singleton
+  static Supplier<TestSuiteModel> provideTestSuiteModelSupplier(JUnit4TestModelBuilder builder) {
     return Suppliers.memoize(builder);
   }
 
-  @Provides @Singleton
-  TextListener provideTextListener(@Stdout PrintStream testRunnerOut) {
+  @Provides
+  @Singleton
+  static TextListener provideTextListener(@Stdout PrintStream testRunnerOut) {
     return new TextListener(asUtf8PrintStream(testRunnerOut));
   }
 
@@ -92,8 +86,9 @@
     }
   }
 
-  @Provides @Singleton
-  Request provideRequest() {
+  @Provides
+  @Singleton
+  static Request provideRequest(@TopLevelSuite Class<?> suiteClass) {
     /*
      * JUnit4Runner requests the Runner twice, once to build the model (before
      * filtering) and once to run the tests (after filtering). Constructing the
diff --git a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4RunnerModule.java b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4RunnerModule.java
index 5f15524..91cfd1d 100644
--- a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4RunnerModule.java
+++ b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4RunnerModule.java
@@ -14,73 +14,61 @@
 
 package com.google.testing.junit.runner.junit4;
 
-import static com.google.inject.multibindings.Multibinder.newSetBinder;
+import static dagger.Provides.Type.SET;
 
 import com.google.common.base.Optional;
 import com.google.common.base.Ticker;
-import com.google.common.collect.ImmutableList;
 import com.google.common.io.ByteStreams;
-import com.google.inject.AbstractModule;
-import com.google.inject.Provides;
-import com.google.inject.Singleton;
-import com.google.inject.multibindings.Multibinder;
 import com.google.testing.junit.runner.internal.SignalHandlers;
 import com.google.testing.junit.runner.util.TestNameProvider;
 
+import dagger.Module;
+import dagger.Provides;
+
 import org.junit.runner.notification.RunListener;
 
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.OutputStream;
 import java.nio.file.Path;
-import java.util.List;
+
+import javax.inject.Singleton;
 
 /**
- * Guice module for real test runs.
+ * Dagger module for real test runs.
  */
-public class JUnit4RunnerModule extends AbstractModule {
-  private final Class<?> suite;
-  private final JUnit4Config config;
-  private final ImmutableList<String> unparsedArgs;
-
-  public static JUnit4RunnerModule create(Class<?> suite, List<String> args) {
-    JUnit4Options options = JUnit4Options.parse(System.getenv(), ImmutableList.copyOf(args));
-    JUnit4Config config = new JUnit4Config(
-        options.getTestIncludeFilter(),
-        options.getTestExcludeFilter(),
-        Optional.<Path>absent());
-    return new JUnit4RunnerModule(suite, config, ImmutableList.copyOf(options.getUnparsedArgs()));
+@Module(includes = {JUnit4RunnerBaseModule.class, JUnit4InstanceModules.Config.class})
+public final class JUnit4RunnerModule {
+  @Provides
+  static Ticker ticker() {
+    return Ticker.systemTicker();
   }
 
-  private JUnit4RunnerModule(
-      Class<?> suite, JUnit4Config config, ImmutableList<String> unparsedArgs) {
-    this.suite = suite;
-    this.config = config;
-    this.unparsedArgs = unparsedArgs;
+  @Provides
+  static SignalHandlers.HandlerInstaller signalHandlerInstaller() {
+    return SignalHandlers.createRealHandlerInstaller();
   }
 
-  @Override
-  protected void configure() {
-    install(new JUnit4RunnerBaseModule(suite));
-
-    // We require explicit bindings so we don't use an unexpected just-in-time binding
-    bind(SignalHandlers.class);
-
-    // Normal bindings
-    bind(JUnit4Config.class).toInstance(config);
-    bind(Ticker.class).toInstance(Ticker.systemTicker());
-    bind(SignalHandlers.HandlerInstaller.class).toInstance(
-        SignalHandlers.createRealHandlerInstaller());
-
-    // Bind listeners
-    Multibinder<RunListener> listenerBinder = newSetBinder(binder(), RunListener.class);
-    listenerBinder.addBinding().to(JUnit4TestNameListener.class);
-    listenerBinder.addBinding().to(JUnit4TestXmlListener.class);
-    listenerBinder.addBinding().to(JUnit4TestStackTraceListener.class);
+  @Provides(type = SET)
+  static RunListener nameListener(JUnit4TestNameListener impl) {
+    return impl;
   }
 
-  @Provides @Singleton @Xml
-  OutputStream provideXmlStream() {
+  @Provides(type = SET)
+  static RunListener xmlListener(JUnit4TestXmlListener impl) {
+    return impl;
+  }
+
+  @Provides(type = SET)
+  static RunListener stackTraceListener(JUnit4TestStackTraceListener impl) {
+    return impl;
+  }
+
+
+  @Provides
+  @Singleton
+  @Xml
+  static OutputStream provideXmlStream(JUnit4Config config) {
     Optional<Path> path = config.getXmlOutputPath();
 
     if (path.isPresent()) {
@@ -103,16 +91,10 @@
   @Provides @Singleton
   SettableCurrentRunningTest provideCurrentRunningTest() {
     return new SettableCurrentRunningTest() {
+      @Override
       void setGlobalTestNameProvider(TestNameProvider provider) {
         testNameProvider = provider;
       }
     };
   }
-
-  /**
-   * Gets the list of unparsed command line arguments.
-   */
-  public ImmutableList<String> getUnparsedArgs() {
-    return unparsedArgs;
-  }
 }
diff --git a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4TestModelBuilder.java b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4TestModelBuilder.java
index 62df88d..5dd98e9 100644
--- a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4TestModelBuilder.java
+++ b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4TestModelBuilder.java
@@ -16,7 +16,6 @@
 
 import com.google.common.base.Preconditions;
 import com.google.common.base.Supplier;
-import com.google.inject.Singleton;
 import com.google.testing.junit.runner.model.TestSuiteModel;
 import com.google.testing.junit.runner.model.TestSuiteModel.Builder;
 
@@ -24,6 +23,7 @@
 import org.junit.runner.Request;
 
 import javax.inject.Inject;
+import javax.inject.Singleton;
 
 /**
  * Builds a {@link TestSuiteModel} for JUnit4 tests.
diff --git a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4TestNameListener.java b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4TestNameListener.java
index b7da73f..e1c2c54 100644
--- a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4TestNameListener.java
+++ b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4TestNameListener.java
@@ -14,15 +14,16 @@
 
 package com.google.testing.junit.runner.junit4;
 
-import com.google.inject.Inject;
-import com.google.inject.Singleton;
 import com.google.testing.junit.runner.util.TestNameProvider;
 
 import org.junit.runner.Description;
 import org.junit.runner.notification.RunListener;
 
+import javax.inject.Inject;
+import javax.inject.Singleton;
+
 /**
- * A listener to get the name of a JUnit4 test. 
+ * A listener to get the name of a JUnit4 test.
  */
 @Singleton
 class JUnit4TestNameListener extends RunListener {
diff --git a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4TestStackTraceListener.java b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4TestStackTraceListener.java
index abb0614..8f62b1d 100644
--- a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4TestStackTraceListener.java
+++ b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4TestStackTraceListener.java
@@ -14,8 +14,6 @@
 
 package com.google.testing.junit.runner.junit4;
 
-import com.google.inject.Inject;
-import com.google.inject.Singleton;
 import com.google.testing.junit.runner.internal.SignalHandlers;
 import com.google.testing.junit.runner.internal.StackTraces;
 import com.google.testing.junit.runner.internal.Stderr;
@@ -28,6 +26,9 @@
 
 import java.io.PrintStream;
 
+import javax.inject.Inject;
+import javax.inject.Singleton;
+
 /**
  * A listener than dumps all stack traces when the test receives a SIGTERM.
  */
diff --git a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4TestXmlListener.java b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4TestXmlListener.java
index 5e572e1..609591b 100644
--- a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4TestXmlListener.java
+++ b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4/JUnit4TestXmlListener.java
@@ -15,8 +15,6 @@
 package com.google.testing.junit.runner.junit4;
 
 import com.google.common.base.Supplier;
-import com.google.inject.Inject;
-import com.google.inject.Singleton;
 import com.google.testing.junit.runner.internal.SignalHandlers;
 import com.google.testing.junit.runner.internal.Stderr;
 import com.google.testing.junit.runner.model.TestSuiteModel;
@@ -33,12 +31,15 @@
 import java.io.OutputStream;
 import java.io.PrintStream;
 
+import javax.inject.Inject;
+import javax.inject.Singleton;
+
 /**
  * A listener that writes the test output as XML.
  */
 @Singleton
 class JUnit4TestXmlListener extends RunListener  {
-  
+
   private final Supplier<TestSuiteModel> modelSupplier;
   private final CancellableRequestFactory requestFactory;
   private final SignalHandlers signalHandlers;
diff --git a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/model/AntXmlResultWriter.java b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/model/AntXmlResultWriter.java
index d2ead67..29d983a 100644
--- a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/model/AntXmlResultWriter.java
+++ b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/model/AntXmlResultWriter.java
@@ -25,6 +25,8 @@
 import java.io.StringWriter;
 import java.util.Map.Entry;
 
+import javax.inject.Inject;
+
 /**
  * Writes the JUnit test nodes and their results into Ant-JUnit XML. Ant-JUnit XML is not a
  * standardized format. For this implementation the
@@ -59,6 +61,9 @@
 
   private int testSuiteId;
 
+  @Inject
+  public AntXmlResultWriter() {}
+
   @Override
   public void writeTestSuites(XmlWriter writer, TestResult result) throws IOException {
     testSuiteId = 0;
diff --git a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/sharding/BUILD b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/sharding/BUILD
index 43a7d05..e442368 100644
--- a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/sharding/BUILD
+++ b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/sharding/BUILD
@@ -13,8 +13,8 @@
     srcs = glob(["*.java"]),
     deps = [
         "//src/java_tools/junitrunner/java/com/google/testing/junit/runner/sharding/api",
+        "//third_party:dagger",
         "//third_party:guava",
-        "//third_party:guice",
         "//third_party:jsr305",
         "//third_party:junit4",
     ],
diff --git a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/sharding/ShardingEnvironment.java b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/sharding/ShardingEnvironment.java
index 6d3836f..366f25a 100644
--- a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/sharding/ShardingEnvironment.java
+++ b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/sharding/ShardingEnvironment.java
@@ -20,6 +20,8 @@
 import java.io.File;
 import java.io.IOException;
 
+import javax.inject.Inject;
+
 /**
  * Utility class that encapsulates dependencies from sharding implementations
  * on the test environment.  See http://bazel.io/docs/test-sharding.html for a
@@ -35,6 +37,9 @@
   /** Usage: -Dtest.sharding.strategy=round_robin */
   private static final String TEST_SHARDING_STRATEGY = "test.sharding.strategy";
 
+  @Inject
+  public ShardingEnvironment() {}
+
   /**
    * Return true iff the current test should be sharded.
    */
diff --git a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/sharding/ShardingFilters.java b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/sharding/ShardingFilters.java
index 206a8e9..e149a74 100644
--- a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/sharding/ShardingFilters.java
+++ b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/sharding/ShardingFilters.java
@@ -14,7 +14,6 @@
 
 package com.google.testing.junit.runner.sharding;
 
-import com.google.inject.Inject;
 import com.google.testing.junit.runner.sharding.api.ShardingFilterFactory;
 
 import org.junit.runner.Description;
@@ -22,6 +21,8 @@
 
 import java.util.Collection;
 
+import javax.inject.Inject;
+
 /**
  * A factory for test sharding filters.
  */
@@ -54,7 +55,7 @@
       }
     }
   }
-  
+
   public static final ShardingFilterFactory DEFAULT_SHARDING_STRATEGY =
       ShardingStrategy.ROUND_ROBIN;
   private final ShardingEnvironment shardingEnvironment;
@@ -104,9 +105,9 @@
       } catch (ClassNotFoundException | InstantiationException |
           IllegalAccessException | IllegalArgumentException e2) {
         throw new RuntimeException(
-            "Could not create custom sharding strategy class " + strategy, e2);  
+            "Could not create custom sharding strategy class " + strategy, e2);
       }
     }
-    return shardingFilterFactory; 
+    return shardingFilterFactory;
   }
 }
diff --git a/third_party/BUILD b/third_party/BUILD
index 3584789..dd43335 100644
--- a/third_party/BUILD
+++ b/third_party/BUILD
@@ -183,7 +183,6 @@
         ":auto_service",
         ":auto_value_value",
         ":guava",
-        ":jsr305",
         ":tomcat_annotations_api",
     ],
 )
@@ -208,7 +207,10 @@
 java_library(
     name = "dagger",
     exported_plugins = [":dagger_plugin"],
-    exports = ["dagger_api"],
+    exports = [
+        ":dagger_api",
+        ":jsr330_inject",
+    ],
 )
 
 java_plugin(
@@ -218,6 +220,8 @@
         ":dagger_api",
         ":dagger_compiler",
         ":dagger_producers",
+        ":guava",
+        ":jsr330_inject",
     ],
 )