square / dagger

A fast dependency injector for Android and Java.
https://square.github.io/dagger/
Apache License 2.0
7.31k stars 3.06k forks source link

Non-static providers inside abstract modules cause runtime crashes #549

Closed AttwellBrian closed 7 years ago

AttwellBrian commented 7 years ago

If you forget to add static to an abstract module your app will crash at runtime. This should be prevented at compile time.

Example: Suppose you have a nice abstract module such as the following example:

@dagger.Module
public abstract static class Module {
    @NonNull
    @OnlineScope
    @Provides
    static A a() {
        return new A();
    }
}

@OnlineScope
@dagger.Component(modules = Module.class, dependencies = ParentComponent.class)
interface Component {
    @dagger.Component.Builder
    interface Builder {
        @BindsInstance
        Builder interactor(OnlineInteractor interactor);
        @BindsInstance
        Builder view(Observable<Optional<OnlineView>> view);
        Builder parentComponent(ParentComponent component);
        Component build();
    }
}

Next, lets add a new provider to the Module.

@dagger.Module
public abstract static class Module {
    @NonNull
    @OnlineScope
    @Provides
    static A a() {
        return new A();
       }
   }

    @NonNull
    @OnlineScope
    @Provides
    B b() {
        return new B();
    }
}

Oops! We forgot to add static to the second provider. This example now crashes at runtime.

JakeWharton commented 7 years ago

You want google/dagger not square/dagger.

AttwellBrian commented 7 years ago

Ha. This is embarrassing.

Moved to https://github.com/google/dagger/issues/621