0

I'm trying dagger and my current use case is represented by the code below. I tought Dagger would inject the objects recursively ut that doesn't seem to happen.

I'm getting null on top.inner.

public static class Bottom {

}

/*
public static class Inner {
  @Inject Bottom bottom;
}
*/

public static interface Inner {
    public Bottom bottom();

    public static class Implementation() {
        @Inject Bottom bottom;
        @Override public Bottom bottom() {
             return bottom;
        }
    }
}

public static class Top {
  @Inject Provider<Inner> inner;
}

@Module(injects = {DaggerTest.class, /* Top.class, Inner.class, */ Bottom.class})
public class AppModule {
  @Provides public Bottom providesBottom() {
    return new Bottom();
  }
}

@Inject Top top;

@Test
public void testProviderInjection() {
  ObjectGraph graph = ObjectGraph.create(new AppModule());
  graph.inject(this);

  Assert.assertNotNull("top", top);
  Assert.assertNotNull("top.inner", top.inner);
  Assert.assertNotNull("top.inner.get()", top.inner.get());
  /* Assert.assertNotNull("top.inner.get().bottom", top.inner.get().bottom); */
  Assert.assertNotNull("top.inner.get().bottom", top.inner.get().bottom());
}

I have no ideia if this is the desired behavior of Dagger but if it is can you suggest me possible way to do this whitout resorting to injection on every object.

EDIT I changed a little the use case. I forget a little detail :(.

Thanks

Filipe Pinheiro
  • 1,082
  • 8
  • 32

1 Answers1

2

Each type can be created by @Provides or have @Inject annotations, but it can't have both. Your solution in the code above is to delete the @Provides methods for everything but Bottom.

Jesse Wilson
  • 39,078
  • 8
  • 121
  • 128
  • Thank you for the response Jesse, but I forget a little detail, Inner is an interface. How can I define a @Provides for Inner? – Filipe Pinheiro Sep 05 '13 at 08:53
  • you define a @Provides method that returns Inner as part of its signature, and internally is implemented to create whichever implementation you choose for that interface to be returned. – David Santiago Turiño Oct 19 '13 at 21:12