AssistedInject for a type with generic parameter produces code that doesn't compile
See original GitHub issueDagger 2.31 The following class generates code that doesn’t compile:
package com.example
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
class AssistedInjectRepro<T> @AssistedInject constructor(
@Assisted obj: T,
) {
@AssistedFactory
interface Factory {
fun <T> create(
obj: T,
): AssistedInjectRepro<T>
}
}
generated code:
// Generated by Dagger (https://dagger.dev).
package com.example;
import dagger.internal.InstanceFactory;
import javax.inject.Provider;
@SuppressWarnings({
"unchecked",
"rawtypes"
})
public class AssistedInjectRepro_Factory_Impl implements AssistedInjectRepro.Factory {
private final AssistedInjectRepro_Factory<T> delegateFactory;
AssistedInjectRepro_Factory_Impl(AssistedInjectRepro_Factory<T> delegateFactory) {
this.delegateFactory = delegateFactory;
}
@Override
public <T> AssistedInjectRepro<T> create(T obj) {
return delegateFactory.get(obj);
}
public static <T> Provider<AssistedInjectRepro.Factory> create(
AssistedInjectRepro_Factory<T> delegateFactory) {
return InstanceFactory.create(new AssistedInjectRepro_Factory_Impl(delegateFactory));
}
}
which fails with
> Task :app:kaptDebugKotlin FAILED
...generated/source/kapt/debug/com/example/AssistedInjectRepro_Factory_Impl.java:12: error: cannot find symbol
private final AssistedInjectRepro_Factory<T> delegateFactory;
^
symbol: class T
location: class AssistedInjectRepro_Factory_Impl/.../build/generated/source/kapt/debug/com/example/AssistedInjectRepro_Factory_Impl.java:14: error: cannot find symbol
AssistedInjectRepro_Factory_Impl(AssistedInjectRepro_Factory<T> delegateFactory) {
^
symbol: class T
location: class AssistedInjectRepro_Factory_Impl
FAILURE: Build failed with an exception.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:12
- Comments:12
Top Results From Across the Web
Can I use some kind of assisted Inject with Dagger?
Because AutoFactory generates code, you don't need to write an interface to represent the constructor: AutoFactory will write a brand new ...
Read more >How To Use Generics in Go | DigitalOcean
By declaring C any inside your type parameters, your code says, “create a generic type parameter named C that I can use in...
Read more >Assisted Injection - Dagger
Dagger is a fully static, compile-time dependency injection framework for both Java and Android. It is developed by the Java Core Libraries Team...
Read more >How to Set up Dagger With ViewModel & Saved State Module
Neither one of these is great because they makes your code more fragile ... The generic parameter T serves as a way to...
Read more >Connecting The Dots: Dagger • ViewModel • Saved State
I don't describe here, how to setup Dagger with ViewModel as there ... The generic parameter T serves as way to inform AssistedInject...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@lwasyl, thanks for the repro case!
I agree we should support this. I should be able to get a fix out soon.
@lwasyl, just a heads up that we’re still considering this but it’s unlikely it’ll be in the next release.
For now, I’m going to add some validation in our processor so that at least we’ll catch this and fail fast with a better error message rather than generating invalid java.