Add ability to inject abstract @AssistedInject
See original GitHub issueI have a use case while I have to inject the abstract class, not the implementation itself, but the implementation requires an assisted parameter to be injected, like this:
class ClassAImpl @AssistedInject constructor(
@Assisted private val params: ClassAParams
) : ClassA {
....
@AssistedFactory
interface ClassAFactory {
fun create(params: ClassAParams): ClassA
}
}
I got this error:
An assisted factory's abstract method must return a type with an @AssistedInject-annotated constructor.
While it should work fine
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Can I use some kind of assisted Inject with Dagger?
I wrote about it in this blogpost, but I'll add a full example here to make things easier. First thing you need are...
Read more >Assisted Injection - Dagger
This can be done by adding a name via the @Assisted("name") annotation. These must be put on both the factory method and the...
Read more >Assisted Injection for Dagger - Google Groups
I have a class that gets some dependencies from the object graph, and other dependencies from a caller at runtime. It's like a...
Read more >extensions/assistedinject/src/com/google/inject ... - Google Git
All the data necessary to perform an assisted inject. */ ... .add("implementation type", implementationType) ... "%s is abstract, not a concrete class.
Read more >Connecting The Dots: Dagger • ViewModel • Saved State
Dagger 2.31 added @AssistedInject functionality and therefore the setup is simplified ... Now, you want to fully use power of ViewModels, ...
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
Just because the
ClassAFactory
is nested intoClassAImpl
does not mean Dagger will extrapolate anything from that: it sees an@AssistedFactory
interface whose abstract methods returns aClassA
, whose constructor is (apparently) not annotated with@AssistedInject
. Dagger has no means to know that you actually would like that factory to return aClassAImpl
instance rather than any otherClassA
subclass (orClassA
itself if not abstract)Because you don’t inject either
ClassA
orClassAImpl
instances, but onlyClassAFactory
, you can declare the method to returnClassAImpl
, and then it should Just Work™.If you don’t want to expose the detail that this will be a
ClassAImpl
instance, and really want injection points to only know aboutClassA
, then make an interface forClassA
, and then have the@AssistedFactory
extend it and override the method to tighten its return type, and then finally add a binding for those interfaces so Dagger will actually inject aClassAImplFactory
whenever aClassAFactory
is needed:(this should work in theory, but I haven’t tested it, so maybe there are limitations in Dagger that would prevent it from working)
Yep, got it to work …that was the prob. Thanks