dagger-reflect icon indicating copy to clipboard operation
dagger-reflect copied to clipboard

Cumbersome injection with generics

Open emartynov opened this issue 5 years ago • 3 comments

As an exercise purpose, I'm trying to add Dagger Reflect to Plaid project. After all necessary changes to make project compile I got runtime crash.

The issue is that HomeActivity should be injected by HomeComponent. The issue is that HomeComponent doesn't declare the inject method directly but rather implements generic injector interface BaseActivityComponent. Which implements BaseComponent that has generic method

fun inject(target: T)

During debug, I see that reflection is looking to parameter type of the method declaration which is resoled to Object rather than taking the type of the passed parameter HomeActivity.

I wonder what are you thoughts about the issue and the approach taken in the plaid app?

emartynov avatar Mar 18 '20 11:03 emartynov

The support of generics is lacking. There's also #135 tracking its support on modules.

JakeWharton avatar Mar 24 '20 14:03 JakeWharton

I've spent a fair bit of time trying to get dagger reflect to work in a large project and although most things CAN be worked around with reasonable effort, the lack of support for generics is what blocks me - eliminating them in this particular code base is sadly not a tempting approach at the moment. I will report my other findings in other tickets.

catellie avatar Nov 05 '20 09:11 catellie

In case anyone else digs into this, I found it useful to catch the unwanted generics at this point in the code in the debugger and then I could peek around in the scope to try to figure out what I really wanted:

https://github.com/JakeWharton/dagger-reflect/blob/6ab4adefb67c315e49e303c973bab557b213afa7/reflect/src/main/java/dagger/reflect/UnlinkedBindsBinding.java#L25

(If your code base is small enough you may even be able to fiddle the dependency key at this stage, but...)

catellie avatar Nov 05 '20 10:11 catellie