From 5b547fd4e5caf85f154472dc151d8baf1ccd751b Mon Sep 17 00:00:00 2001 From: Chamil Prabodha Date: Mon, 24 Oct 2022 13:47:51 +0200 Subject: [PATCH 1/2] Introduce additional matchers to `TimeZoneUsage` rule Add matchers for `OffsetDateTime`, `OffsetTime` and `ZonedDateTime` classes. --- .../errorprone/bugpatterns/TimeZoneUsage.java | 8 ++++++- .../bugpatterns/TimeZoneUsageTest.java | 24 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/TimeZoneUsage.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/TimeZoneUsage.java index 1bda1d207b..c4426261ff 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/TimeZoneUsage.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/TimeZoneUsage.java @@ -26,6 +26,9 @@ import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; +import java.time.OffsetDateTime; +import java.time.OffsetTime; +import java.time.ZonedDateTime; /** A {@link BugChecker} that flags illegal time-zone related operations. */ @AutoService(BugChecker.class) @@ -58,7 +61,10 @@ public final class TimeZoneUsage extends BugChecker implements MethodInvocationT .onClassAny( LocalDate.class.getName(), LocalDateTime.class.getName(), - LocalTime.class.getName()) + LocalTime.class.getName(), + OffsetDateTime.class.getName(), + OffsetTime.class.getName(), + ZonedDateTime.class.getName()) .named("now"), staticMethod().onClassAny(Instant.class.getName()).named("now").withNoParameters()); diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/TimeZoneUsageTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/TimeZoneUsageTest.java index b675225b92..1250fa5349 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/TimeZoneUsageTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/TimeZoneUsageTest.java @@ -26,7 +26,10 @@ void identification() { "import java.time.LocalDate;", "import java.time.LocalDateTime;", "import java.time.LocalTime;", + "import java.time.OffsetDateTime;", + "import java.time.OffsetTime;", "import java.time.ZoneId;", + "import java.time.ZonedDateTime;", "", "class A {", " void m() {", @@ -78,6 +81,27 @@ void identification() { " LocalTime.now(clock);", " // BUG: Diagnostic matches: X", " LocalTime.now(UTC);", + "", + " // BUG: Diagnostic matches: X", + " OffsetDateTime.now();", + " // BUG: Diagnostic matches: X", + " OffsetDateTime.now(clock);", + " // BUG: Diagnostic matches: X", + " OffsetDateTime.now(UTC);", + "", + " // BUG: Diagnostic matches: X", + " OffsetTime.now();", + " // BUG: Diagnostic matches: X", + " OffsetTime.now(clock);", + " // BUG: Diagnostic matches: X", + " OffsetTime.now(UTC);", + "", + " // BUG: Diagnostic matches: X", + " ZonedDateTime.now();", + " // BUG: Diagnostic matches: X", + " ZonedDateTime.now(clock);", + " // BUG: Diagnostic matches: X", + " ZonedDateTime.now(UTC);", " }", "", " abstract class ForwardingClock extends Clock {", From 827b70cf8aa31a4327ed5e7f4a8dc7e8f64e7058 Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Mon, 24 Oct 2022 15:57:22 +0200 Subject: [PATCH 2/2] Tweaks --- .../bugpatterns/TimeZoneUsageTest.java | 62 +++++++++---------- 1 file changed, 28 insertions(+), 34 deletions(-) diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/TimeZoneUsageTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/TimeZoneUsageTest.java index 1250fa5349..ad4e8a8b07 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/TimeZoneUsageTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/TimeZoneUsageTest.java @@ -1,17 +1,11 @@ package tech.picnic.errorprone.bugpatterns; -import static com.google.common.base.Predicates.containsPattern; - import com.google.errorprone.CompilationTestHelper; import org.junit.jupiter.api.Test; final class TimeZoneUsageTest { private final CompilationTestHelper compilationHelper = - CompilationTestHelper.newInstance(TimeZoneUsage.class, getClass()) - .expectErrorMessage( - "X", - containsPattern( - "Derive the current time from an existing `Clock` Spring bean, and don't rely on a `Clock`'s time zone")); + CompilationTestHelper.newInstance(TimeZoneUsage.class, getClass()); @Test void identification() { @@ -39,68 +33,68 @@ void identification() { " Clock.offset(clock, Duration.ZERO);", " Clock.tick(clock, Duration.ZERO);", "", - " // BUG: Diagnostic matches: X", + " // BUG: Diagnostic contains:", " Clock.systemUTC();", - " // BUG: Diagnostic matches: X", + " // BUG: Diagnostic contains:", " Clock.systemDefaultZone();", - " // BUG: Diagnostic matches: X", + " // BUG: Diagnostic contains:", " Clock.system(UTC);", - " // BUG: Diagnostic matches: X", + " // BUG: Diagnostic contains:", " Clock.tickMillis(UTC);", - " // BUG: Diagnostic matches: X", + " // BUG: Diagnostic contains:", " Clock.tickMinutes(UTC);", - " // BUG: Diagnostic matches: X", + " // BUG: Diagnostic contains:", " Clock.tickSeconds(UTC);", - " // BUG: Diagnostic matches: X", + " // BUG: Diagnostic contains:", " clock.getZone();", - " // BUG: Diagnostic matches: X", + " // BUG: Diagnostic contains:", " clock.withZone(UTC);", "", - " // BUG: Diagnostic matches: X", + " // BUG: Diagnostic contains:", " Instant.now();", " // This is equivalent to `clock.instant()`, which is fine.", " Instant.now(clock);", "", - " // BUG: Diagnostic matches: X", + " // BUG: Diagnostic contains:", " LocalDate.now();", - " // BUG: Diagnostic matches: X", + " // BUG: Diagnostic contains:", " LocalDate.now(clock);", - " // BUG: Diagnostic matches: X", + " // BUG: Diagnostic contains:", " LocalDate.now(UTC);", "", - " // BUG: Diagnostic matches: X", + " // BUG: Diagnostic contains:", " LocalDateTime.now();", - " // BUG: Diagnostic matches: X", + " // BUG: Diagnostic contains:", " LocalDateTime.now(clock);", - " // BUG: Diagnostic matches: X", + " // BUG: Diagnostic contains:", " LocalDateTime.now(UTC);", "", - " // BUG: Diagnostic matches: X", + " // BUG: Diagnostic contains:", " LocalTime.now();", - " // BUG: Diagnostic matches: X", + " // BUG: Diagnostic contains:", " LocalTime.now(clock);", - " // BUG: Diagnostic matches: X", + " // BUG: Diagnostic contains:", " LocalTime.now(UTC);", "", - " // BUG: Diagnostic matches: X", + " // BUG: Diagnostic contains:", " OffsetDateTime.now();", - " // BUG: Diagnostic matches: X", + " // BUG: Diagnostic contains:", " OffsetDateTime.now(clock);", - " // BUG: Diagnostic matches: X", + " // BUG: Diagnostic contains:", " OffsetDateTime.now(UTC);", "", - " // BUG: Diagnostic matches: X", + " // BUG: Diagnostic contains:", " OffsetTime.now();", - " // BUG: Diagnostic matches: X", + " // BUG: Diagnostic contains:", " OffsetTime.now(clock);", - " // BUG: Diagnostic matches: X", + " // BUG: Diagnostic contains:", " OffsetTime.now(UTC);", "", - " // BUG: Diagnostic matches: X", + " // BUG: Diagnostic contains:", " ZonedDateTime.now();", - " // BUG: Diagnostic matches: X", + " // BUG: Diagnostic contains:", " ZonedDateTime.now(clock);", - " // BUG: Diagnostic matches: X", + " // BUG: Diagnostic contains:", " ZonedDateTime.now(UTC);", " }", "",