Skip to content

Commit

Permalink
Require static importing of some com.fasterxml.jackson.annotation e…
Browse files Browse the repository at this point in the history
…nums (#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`
  • Loading branch information
arichapn authored Dec 18, 2023
1 parent 7529b99 commit 7c2078b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ 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",
"com.fasterxml.jackson.annotation.JsonFormat.Shape",
"com.fasterxml.jackson.annotation.JsonInclude.Include",
"com.fasterxml.jackson.annotation.JsonProperty.Access",
"com.google.common.base.Preconditions",
"com.google.common.base.Predicates",
"com.google.common.base.Verify",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ void identification() {
"import static java.util.function.Predicate.not;",
"import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;",
"",
"import com.fasterxml.jackson.annotation.JsonCreator;",
"import com.google.common.base.Predicates;",
"import com.google.common.collect.ImmutableMap;",
"import com.google.common.collect.ImmutableMultiset;",
Expand Down Expand Up @@ -106,6 +107,12 @@ void identification() {
" }",
"",
" // BUG: Diagnostic contains:",
" @JsonCreator(mode = JsonCreator.Mode.DELEGATING)",
" private static A jsonCreator(int a) {",
" return new A();",
" }",
"",
" // BUG: Diagnostic contains:",
" @UseImportPolicy(ImportPolicy.IMPORT_TOP_LEVEL)",
" void refasterAfterTemplate() {}",
"",
Expand All @@ -121,6 +128,7 @@ void replacement() {
"A.java",
"import static java.util.function.Predicate.not;",
"",
"import com.fasterxml.jackson.annotation.JsonCreator;",
"import com.google.common.base.Predicates;",
"import com.google.common.collect.ImmutableMap;",
"import com.google.common.collect.ImmutableSet;",
Expand Down Expand Up @@ -177,6 +185,11 @@ void replacement() {
" @DateTimeFormat(iso = ISO.DATE_TIME) String dateTime,",
" @DateTimeFormat(iso = ISO.TIME) String time) {}",
"",
" @JsonCreator(mode = JsonCreator.Mode.DELEGATING)",
" private static A jsonCreator(int a) {",
" return new A();",
" }",
"",
" @BugPattern(",
" summary = \"\",",
" linkType = BugPattern.LinkType.NONE,",
Expand All @@ -189,6 +202,7 @@ void replacement() {
"}")
.addOutputLines(
"A.java",
"import static com.fasterxml.jackson.annotation.JsonCreator.Mode.DELEGATING;",
"import static com.google.common.collect.ImmutableMap.toImmutableMap;",
"import static com.google.common.collect.ImmutableSet.toImmutableSet;",
"import static com.google.errorprone.BugPattern.LinkType.NONE;",
Expand All @@ -208,6 +222,7 @@ void replacement() {
"import static org.springframework.http.MediaType.APPLICATION_XHTML_XML;",
"import static org.springframework.http.MediaType.TEXT_HTML;",
"",
"import com.fasterxml.jackson.annotation.JsonCreator;",
"import com.google.common.base.Predicates;",
"import com.google.common.collect.ImmutableMap;",
"import com.google.common.collect.ImmutableSet;",
Expand Down Expand Up @@ -261,6 +276,11 @@ void replacement() {
" @DateTimeFormat(iso = DATE_TIME) String dateTime,",
" @DateTimeFormat(iso = TIME) String time) {}",
"",
" @JsonCreator(mode = DELEGATING)",
" private static A jsonCreator(int a) {",
" return new A();",
" }",
"",
" @BugPattern(summary = \"\", linkType = NONE, severity = SUGGESTION, tags = SIMPLIFICATION)",
" static final class TestBugPattern {}",
"",
Expand Down

0 comments on commit 7c2078b

Please sign in to comment.