/** Inject the supplied {@code object} using the activity-specific graph. */ public void inject(Object object) { activityGraph.inject(object); } }
public void inject(Object object) { graph.inject(object); } }
@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); }
private <T> T injectWithModule(T ep, Object ... modules) { return ObjectGraph.createWith(new TestingLoader(), modules).inject(ep); } }
@Test public void providerInjection() { class TestEntryPoint { @Inject Provider<A> aProvider; } @Module(injects = TestEntryPoint.class) class TestModule { } TestEntryPoint entryPoint = new TestEntryPoint(); ObjectGraph.createWith(new TestingLoader(), new TestModule()).inject(entryPoint); assertThat(entryPoint.aProvider.get()).isNotNull(); assertThat(entryPoint.aProvider.get()).isNotNull(); assertThat(entryPoint.aProvider.get()).isNotSameAs(entryPoint.aProvider.get()); }
private <T> T injectWithModule(T ep, Object ... modules) { return ObjectGraph.createWith(new TestingLoader(), modules).inject(ep); }
@Test public void singletons() { class TestEntryPoint { @Inject Provider<F> fProvider; @Inject Provider<I> iProvider; } @Module(injects = TestEntryPoint.class) class TestModule { @Provides @Singleton F provideF() { return new F(); } } TestEntryPoint entryPoint = new TestEntryPoint(); ObjectGraph.createWith(new TestingLoader(), new TestModule()).inject(entryPoint); assertThat(entryPoint.fProvider.get()).isSameAs(entryPoint.fProvider.get()); assertThat(entryPoint.iProvider.get()).isSameAs(entryPoint.iProvider.get()); }
@Test public void singletonsInjectedOnlyIntoProviders() { class TestEntryPoint { @Inject Provider<A> aProvider; } @Module(injects = TestEntryPoint.class) class TestModule { @Provides @Singleton A provideA() { return new A(); } } TestEntryPoint entryPoint = new TestEntryPoint(); ObjectGraph.createWith(new TestingLoader(), new TestModule()).inject(entryPoint); assertThat(entryPoint.aProvider.get()).isSameAs(entryPoint.aProvider.get()); }
@Test public void injectParameterizedType() { class TestEntryPoint { @Inject List<String> listOfStrings; } @Module(injects = TestEntryPoint.class) class TestModule { @Provides List<String> provideList() { return Arrays.asList("a", "b"); } } TestEntryPoint entryPoint = new TestEntryPoint(); ObjectGraph.createWith(new TestingLoader(), new TestModule()).inject(entryPoint); assertThat(entryPoint.listOfStrings).isEqualTo(Arrays.asList("a", "b")); }
@Test public void uninjectableSupertypes() { class TestEntryPoint { @Inject T t; } @Module(injects = TestEntryPoint.class) class TestModule { } TestEntryPoint entryPoint = new TestEntryPoint(); ObjectGraph.createWith(new TestingLoader(), new TestModule()).inject(entryPoint); assertThat(entryPoint.t).isNotNull(); }
@Test public void simpleInjectionWithUnGeneratedCode() { Entry$Point entryPoint = new Entry$Point(); ObjectGraph.create(new TestModule()).inject(entryPoint); assertThat(entryPoint.a).isEqualTo("a"); } }
@Test public void providesSet() { final Set<A> set = Collections.emptySet(); class TestEntryPoint { @Inject Set<A> set; } @Module(injects = TestEntryPoint.class) class TestModule { @Provides Set<A> provideSet() { return set; } } TestEntryPoint entryPoint = new TestEntryPoint(); TestModule module = new TestModule(); ObjectGraph.createWith(new TestingLoader(), module).inject(entryPoint); assertThat(entryPoint.set).isSameAs(set); }
@Test public void injectableSupertypes() { class TestEntryPoint { @Inject Q q; } @Module(injects = TestEntryPoint.class) class TestModule { @Provides F provideF() { return new F(); } } TestEntryPoint entryPoint = new TestEntryPoint(); ObjectGraph.createWith(new TestingLoader(), new TestModule()).inject(entryPoint); assertThat(entryPoint.q.f).isNotNull(); }
@Test public void childModuleWithChildModule() { @Module( injects = TestEntryPoint.class, includes = ModuleWithChildModule.class ) class TestModule { } ObjectGraph objectGraph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); TestEntryPoint entryPoint = new TestEntryPoint(); objectGraph.inject(entryPoint); assertThat(entryPoint.s).isEqualTo("injected"); }
@Test public void privateFieldsFail() { class Test { @Inject private Object nope; } @Module(injects = Test.class) class TestModule { @Provides Object provideObject() { return null; } } try { ObjectGraph.createWith(new TestingLoader(), new TestModule()).inject(new Test()); fail(); } catch (IllegalStateException e) { assertThat(e.getMessage()).contains("Can't inject private field: "); } }
@Test public void childModuleWithBinding() { @Module( injects = TestEntryPoint.class, includes = ModuleWithBinding.class ) class TestModule { } ObjectGraph objectGraph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); TestEntryPoint entryPoint = new TestEntryPoint(); objectGraph.inject(entryPoint); assertThat(entryPoint.s).isEqualTo("injected"); }
@Test public void runtimeProvidesMethodsExceptionsAreNotWrapped() { class TestEntryPoint { @Inject String string; } @Module(injects = TestEntryPoint.class) class TestModule { @Provides String provideString() { throw new ClassCastException("foo"); } } try { ObjectGraph.createWith(new TestingLoader(), new TestModule()).inject(new TestEntryPoint()); fail(); } catch (ClassCastException e) { assertThat(e.getMessage()).isEqualTo("foo"); } }
@Test public void childModuleWithManualConstruction() { @Module( injects = TestEntryPoint.class, includes = ModuleWithConstructor.class ) class TestModule { } ObjectGraph objectGraph = ObjectGraph.createWith(new TestingLoader(), new ModuleWithConstructor("a"), new TestModule()); TestEntryPoint entryPoint = new TestEntryPoint(); objectGraph.inject(entryPoint); assertThat(entryPoint.s).isEqualTo("a"); }
@Test public void injectMembers() { class TestEntryPoint { @Inject MembersInjector<Injectable> membersInjector; } @Module(injects = TestEntryPoint.class) class StringModule { @Provides String provideString() { return "injected"; } } TestEntryPoint entryPoint = new TestEntryPoint(); ObjectGraph.createWith(new TestingLoader(), new StringModule()).inject(entryPoint); Injectable injectable = new Injectable(); entryPoint.membersInjector.injectMembers(injectable); assertThat(injectable.injected).isEqualTo("injected"); }
@Test public void membersInjectorOfNonStaticInnerIsOkay() { class TestEntryPoint { @Inject MembersInjector<NonStaticInner> membersInjector; } @Module(injects = TestEntryPoint.class) class TestModule { @Provides String provideString() { return "injected"; } } TestEntryPoint entryPoint = new TestEntryPoint(); ObjectGraph.createWith(new TestingLoader(), new TestModule()).inject(entryPoint); NonStaticInner nonStaticInner = new NonStaticInner(); entryPoint.membersInjector.injectMembers(nonStaticInner); assertThat(nonStaticInner.injected).isEqualTo("injected"); }