anvil icon indicating copy to clipboard operation
anvil copied to clipboard

[dagger-factory] Member injection for super types fails when passing a generic type and using constructor injection

Open gmarques33 opened this issue 2 years ago • 2 comments

Hi, I'm facing an issue similar to https://github.com/square/anvil/issues/486 but instead of using field injection in the InjectClass, we use constructor injection. Following is the sample to reproduce the issue:

package com.sample.anvil

import javax.inject.Inject

abstract class Base<S> {
    @Inject
    lateinit var strings: List<S>
}
package com.sample.anvil

import javax.inject.Inject

class InjectClass @Inject constructor() : Base<String>() {}

The MemberInjector generated for InjectClass throws compilation error with the message Unresolved reference: S.

Anvil generates:

public class InjectClass_Factory(
  private val strings: List<@JvmSuppressWildcards VM>,
) : Factory<InjectClass> {
...

and Dagger generates:

public final class InjectClass_Factory implements Factory<InjectClass> {
  private final List<String> strings;

  public InjectClass_Factory(List<String> strings) {
    this.strings = strings;
  }
...

gmarques33 avatar Dec 23 '22 10:12 gmarques33

seeing the same for classes that extend an abstract class, the members injector generated also uses generics:

abstract class SomeFragment<P> {
      @set:Inject
    var presenterLazy: Lazy<P>? = null
}

extend:

class SomeFragmentSubclass : SomeFragment<SomePresenter>

generates :

public class SomeFragmentSubclass_MembersInjector(
  private val presenterLazy: Provider<@JvmSuppressWildcards P>,
) : MembersInjector<SomeFragmentSubclass> {

agrosner avatar Mar 29 '23 16:03 agrosner

What does the generated dagger code for SomeFragmentSubclass_MembersInjector or InjectClass_MembersInjector look like?

ZacSweers avatar Apr 03 '23 12:04 ZacSweers