-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Require static importing of some com.fasterxml.jackson.annotation
enums
#910
Conversation
Looks good. No mutations were possible for these changes. |
@@ -57,6 +57,7 @@ public final class StaticImport extends BugChecker implements MemberSelectTreeMa | |||
@VisibleForTesting | |||
static final ImmutableSet<String> STATIC_IMPORT_CANDIDATE_TYPES = | |||
ImmutableSet.of( | |||
"com.fasterxml.jackson.annotation.JsonCreator.Mode", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's more cases like:
@JsonFormat.Shape
.@JsonTypeInfo.As
.@JsonTypeInfo.Id
.@JsonSubTypes.Type
.- (probably more)
Do you think we could find a way to handle these in bulk @rickie? :)
Or would you recommend listing (and testing) each one individually.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No we do not need to test all of them. The check is already sufficiently tested.
We can simply add the members to the list. I'm not sure if we should just add all of them. We only add the ones where statically importing doesn't make the code "less readable/understandable", as in, we do not lose any context on what is happening.
- The
JsonFormat.Shape
seems like a no-brainer that we should add. - For
.Id
I"m not sure:@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS,
I'd say,JsonTypeInfo
can be omitted but theId
part is maybe still "fine" to have? Same holds for:
@JsonTypeInfo(
use = NAME,
include = JsonTypeInfo.As.EXISTING_PROPERTY,
We can drop JsonTypeInfo
but having .As
is maybe better? This is a bit up for discussion I feel. WDYT?
- I'd argue that
@Type
is not that clear where it comes from. On the other hand, all usages are only nested in the@JsonSubTypes
annotation (I think) and in that case it would make sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with your proposal 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took the annotations from the docs, filtered down to the inner classes, and then did quick scans of some uses in public code on GitHub.
If we ignore JsonCreator.Mode
as it's already addressed, and filter down to only the cases where I primarily saw uses where the qualification provides superfluous context, these are what we're left with:
I'd vote for including those in this PR with the same approach as what's currently here for JsonCreator.Mode
.
I also looked at the other types Enric mentioned, and agree with Rick:
- For
JsonTypeInfo.As
andJsonTypeInfo.Id
, I think we should keep around theAs
andId
bits because of code like this - For
JsonSubTypes.Type
, I think we should keep around theType
bit because of code like this
So to summarise, my vote is to add the following changes here:
JsonFormat.Shape.X
-->X
JsonInclude.Include.X
-->X
JsonProperty.Access.X
-->X
JsonTypeInfo.As.X
-->As.X
JsonTypeInfo.Id.X
-->Id.X
JsonSubTypes.Type.X
-->Type.X
What do you guys think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And as for testing, I'd say what we already have provides sufficient confidence for the cases where we're removing all qualification. But it's probably a good idea to add a test for one of the cases where we're leaving some qualification (e.g. JsonTypeInfo.As.X
--> As.X
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just saw Enric's emoji response. 😄
Curious to hear your thoughts too @rickie, if this all sounds good then I'll proceed. 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I completely agree with the proposal of which ones to statically import. W.r.t. the ones that are still qualified but not statically imported, I'm not sure if that would fit in this check TBH. I think that should go in the NonStaticImport
as there we qualify which types to qualify instead of statically import. Although I'm not sure if that also does this rewrite: JsonTypeInfo.Id.X
to Id.X
but it does make X
-> Id.X
. 😄
Nonetheless, I'd say it's worth checking out.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotcha, in that case I'll just add the static import options to this PR.
For the imports which remain (at least partially) qualified, I can keep it on my TODO list to check those out. 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added, let me know what you think. 👍
Looks good. No mutations were possible for these changes. |
com.fasterxml.jackson.annotation.JsonCreator.Mode
com.fasterxml.jackson.annotation
values
Looks good. No mutations were possible for these changes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for filing a PR @arichapn 🚀!
Suggested commit message:
|
320f9b6
to
8a913b7
Compare
Looks good. No mutations were possible for these changes. |
8a913b7
to
f8cad7c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rebased and added some tweaks.
Suggested commit message:
Require static importing of some `com.fasterxml.jackson.annotation` enums (#910)
Types to be imported statically:
- `com.fasterxml.jackson.annotation.JsonCreator.Mode`
- `com.fasterxml.jackson.annotation.JsonFormat.Shape`
- `com.fasterxml.jackson.annotation.JsonInclude.Include`
- `com.fasterxml.jackson.annotation.JsonProperty.Access`
@@ -109,6 +110,12 @@ void identification() { | |||
" @UseImportPolicy(ImportPolicy.IMPORT_TOP_LEVEL)", | |||
" void refasterAfterTemplate() {}", | |||
"", | |||
" // BUG: Diagnostic contains:", | |||
" @JsonCreator(mode = JsonCreator.Mode.DELEGATING)", | |||
" private static A create(int a) {", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For consistency with the method above:
" private static A create(int a) {", | |
" private static A jsonCreator(int a) {", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(To better match the declaration order in StaticImport
I'll move this up a bit.)
Looks good. No mutations were possible for these changes. |
f8cad7c
to
03a43a3
Compare
Looks good. No mutations were possible for these changes. |
Quality Gate passedKudos, no new issues were introduced! 0 New issues |
com.fasterxml.jackson.annotation
valuescom.fasterxml.jackson.annotation
enums
Added are:
JsonCreator.Mode
JsonFormat.Shape
JsonInclude.Include
JsonProperty.Access