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

[AssistedInject] Generated factory compilation error #2309

Closed
stavfx opened this issue Jan 23, 2021 · 1 comment
Closed

[AssistedInject] Generated factory compilation error #2309

stavfx opened this issue Jan 23, 2021 · 1 comment
Assignees

Comments

@stavfx
Copy link

stavfx commented Jan 23, 2021

Given this setup

@Component
interface MyComponent {
    fun factory(): MyFactory
}

@AssistedFactory
interface MyFactory {
    fun create(foo: Boolean): MyPresenter
}

class MyPresenter @AssistedInject constructor(
    @Assisted private val foo: Boolean
)

The following Factory implementation is created

@SuppressWarnings({
    "unchecked",
    "rawtypes"
})
public final class MyFactory_Impl implements MyFactory {
  private final MyPresenter_Factory delegateFactory;

  MyFactory_Impl(MyPresenter_Factory delegateFactory) {
    this.delegateFactory = delegateFactory;
  }

  @Override
  public MyPresenter create(boolean foo) {
    return delegateFactory.get(foo);
  }

  public static Provider<MyFactory> create(MyPresenter_Factory delegateFactory) {
    return InstanceFactory.create(new MyFactory_Impl(delegateFactory));
  }
}

Which yields this error in the static create method:
error: incompatible types: Factory<MyFactory_Impl> cannot be converted to Provider<MyFactory>.

This seems like a type inference issue because this can be fixed by explicitly setting the generic type of InstanceFactory.create like so:
return InstanceFactory.<MyFactory>create(new MyFactory_Impl(delegateFactory));
(or casting MyFactory_Impl, or a few other tricks).

I feel like this should work, but am I also doing something wrong?

@bcorso
Copy link

bcorso commented Jan 26, 2021

Hi @stavfx,

Thanks for reporting this. I think you're right that this is likely a type inference issue that works with java 8 but has issues with java 7. I'll look into fixing this for the next release.

@bcorso bcorso self-assigned this Jan 26, 2021
copybara-service bot pushed a commit that referenced this issue Jan 28, 2021
To test this, I've updated our gradle tests to allow inputing a Java version as a cmd-line argument, and I've added a regression test for this issue.

Fixes #2309

RELNOTES=Fixes #2309: Fixes type inference issues with generated assisted factory in Java 7.
PiperOrigin-RevId: 354200817
copybara-service bot pushed a commit that referenced this issue Jan 29, 2021
To test this, I've updated our gradle use sourceCompatibility = VERSION_1_7.

Fixes #2309

RELNOTES=Fixes #2309: Fixes type inference issues with generated assisted factory in Java 7.
PiperOrigin-RevId: 354200817
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

Successfully merging a pull request may close this issue.

2 participants