-
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-11004 Introduce bug checker to flag Instant.now()
#10
Conversation
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.
Didn't check the tests yet. I wonder whether it'd be useful to extend UseTimeInScope: if there's a Clock
in scope, then Instant.now()
should become clock.instant()
.
public final class InstantNowCheck extends BugChecker implements MethodInvocationTreeMatcher { | ||
private static final long serialVersionUID = 1L; | ||
private static final Matcher<ExpressionTree> NOW_USAGES = | ||
anyOf(staticMethod().onClassAny(Instant.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.
anyOf(staticMethod().onClassAny(Instant.class.getName()).namedAnyOf("now")); | |
staticMethod().onClassAny(Instant.class.getName().namedAnyOf("now")); |
That said, there are a few other classes that derive time from an implicit time source that we should flag.
} | ||
|
||
SuggestedFix.Builder builder = SuggestedFix.builder(); | ||
builder.replace(tree, Util.treeToString(tree, state).replace(".now()", ".EPOCH")); |
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.
As mentioned during our last meeting, I don't think we should do this replacement: it's not correct. Instead developers should manually first the issue in a way that is most appropriate.
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.
In case we refactor this plugin to run in tests only, does it worth introducing more logic to that and not just replacing with InstantEpoch
? if the test will fail - then should be fixed manually.
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.
If we can reliably identify tests (something that's also necessary for #5 (comment)) then sure, Instant.EPOCH
(and LocalDate.EPOCH
, etc.) works for me.
e131122
to
bb952b0
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; did not attempt to fix the build. Given that we now also have #9, are you still interested in this PR? :)
} | ||
|
||
SuggestedFix.Builder builder = SuggestedFix.builder(); | ||
builder.replace(tree, Util.treeToString(tree, state).replace(".now()", ".EPOCH")); |
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.
If we can reliably identify tests (something that's also necessary for #5 (comment)) then sure, Instant.EPOCH
(and LocalDate.EPOCH
, etc.) works for me.
@AutoService(BugChecker.class) | ||
@BugPattern( | ||
name = "InstantNow", | ||
summary = "Avoid using {@link Instant#now()} when possible", |
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.
Javadoc doesn't work here :)
bb952b0
to
ae15923
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. Ping @anna8712 :)
@Stephan202 Thanks for your comments, I agree to close this PR, since https://github.com/PicnicSupermarket/error-prone-support/pull/9/files covers |
Sure; update the ticket :) |
No description provided.