Skip to content

Commit ce1a422

Browse files
klueverError Prone Team
authored andcommitted
Add Durations.wait() to WaitMatchers.waitMethodWithTimeout (and make them CONSTANT_CASE).
PiperOrigin-RevId: 805324565
1 parent da03fea commit ce1a422

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

check_api/src/main/java/com/google/errorprone/matchers/WaitMatchers.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.errorprone.matchers;
1818

1919
import static com.google.errorprone.matchers.Matchers.anyOf;
20+
import static com.google.errorprone.matchers.Matchers.staticMethod;
2021
import static com.google.errorprone.matchers.method.MethodMatchers.instanceMethod;
2122

2223
import com.sun.source.tree.MethodInvocationTree;
@@ -29,15 +30,15 @@ public final class WaitMatchers {
2930
private static final String CONDITION_FQN = "java.util.concurrent.locks.Condition";
3031

3132
/** Matches any wait/await method. */
32-
public static final Matcher<MethodInvocationTree> waitMethod =
33+
public static final Matcher<MethodInvocationTree> WAIT_METHOD =
3334
anyOf(
3435
instanceMethod().onExactClass(OBJECT_FQN).named("wait"),
3536
instanceMethod()
3637
.onDescendantOf(CONDITION_FQN)
3738
.withNameMatching(Pattern.compile("await.*")));
3839

3940
/** Matches wait/await methods that have a timeout. */
40-
public static final Matcher<MethodInvocationTree> waitMethodWithTimeout =
41+
public static final Matcher<MethodInvocationTree> WAIT_METHOD_WITH_TIMEOUT =
4142
anyOf(
4243
instanceMethod().onExactClass(OBJECT_FQN).named("wait").withParameters("long"),
4344
instanceMethod().onExactClass(OBJECT_FQN).named("wait").withParameters("long", "int"),
@@ -46,7 +47,8 @@ public final class WaitMatchers {
4647
.named("await")
4748
.withParameters("long", "java.util.concurrent.TimeUnit"),
4849
instanceMethod().onDescendantOf(CONDITION_FQN).named("awaitNanos"),
49-
instanceMethod().onDescendantOf(CONDITION_FQN).named("awaitUntil"));
50+
instanceMethod().onDescendantOf(CONDITION_FQN).named("awaitUntil"),
51+
staticMethod().onClass("com.google.common.time.Durations").named("wait"));
5052

5153
private WaitMatchers() {}
5254
}

core/src/main/java/com/google/errorprone/bugpatterns/WaitNotInLoop.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
import static com.google.errorprone.matchers.Matchers.allOf;
2121
import static com.google.errorprone.matchers.Matchers.inLoop;
2222
import static com.google.errorprone.matchers.Matchers.not;
23-
import static com.google.errorprone.matchers.WaitMatchers.waitMethod;
24-
import static com.google.errorprone.matchers.WaitMatchers.waitMethodWithTimeout;
23+
import static com.google.errorprone.matchers.WaitMatchers.WAIT_METHOD;
24+
import static com.google.errorprone.matchers.WaitMatchers.WAIT_METHOD_WITH_TIMEOUT;
25+
import static com.google.errorprone.util.ASTHelpers.findEnclosingNode;
26+
import static com.google.errorprone.util.ASTHelpers.getSymbol;
2527

2628
import com.google.errorprone.BugPattern;
2729
import com.google.errorprone.BugPattern.StandardTags;
@@ -30,7 +32,6 @@
3032
import com.google.errorprone.fixes.SuggestedFix;
3133
import com.google.errorprone.matchers.Description;
3234
import com.google.errorprone.matchers.Matcher;
33-
import com.google.errorprone.util.ASTHelpers;
3435
import com.sun.source.tree.MethodInvocationTree;
3536
import com.sun.tools.javac.code.Symbol.MethodSymbol;
3637
import com.sun.tools.javac.tree.JCTree.JCIf;
@@ -47,7 +48,7 @@
4748
tags = StandardTags.FRAGILE_CODE)
4849
public class WaitNotInLoop extends BugChecker implements MethodInvocationTreeMatcher {
4950

50-
private static final Matcher<MethodInvocationTree> matcher = allOf(waitMethod, not(inLoop()));
51+
private static final Matcher<MethodInvocationTree> matcher = allOf(WAIT_METHOD, not(inLoop()));
5152

5253
@Override
5354
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
@@ -56,17 +57,17 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState
5657
}
5758

5859
Description.Builder description = buildDescription(tree);
59-
MethodSymbol sym = ASTHelpers.getSymbol(tree);
60+
MethodSymbol sym = getSymbol(tree);
6061
description.setMessage(
6162
String.format("Because of spurious wakeups, %s must always be called in a loop", sym));
6263

6364
// If this looks like the "Wait until a condition becomes true" case from the wiki content,
6465
// rewrite the enclosing if to a while. Other fixes are too complicated to construct
6566
// mechanically, so we provide detailed instructions in the wiki content.
66-
if (!waitMethodWithTimeout.matches(tree, state)) {
67-
JCIf enclosingIf = ASTHelpers.findEnclosingNode(state.getPath().getParentPath(), JCIf.class);
67+
if (!WAIT_METHOD_WITH_TIMEOUT.matches(tree, state)) {
68+
JCIf enclosingIf = findEnclosingNode(state.getPath().getParentPath(), JCIf.class);
6869
if (enclosingIf != null && enclosingIf.getElseStatement() == null) {
69-
CharSequence ifSource = state.getSourceForNode(enclosingIf);
70+
String ifSource = state.getSourceForNode(enclosingIf);
7071
if (ifSource == null) {
7172
// Source isn't available, so we can't construct a fix
7273
return description.build();

0 commit comments

Comments
 (0)