Skip to content
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

Closed
wants to merge 1 commit into from

Conversation

anna8712
Copy link
Contributor

No description provided.

@anna8712 anna8712 added the WIP Work in progress label Jan 18, 2021
Copy link
Member

@Stephan202 Stephan202 left a 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"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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"));
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Member

@Stephan202 Stephan202 left a 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"));
Copy link
Member

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",
Copy link
Member

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 :)

Copy link
Member

@Stephan202 Stephan202 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rebased. Ping @anna8712 :)

@anna8712
Copy link
Contributor Author

anna8712 commented Mar 1, 2021

@Stephan202 Thanks for your comments, I agree to close this PR, since https://github.com/PicnicSupermarket/error-prone-support/pull/9/files covers *.now cases.
Should I close this PR (and delete the branch)?

@Stephan202
Copy link
Member

Sure; update the ticket :)

@Stephan202 Stephan202 closed this Mar 1, 2021
@Stephan202 Stephan202 deleted the anna8712/PRP-11004 branch March 1, 2021 18:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
WIP Work in progress
Development

Successfully merging this pull request may close these issues.

2 participants