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

K2 factory generation: don't unwrap generic types in IR #1120

Open
RBusarow opened this issue Feb 20, 2025 · 0 comments
Open

K2 factory generation: don't unwrap generic types in IR #1120

RBusarow opened this issue Feb 20, 2025 · 0 comments
Labels
bug Something isn't working dagger factory Happens when generateDaggerFactories is set to true

Comments

@RBusarow
Copy link
Collaborator

Given this test:

compile2(
  """
  package com.squareup.test
  
  import dagger.Lazy
  import javax.inject.Inject
  import javax.inject.Provider
  
  class InjectClass @Inject constructor(
    val string: String, 
    val stringProvider: Provider<String>,
    val stringListProvider: Provider<List<String>>,
    val lazyString: Lazy<String>
  ) {
    override fun equals(other: Any?): Boolean {
      return toString() == other.toString()
    }
    override fun toString(): String {
     return string + stringProvider.get() + 
         stringListProvider.get()[0] + lazyString.get()
    }
  }
  """,
) {
  val factoryClass = classLoader.injectClass_Factory
  val staticMethods = factoryClass.declaredMethods.filter { it.isStatic }

  val factoryInstance = staticMethods.single { it.name == "create" }
    .invoke(
      null,
      Provider { "a" },
      Provider { "b" },
      Provider { listOf("c") },
      Provider { "d" },
    )
  assertThat(factoryInstance::class.java).isEqualTo(factoryClass)

  val newInstance = staticMethods.single { it.name == "newInstance" }
    .invoke(null, "a", Provider { "b" }, Provider { listOf("c") }, Lazy { "d" })
  val getInstance = (factoryInstance as Factory<*>).get()
}

The call to factoryInstant.get() will fail:

java.lang.ClassCastException: class java.lang.String cannot be cast to class javax.inject.Provider (java.lang.String is in module java.base of loader 'bootstrap'; javax.inject.Provider is in unnamed module of loader 'app')
@RBusarow RBusarow added bug Something isn't working dagger factory Happens when generateDaggerFactories is set to true labels Feb 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working dagger factory Happens when generateDaggerFactories is set to true
Projects
None yet
Development

No branches or pull requests

1 participant