-
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
PRP-10968 Introduce TimeZoneUsageCheck
#9
Conversation
7ff4063
to
d8a721b
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 a commit. Suggested commit message:
PRP-10968 Flag discouraged time zone-dependent APIs (#9)
Will merge if you're okay with my changes.
LocalDate.class.getName(), | ||
LocalDateTime.class.getName(), | ||
LocalTime.class.getName()) | ||
.namedAnyOf("now")); |
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.
.namedAnyOf("now")); | |
.named("now")); |
@AutoService(BugChecker.class) | ||
@BugPattern( | ||
name = "TimeZoneUsageCheck", | ||
summary = "Avoid illegal operations on assorted time related objects", |
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.
summary = "Avoid illegal operations on assorted time related objects", | |
summary = "Derive the current time from an existing `Clock` Spring bean, and don't rely on a `Clock`'s time zone", |
/** A {@link BugChecker} which flags illegal time-zone related operations. */ | ||
@AutoService(BugChecker.class) | ||
@BugPattern( | ||
name = "TimeZoneUsageCheck", |
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.
name = "TimeZoneUsageCheck", | |
name = "TimeZoneUsage", |
"clock.instant();", | ||
"clock.millis();", | ||
"Clock.offset(clock, Duration.ZERO);", | ||
"Clock.tick(clock, Duration.ZERO);", |
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.
Indentation.
} | ||
|
||
@Test | ||
public void testIdentifyCases() { |
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.
We generally just combine this into a single test class.
if (!IS_BANNED_TIME_METHOD.matches(tree, state)) { | ||
return Description.NO_MATCH; | ||
} | ||
return buildDescription(tree).build(); |
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.
@rickie why the removed newline? Seems nicer with it :)
Alternatively, let's do:
if (!IS_BANNED_TIME_METHOD.matches(tree, state)) { | |
return Description.NO_MATCH; | |
} | |
return buildDescription(tree).build(); | |
return IS_BANNED_TIME_METHOD.matches(tree, state) | |
? return buildDescription(tree).build() | |
: Description.NO_MATCH; |
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.
The line felt more like a direct else
that needed to be close to the if, so I removed the line.
I applied the alternative, way better :) @Stephan202
b2d642c
to
d540c39
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. @anna8712 okay to merge? :)
^ @anna8712 question about whether to merge this one stands :) |
Introduce a new Error Prone plugin which flags bad practices with objects related to
time
, which might be critical when we'll handle different time zones in our systems.This check will flag the bad usages, as a build
Warning
.