question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dagger should error out when components have injection methods for interfaces

See original GitHub issue
interface 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:open
  • Created 7 years ago
  • Reactions:2
  • Comments:11

github_iconTop GitHub Comments

5reactions
gk5885commented, Nov 2, 2016

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 @Injected 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:

  • +1 to a warning
  • +1 to static analysis that you can opt into for your project if you want
1reaction
pyricaucommented, Nov 2, 2016

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?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found