From 00b3d796dfad3993d85347f02652729c8821d166 Mon Sep 17 00:00:00 2001 From: Austin Richardson Date: Thu, 7 Dec 2023 14:50:16 +0100 Subject: [PATCH 1/4] StaticImport: add `com.fasterxml.jackson.annotation.JsonCreator.Mode` --- .../errorprone/bugpatterns/StaticImport.java | 1 + .../bugpatterns/StaticImportTest.java | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/StaticImport.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/StaticImport.java index 3adb431a9c..7bba20c7b9 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/StaticImport.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/StaticImport.java @@ -57,6 +57,7 @@ public final class StaticImport extends BugChecker implements MemberSelectTreeMa @VisibleForTesting static final ImmutableSet STATIC_IMPORT_CANDIDATE_TYPES = ImmutableSet.of( + "com.fasterxml.jackson.annotation.JsonCreator.Mode", "com.google.common.base.Preconditions", "com.google.common.base.Predicates", "com.google.common.base.Verify", diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/StaticImportTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/StaticImportTest.java index bcfb097d39..29165a5712 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/StaticImportTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/StaticImportTest.java @@ -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;", @@ -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) {", + " return new A();", + " }", + "", " void toImmutableMultiset() {}", "}") .doTest(); @@ -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;", @@ -186,9 +194,16 @@ void replacement() { "", " @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)", " final class Test {}", + "", + " @JsonCreator(mode = JsonCreator.Mode.DELEGATING)", + " private static A create(int a) {", + " return new A();", + " }", + "", "}") .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;", @@ -208,6 +223,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;", @@ -266,6 +282,12 @@ void replacement() { "", " @SpringBootTest(webEnvironment = RANDOM_PORT)", " final class Test {}", + "", + " @JsonCreator(mode = DELEGATING)", + " private static A create(int a) {", + " return new A();", + " }", + "", "}") .doTest(TestMode.TEXT_MATCH); } From 37588e583bdef0ebf741bb3699e552958f82e768 Mon Sep 17 00:00:00 2001 From: Austin Richardson Date: Thu, 7 Dec 2023 15:18:10 +0100 Subject: [PATCH 2/4] Update test code to reflect Google code style --- .../tech/picnic/errorprone/bugpatterns/StaticImportTest.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/StaticImportTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/StaticImportTest.java index 29165a5712..9de62040c3 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/StaticImportTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/StaticImportTest.java @@ -199,7 +199,6 @@ void replacement() { " private static A create(int a) {", " return new A();", " }", - "", "}") .addOutputLines( "A.java", @@ -287,7 +286,6 @@ void replacement() { " private static A create(int a) {", " return new A();", " }", - "", "}") .doTest(TestMode.TEXT_MATCH); } From a762ac1472775756e44a2c3f1a5d3a604286fcd3 Mon Sep 17 00:00:00 2001 From: Austin Richardson Date: Fri, 8 Dec 2023 10:30:02 +0100 Subject: [PATCH 3/4] StaticImport add `com.fasterxml.jackson.annotation.{JsonFormat.Shape,JsonInclude.Include,JsonProperty.Access}` --- .../java/tech/picnic/errorprone/bugpatterns/StaticImport.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/StaticImport.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/StaticImport.java index 7bba20c7b9..8eb703f76d 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/StaticImport.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/StaticImport.java @@ -58,6 +58,9 @@ public final class StaticImport extends BugChecker implements MemberSelectTreeMa static final ImmutableSet 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", From 03a43a380632a8a5777709c92f59e0e0f019b687 Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Sat, 16 Dec 2023 15:56:28 +0100 Subject: [PATCH 4/4] Suggestions --- .../bugpatterns/StaticImportTest.java | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/StaticImportTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/StaticImportTest.java index 9de62040c3..4dde83f6e1 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/StaticImportTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/StaticImportTest.java @@ -107,15 +107,15 @@ void identification() { " }", "", " // BUG: Diagnostic contains:", - " @UseImportPolicy(ImportPolicy.IMPORT_TOP_LEVEL)", - " void refasterAfterTemplate() {}", - "", - " // BUG: Diagnostic contains:", " @JsonCreator(mode = JsonCreator.Mode.DELEGATING)", - " private static A create(int a) {", + " private static A jsonCreator(int a) {", " return new A();", " }", "", + " // BUG: Diagnostic contains:", + " @UseImportPolicy(ImportPolicy.IMPORT_TOP_LEVEL)", + " void refasterAfterTemplate() {}", + "", " void toImmutableMultiset() {}", "}") .doTest(); @@ -185,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,", @@ -194,11 +199,6 @@ void replacement() { "", " @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)", " final class Test {}", - "", - " @JsonCreator(mode = JsonCreator.Mode.DELEGATING)", - " private static A create(int a) {", - " return new A();", - " }", "}") .addOutputLines( "A.java", @@ -276,16 +276,16 @@ 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 {}", "", " @SpringBootTest(webEnvironment = RANDOM_PORT)", " final class Test {}", - "", - " @JsonCreator(mode = DELEGATING)", - " private static A create(int a) {", - " return new A();", - " }", "}") .doTest(TestMode.TEXT_MATCH); }