/** * Given a List<Object> with Dagger modules inside performs a plus over the application graph and * returns a new one with all the dependencies already created plus the one resolved with the * list of modules passed as argument. */ public ObjectGraph plusGraph(List<Object> activityScopeModules) { if (activityScopeModules == null) { throw new IllegalArgumentException( "You can't extend the application graph with a null list of modules"); } return graph.plus(activityScopeModules.toArray()); }
@Override public ObjectGraph plusGraph(List<Object> activityScopeModules) { ObjectGraph newObjectGraph; if (fakeObjectGraph == null) { newObjectGraph = super.plusGraph(activityScopeModules); } else { newObjectGraph = fakeObjectGraph.plus(activityScopeModules.toArray()); } return newObjectGraph; }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Create the activity graph by .plus-ing our modules onto the application graph. DemoApplication application = (DemoApplication) getApplication(); activityGraph = application.getApplicationGraph().plus(getModules().toArray()); // Inject ourselves so subclasses will have dependencies fulfilled when this method returns. activityGraph.inject(this); }
@Override public ObjectGraph getApplicationGraph() { if (mActivityGraph == null) { mActivityGraph = ((Injectable) getApplication()).getApplicationGraph() .plus(new ActivityModule(this), new UiModule()); } return mActivityGraph; }
@Override public void onCreate() { super.onCreate(); ((Injectable) getApplication()) .getApplicationGraph() .plus(new ActivityModule(this)) .inject(this); }
@Override public void onCreate() { super.onCreate(); ((Injectable) getApplication()) .getApplicationGraph() .plus(new ActivityModule(this)) .inject(this); synchronized (sItemSyncAdapterLock) { if (sItemSyncAdapter == null) { sItemSyncAdapter = new ItemSyncAdapter(getApplicationContext(), mFactory, mReadabilityClient); } } }
@Override public void onCreate() { super.onCreate(); ((Injectable) getApplication()) .getApplicationGraph() .plus(new ActivityModule(this)) .inject(this); }
@Test public void basicInjectionWithExtension() { ObjectGraph root = ObjectGraph.createWith(new TestingLoader(), new RootModule()); RealSingleton rs = root.get(RealSingleton.class); assertThat(rs.ints).containsExactly(0, 1); ObjectGraph extension = root.plus(new ExtensionModule()); Main main = extension.get(Main.class); assertThat(main.ints).containsExactly(0, 1, 2, 3); // Second time around. ObjectGraph extension2 = root.plus(new ExtensionModule()); Main main2 = extension2.get(Main.class); assertThat(main2.ints).containsExactly(0, 1, 4, 5); }
@Test public void basicInjectionWithExtensionAndOverrides() { try { ObjectGraph.createWith(new TestingLoader(), new RootModule()).plus(new TestModule()); fail("Should throw exception."); } catch (IllegalArgumentException e) { assertEquals("TestModule: Module overrides cannot contribute set bindings.", e.getMessage()); } }
@Test public void whenLibraryModulePlussedToNecessaryModule_shouldNotFailOnUnusedLibraryModule() throws Exception { class EntryPoint { } class BagOfMoney { } @Module(injects = EntryPoint.class, library = true) class ExampleLibraryModule { @Provides BagOfMoney providesMoney() { return new BagOfMoney(); } } @Module(injects = EntryPoint.class) class TestModule { } ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); graph = graph.plus(new ExampleLibraryModule()); graph.validate(); }
@Test public void basicInjectionWithExtension() { A a = new A(); ObjectGraph root = ObjectGraph.createWith(new TestingLoader(), new RootModule(a)); assertThat(root.get(A.class)).isSameAs(a); // Extension graph behaves as the root graph would for root-ish things. ObjectGraph extension = root.plus(new ExtensionModule()); assertThat(extension.get(A.class)).isSameAs(a); assertThat(extension.get(B.class).a).isSameAs(a); }
@Test public void testSingletonLinkingThroughExtensionGraph() { ObjectGraph root = ObjectGraph.createWith(new TestingLoader(), new RootModule()); // DO NOT CALL root.get(C.class)) HERE to get forced-linking behaviour from plus(); ObjectGraph extension = root.plus(new ExtensionModule()); assertThat(extension.get(SingletonLinkedFromExtension.class).c).isSameAs(root.get(C.class)); }
@Test public void basicExtension() { assertNotNull(ObjectGraph.createWith(new TestingLoader(), new RootModule()) .plus(new ExtensionModule())); }
@Test public void scopedGraphs() { ObjectGraph app = ObjectGraph.createWith(new TestingLoader(), new RootModule()); assertThat(app.get(A.class)).isNotNull(); assertThat(app.get(A.class)).isSameAs(app.get(A.class)); assertThat(app.get(B.class)).isNotSameAs(app.get(B.class)); assertFailInjectNotRegistered(app, C.class); assertFailInjectNotRegistered(app, D.class); ObjectGraph request1 = app.plus(new ExtensionModule()); ObjectGraph request2 = app.plus(new ExtensionModule()); for (ObjectGraph request : Arrays.asList(request1, request2)) { assertThat(request.get(A.class)).isNotNull(); assertThat(request.get(A.class)).isSameAs(request.get(A.class)); assertThat(request.get(B.class)).isNotSameAs(request.get(B.class)); assertThat(request.get(C.class)).isNotNull(); assertThat(request.get(C.class)).isSameAs(request.get(C.class)); assertThat(request.get(D.class)).isNotSameAs(request.get(D.class)); } // Singletons are one-per-graph-instance where they are declared. assertThat(request1.get(C.class)).isNotSameAs(request2.get(C.class)); // Singletons that come from common roots should be one-per-common-graph-instance. assertThat(request1.get(C.class).a).isSameAs(request2.get(C.class).a); }
@Test public void basicInjection() { ObjectGraph root = ObjectGraph.createWith(new TestingLoader(), new RootModule()); assertThat(root.get(A.class)).isNotNull(); assertThat(root.get(A.class)).isSameAs(root.get(A.class)); // Present and Singleton. assertThat(root.get(B.class)).isNotSameAs(root.get(B.class)); // Not singleton. assertFailInjectNotRegistered(root, C.class); // Not declared in RootModule. assertFailInjectNotRegistered(root, D.class); // Not declared in RootModule. // Extension graph behaves as the root graph would for root-ish things. ObjectGraph extension = root.plus(new ExtensionModule()); assertThat(root.get(A.class)).isSameAs(extension.get(A.class)); assertThat(root.get(B.class)).isNotSameAs(extension.get(B.class)); assertThat(root.get(B.class).a).isSameAs(extension.get(B.class).a); assertThat(extension.get(C.class).a).isNotNull(); assertThat(extension.get(D.class).c).isNotNull(); }
public DaggerInjector extend(Object... modules) { ObjectGraph objectGraph = this.objectGraph.plus(modules); return new DaggerInjector(objectGraph); }
objectGraph = objectGraph.plus(rootModule);
private Scoop getActivityScoop() { if (activityScoop == null) { Timber.d("getActivityScoop"); ObjectGraph activityGraph = getApp().getApplicationGraph().plus(new MainActivityModule(this)); DaggerInjector activityInjector = new DaggerInjector(activityGraph); activityScoop = new Scoop.Builder("activity_scoop") .service(DaggerInjector.SERVICE_NAME, activityInjector) .build(); } return activityScoop; }