SONARJAVA-6328 - Implement S8688: Time-based .now() methods should specify a ZoneId or a Clock#5606
Conversation
SummaryThis PR implements rule S8688, which detects calls to Why it matters: Relying on the system's default timezone can cause inconsistent behavior across different deployment environments (developer machine, CI, production). Explicitly passing a Implementation: The check extends What reviewers should knowStart with:
Key design decisions:
What to verify:
|
| } | ||
|
|
||
| @Override | ||
| public void visitNode(Tree tree) { |
There was a problem hiding this comment.
If you extend the AbstractMethodDetection class, you can override getMethodInvocationMatchers method which will trigger the rule on invocations of specific method matchers. Then, instead of calling visitNode and explicitly having to check that the node is a MethodInvocationTree, you can override the onMethodInvocationFound method and directly raise the issue without the need for the condition.
| if (NOW_MATCHER.matches(mit) && mit.methodSelect() instanceof MemberSelectExpressionTree mset) { | ||
| reportIssue(mset.identifier(), mit, "Explicitly specify the time zone by passing a ZoneId or a Clock to the .now() method."); | ||
| } |
There was a problem hiding this comment.
The call to NOW_MATCHER.matches(mit) is unnecessary here: the method will only be called on method invocations that match the method matchers returned by getMethodInvocationMatchers(). You also know that the method will only ever be invoked on method calls where methodSelect will be a MemberSelectExpressionTree, so you could do something like:
| if (NOW_MATCHER.matches(mit) && mit.methodSelect() instanceof MemberSelectExpressionTree mset) { | |
| reportIssue(mset.identifier(), mit, "Explicitly specify the time zone by passing a ZoneId or a Clock to the .now() method."); | |
| } | |
| reportIssue(((MemberSelectExpressionTree) mit.methodSelect()).identifier(), mit, "Explicitly specify the time zone by passing a ZoneId or a Clock to the .now() method."); |
|




No description provided.