Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.12: Possible compilation error with @Binds #896

Closed
Martin-Luft opened this issue Oct 11, 2017 · 7 comments
Closed

2.12: Possible compilation error with @Binds #896

Martin-Luft opened this issue Oct 11, 2017 · 7 comments

Comments

@Martin-Luft
Copy link

Martin-Luft commented Oct 11, 2017

Updating from 2.11 to 2.12 breaks my whole build.

this.addressPresenterProvider =
        DoubleCheck.provider(
            AddressPresenter_Factory.create((Provider) addressViewProvider, eventBusProvider));

The method provider(Provider) in the type DoubleCheck is not applicable for the arguments (Factory)

This happens when using @Binds, but not with an identical @Provides method

This does not work anymore:

@Module
public abstract class ClientMobileModule {

  @Binds
  @Singleton
  public abstract AddressViewInterface addressView(AddressView addressView);
@Singleton
public class AddressView implements AddressViewInterface {

  @Inject
  AddressView() {
  }

This works (using @provides instead of @BINDS):

@Module
public final class ClientMobileModule {

  @Provides
  @Singleton
  public static AddressViewInterface addressView(AddressView addressView) {
    return addressView;
  }
@Singleton
public class AddressView implements AddressViewInterface {

  @Inject
  AddressView() {
  }

This also works (removing @singleton from the view implementation):

@Module
public abstract class ClientMobileModule {

  @Binds
  @Singleton
  public abstract AddressViewInterface addressView(AddressView addressView);
public class AddressView implements AddressViewInterface {

  @Inject
  AddressView() {
  }
@Martin-Luft Martin-Luft changed the title Dagger 2.12 -> compile error Dagger 2.12 -> compile error when injecting interfaces without provide methods Oct 11, 2017
@ronshapiro
Copy link

This unfortunately isn't enough information on it's own to be able to understand what the issue is. What's the type of the addressPresenterProvider field?

@ronshapiro ronshapiro changed the title Dagger 2.12 -> compile error when injecting interfaces without provide methods 2.12: Possible compilation error with @Binds Oct 11, 2017
@Martin-Luft
Copy link
Author

@ronshapiro I created a little project for you:

https://github.com/Martin-Wegner/dagger2_issue896

Removing the @Singleton annotation from the AddressView also fixed the error...

Building with Maven works, only Eclipse produces the error.

@ronshapiro
Copy link

I don't have any way to test this with eclipse, but my guess is that there's a bug with Eclipse's Types implementation which is causing Dagger to generate the wrong code.

When you build that sample project in eclipse, do you get the same generated code as you do with Maven, but the compilation fails?

@Martin-Luft
Copy link
Author

Martin-Luft commented Oct 12, 2017

@ronshapiro I will test this tomorrow. Can you tell me please, if the @Singleton annotation is sufficient if I use it only at the @Binds/@Provides methods and not at the implementing views?

The generated code is the same when using @Singleton in both classes (@Binds class and implementing view class) and using it only in @Binds class.

@Martin-Luft
Copy link
Author

Martin-Luft commented Oct 16, 2017

@ronshapiro the generated source is exactly the same (Maven and Eclipse). I tested it with Eclipse for Java developers Neon 3.

@Martin-Luft
Copy link
Author

Generated bad code for Eclipse:

package com.google.dagger.issue896.dagger;

import com.google.dagger.issue896.presenter.RootPresenter;
import com.google.dagger.issue896.presenter.RootPresenter_Factory;
import com.google.dagger.issue896.view.RootView;
import com.google.dagger.issue896.view.RootView_Factory;
import dagger.internal.DoubleCheck;
import javax.annotation.Generated;
import javax.inject.Provider;

@Generated(
  value = "dagger.internal.codegen.ComponentProcessor",
  comments = "https://google.github.io/dagger"
)
public final class DaggerMyComponent implements MyComponent {
  private Provider<RootView> rootViewProvider;

  private Provider<RootPresenter> rootPresenterProvider;

  private DaggerMyComponent(Builder builder) {
    initialize(builder);
  }

  public static Builder builder() {
    return new Builder();
  }

  public static MyComponent create() {
    return new Builder().build();
  }

  @SuppressWarnings("unchecked")
  private void initialize(final Builder builder) {
    this.rootViewProvider = DoubleCheck.provider(RootView_Factory.create());
    this.rootPresenterProvider =
        DoubleCheck.provider(RootPresenter_Factory.create((Provider) rootViewProvider));
  }

  @Override
  public RootPresenter rootPresenter() {
    return rootPresenterProvider.get();
  }

  public static final class Builder {
    private Builder() {}

    public MyComponent build() {
      return new DaggerMyComponent(this);
    }
  }
}

Generated good code for Eclipse:

package com.google.dagger.issue896.dagger;

import com.google.dagger.issue896.presenter.RootPresenter;
import com.google.dagger.issue896.presenter.RootPresenter_Factory;
import com.google.dagger.issue896.view.RootView_Factory;
import dagger.internal.DoubleCheck;
import javax.annotation.Generated;
import javax.inject.Provider;

@Generated(
  value = "dagger.internal.codegen.ComponentProcessor",
  comments = "https://google.github.io/dagger"
)
public final class DaggerMyComponent implements MyComponent {
  private Provider<RootPresenter.RootViewInterface> rootViewProvider;

  private Provider<RootPresenter> rootPresenterProvider;

  private DaggerMyComponent(Builder builder) {
    initialize(builder);
  }

  public static Builder builder() {
    return new Builder();
  }

  public static MyComponent create() {
    return new Builder().build();
  }

  @SuppressWarnings("unchecked")
  private void initialize(final Builder builder) {
    this.rootViewProvider = DoubleCheck.provider((Provider) RootView_Factory.create());
    this.rootPresenterProvider =
        DoubleCheck.provider(RootPresenter_Factory.create(rootViewProvider));
  }

  @Override
  public RootPresenter rootPresenter() {
    return rootPresenterProvider.get();
  }

  public static final class Builder {
    private Builder() {}

    public MyComponent build() {
      return new DaggerMyComponent(this);
    }
  }
}

@ronshapiro
Copy link

If the generated code is the same, you should file a bug with Eclipse. That code should work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants