Skip to content

Commit

Permalink
Introduce additional matchers to TimeZoneUsage rule
Browse files Browse the repository at this point in the history
Add matchers for `OffsetDateTime`, `OffsetTime` and `ZonedDateTime` classes.
  • Loading branch information
chamil-prabodha committed Oct 24, 2022
1 parent 7fe61c2 commit c6f49e3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {",
Expand Down Expand Up @@ -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 {",
Expand Down

0 comments on commit c6f49e3

Please sign in to comment.