square / java-code-styles

IntelliJ IDEA code style settings for Square's Java and Android projects.
2.96k stars 771 forks source link

exclude wrong Inject types #79

Closed thomasmahoney closed 4 years ago

thomasmahoney commented 4 years ago

According to the Square Java style guide, we should prefer javax.inject.Inject for dependency injection: https://wiki.sqprod.co/pages/viewpage.action?pageId=14811784

This PR excludes the other two Inject options from IJ suggestions, enabling developers to auto-import the desired type after annotating a field or constructor with @Inject. The google one, at least, still seems to work fine, but there's no reason to prefer it, so we should stick with the style guide's recommendation and also eliminate the manual touchpoint of choosing an import from a list.

Another nice benefit of using the javax type is that IJ understands it, so it prevents the annoying "managed bean must have a constructor with no parameters, or a constructor annotated @Inject" error.

The com.google.inject.Inject type is already in widespread use (5k results searching for the import on go/grep, vs. 10k for the javax type), but this change shouldn't create a nuisance for anyone working in one of those files - it will just affect import suggestions.

Also - junit.framework.Assert was in here twice; I removed the duplicate.

swankjesse commented 4 years ago

The only reason to use the Google one is for optional = true. Modern Guice has alternatives that work better!