From d2e7ceafb19ef9d8c4e6b8933d55d2ec4dfb3a60 Mon Sep 17 00:00:00 2001 From: ghm Date: Thu, 29 Jan 2026 10:04:47 -0800 Subject: [PATCH] Inline WaitMatchers. This seemed a bit unnecessary, noticed in https://github.com/google/error-prone/commit/404b754a150413045eeba0ede07b3f20c6041457. PiperOrigin-RevId: 862785212 --- .../errorprone/matchers/WaitMatchers.java | 68 ------------------- .../errorprone/bugpatterns/WaitNotInLoop.java | 41 ++++++++++- 2 files changed, 39 insertions(+), 70 deletions(-) delete mode 100644 check_api/src/main/java/com/google/errorprone/matchers/WaitMatchers.java diff --git a/check_api/src/main/java/com/google/errorprone/matchers/WaitMatchers.java b/check_api/src/main/java/com/google/errorprone/matchers/WaitMatchers.java deleted file mode 100644 index 677bae949d5..00000000000 --- a/check_api/src/main/java/com/google/errorprone/matchers/WaitMatchers.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright 2015 The Error Prone Authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.errorprone.matchers; - -import static com.google.errorprone.matchers.Matchers.anyOf; -import static com.google.errorprone.matchers.Matchers.staticMethod; -import static com.google.errorprone.matchers.method.MethodMatchers.instanceMethod; - -import com.sun.source.tree.MethodInvocationTree; -import java.util.regex.Pattern; - -/** Matchers for method invocations related to Object.wait() and Condition.await(); */ -public final class WaitMatchers { - - private static final String OBJECT_FQN = "java.lang.Object"; - private static final String CONDITION_FQN = "java.util.concurrent.locks.Condition"; - - private static final Matcher UNINTERRUPTIBLES_AWAIT_CONDITION = - anyOf( - staticMethod() - .onClass("com.google.common.util.concurrent.Uninterruptibles") - .named("awaitUninterruptibly") - .withParameters( - "java.util.concurrent.locks.Condition", "long", "java.util.concurrent.TimeUnit"), - staticMethod() - .onClass("com.google.common.util.concurrent.Uninterruptibles") - .named("awaitUninterruptibly") - .withParameters("java.util.concurrent.locks.Condition", "java.time.Duration")); - - /** Matches any wait/await method. */ - public static final Matcher WAIT_METHOD = - anyOf( - instanceMethod().onExactClass(OBJECT_FQN).named("wait"), - instanceMethod() - .onDescendantOf(CONDITION_FQN) - .withNameMatching(Pattern.compile("await.*")), - UNINTERRUPTIBLES_AWAIT_CONDITION); - - /** Matches wait/await methods that have a timeout. */ - public static final Matcher WAIT_METHOD_WITH_TIMEOUT = - anyOf( - instanceMethod().onExactClass(OBJECT_FQN).named("wait").withParameters("long"), - instanceMethod().onExactClass(OBJECT_FQN).named("wait").withParameters("long", "int"), - instanceMethod() - .onDescendantOf(CONDITION_FQN) - .named("await") - .withParameters("long", "java.util.concurrent.TimeUnit"), - instanceMethod().onDescendantOf(CONDITION_FQN).named("awaitNanos"), - instanceMethod().onDescendantOf(CONDITION_FQN).named("awaitUntil"), - staticMethod().onClass("com.google.common.time.Durations").named("wait"), - UNINTERRUPTIBLES_AWAIT_CONDITION); - - private WaitMatchers() {} -} diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/WaitNotInLoop.java b/core/src/main/java/com/google/errorprone/bugpatterns/WaitNotInLoop.java index d067909d521..4997966b1db 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/WaitNotInLoop.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/WaitNotInLoop.java @@ -18,10 +18,11 @@ import static com.google.errorprone.BugPattern.SeverityLevel.WARNING; import static com.google.errorprone.matchers.Matchers.allOf; +import static com.google.errorprone.matchers.Matchers.anyOf; import static com.google.errorprone.matchers.Matchers.inLoop; +import static com.google.errorprone.matchers.Matchers.instanceMethod; import static com.google.errorprone.matchers.Matchers.not; -import static com.google.errorprone.matchers.WaitMatchers.WAIT_METHOD; -import static com.google.errorprone.matchers.WaitMatchers.WAIT_METHOD_WITH_TIMEOUT; +import static com.google.errorprone.matchers.Matchers.staticMethod; import static com.google.errorprone.util.ASTHelpers.findEnclosingNode; import static com.google.errorprone.util.ASTHelpers.getSymbol; @@ -35,6 +36,7 @@ import com.sun.source.tree.MethodInvocationTree; import com.sun.tools.javac.code.Symbol.MethodSymbol; import com.sun.tools.javac.tree.JCTree.JCIf; +import java.util.regex.Pattern; /** * @author eaftan@google.com (Eddie Aftandilian) @@ -47,6 +49,41 @@ severity = WARNING, tags = StandardTags.FRAGILE_CODE) public class WaitNotInLoop extends BugChecker implements MethodInvocationTreeMatcher { + private static final String OBJECT_FQN = "java.lang.Object"; + private static final String CONDITION_FQN = "java.util.concurrent.locks.Condition"; + + private static final Matcher UNINTERRUPTIBLES_AWAIT_CONDITION = + anyOf( + staticMethod() + .onClass("com.google.common.util.concurrent.Uninterruptibles") + .named("awaitUninterruptibly") + .withParameters( + "java.util.concurrent.locks.Condition", "long", "java.util.concurrent.TimeUnit"), + staticMethod() + .onClass("com.google.common.util.concurrent.Uninterruptibles") + .named("awaitUninterruptibly") + .withParameters("java.util.concurrent.locks.Condition", "java.time.Duration")); + + private static final Matcher WAIT_METHOD = + anyOf( + instanceMethod().onExactClass(OBJECT_FQN).named("wait"), + instanceMethod() + .onDescendantOf(CONDITION_FQN) + .withNameMatching(Pattern.compile("await.*")), + UNINTERRUPTIBLES_AWAIT_CONDITION); + + private static final Matcher WAIT_METHOD_WITH_TIMEOUT = + anyOf( + instanceMethod().onExactClass(OBJECT_FQN).named("wait").withParameters("long"), + instanceMethod().onExactClass(OBJECT_FQN).named("wait").withParameters("long", "int"), + instanceMethod() + .onDescendantOf(CONDITION_FQN) + .named("await") + .withParameters("long", "java.util.concurrent.TimeUnit"), + instanceMethod().onDescendantOf(CONDITION_FQN).named("awaitNanos"), + instanceMethod().onDescendantOf(CONDITION_FQN).named("awaitUntil"), + staticMethod().onClass("com.google.common.time.Durations").named("wait"), + UNINTERRUPTIBLES_AWAIT_CONDITION); private static final Matcher matcher = allOf(WAIT_METHOD, not(inLoop()));