Dagger should error out when components have injection methods for interfaces
See original GitHub issueinterface MyInterface {
}
@Component
interface MyComponent {
void inject(MyInterface foo);
}
Dagger builds just fine and doesn’t complain. The inject method is implemented as a no-op.
We made that mistake and this wasn’t detected until we tried to build our release. Seems like it’d be nice it Dagger told us we made a mistake 😃 .
Issue Analytics
- State:
- Created 7 years ago
- Reactions:2
- Comments:11
Top Results From Across the Web
Depending on an interface - Dagger
If you try to compile, Dagger reports an error. Since Command is an interface and can't have an @Inject constructor, we need to...
Read more >Error when two components has same inject method signature ...
Think of dagger as an object graph—which it actually is. You probably should not have 2 different components being able to inject the...
Read more >Dagger error in Java | Edureka Community
The class has no @Inject annotated constructor! And there is no other module in the component, so there is nothing Dagger could do....
Read more >From Dagger components to manual dependency injection
Dagger is a great framework that has changed the way we have written Android apps in the latest years. Using Dagger we don't...
Read more >Dependency Injection with Dagger 2 - CodePath Cliffnotes
The @Singleton annotation also signals to the Dagger compiler that the instance should be created only once in the application. In the following...
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
So, that’s actually a question about any type without members to inject and whether or not that should be an error.
First, we try to avoid spooky action at a distance. Say you have A extends B extends C. If C has an
@Inject
ed field and that’s the only field that is injected, it would be potentially surprising and hard to manage if a change to a field on C suddenly breaks components that request members injection on A. The distance between the code that changes and the error is too great. Totally fine for a warning though, IMO.Second, intentional no-ops can be nice. If you have a whole class of types (Say,
Activity
subclasses) that get a members injection method apiece, it might be more manageable to have each get injected even if some of them don’t have injectable members.Third, I think it can be annoying to be building out a new structure and get errors like the one you’re proposing when you’re just not done assembling all of the parts. It can be a little frustrating to have to inject a placeholder something just to make sure that the component builds while you’re trying to get everything set up.
Finally, I think it’s in the spirit of the JLS to only error on “dead code” when it is unreachable. I.e. why we have errors for private members that can’t be injected, but why we don’t fail builds for private methods that aren’t called.
So, those are all of my reasons for not wanting to make it a mandatory error, but:
Sorry, I don’t think I follow. What’s the issue with having an error for any no-op injection method on components, how would that be a pain in development?