Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Multiple improvements to WaitNotInLoop.
Changes: 1) Now detects only the case where the wait is not in a loop. No longer detects the case where it is in a loop, but the synchronized block is inside the loop. (I will move that to a separate check) 2) Much better explanation in the markdown file, including suggestions for how to fix common patterns. 3) Now matches Condition.await() in addition to Object.wait(). 4) Now produces a suggested fix for the simple case of changing the enclosing if to a while. RELNOTES: - Improvements to WaitNotInLoop. Detects a somewhat different set of problems, but explains them better, and fixes a subset. ------------- Created by MOE: http://code.google.com/p/moe-java MOE_MIGRATED_REVID=91902359
- Loading branch information
Showing
8 changed files
with
565 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
core/src/main/java/com/google/errorprone/matchers/WaitMatchers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,51 @@ | |||
/* | |||
* Copyright 2015 Google Inc. All Rights Reserved. | |||
* | |||
* 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.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 class WaitMatchers { | |||
|
|||
private static final String OBJECT_FQN = "java.lang.Object"; | |||
private static final String CONDITION_FQN = "java.util.concurrent.locks.Condition"; | |||
|
|||
/** | |||
* Matches any wait/await method. | |||
*/ | |||
public static final Matcher<MethodInvocationTree> waitMethod = anyOf( | |||
instanceMethod().onExactClass(OBJECT_FQN).named("wait"), | |||
instanceMethod().onDescendantOf(CONDITION_FQN).withNameMatching(Pattern.compile("await.*"))); | |||
|
|||
/** | |||
* Matches wait/await methods that have a timeout. | |||
*/ | |||
public static final Matcher<MethodInvocationTree> waitMethodWithTimeout = anyOf( | |||
instanceMethod().onExactClass(OBJECT_FQN).withSignature("wait(long)"), | |||
instanceMethod().onExactClass(OBJECT_FQN).withSignature("wait(long,int)"), | |||
instanceMethod().onDescendantOf(CONDITION_FQN) | |||
.withSignature("await(long,java.util.concurrent.TimeUnit)"), | |||
instanceMethod().onDescendantOf(CONDITION_FQN).named("awaitNanos"), | |||
instanceMethod().onDescendantOf(CONDITION_FQN).named("awaitUntil")); | |||
} |
59 changes: 59 additions & 0 deletions
59
core/src/test/java/com/google/errorprone/MatcherChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,59 @@ | |||
/* | |||
* Copyright 2015 Google Inc. All Rights Reserved. | |||
* | |||
* 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; | |||
|
|||
import static com.google.errorprone.BugPattern.Category.ONE_OFF; | |||
import static com.google.errorprone.BugPattern.MaturityLevel.MATURE; | |||
import static com.google.errorprone.BugPattern.SeverityLevel.ERROR; | |||
|
|||
import com.google.errorprone.bugpatterns.BugChecker; | |||
import com.google.errorprone.bugpatterns.BugChecker.ExpressionStatementTreeMatcher; | |||
import com.google.errorprone.matchers.Description; | |||
import com.google.errorprone.matchers.Matcher; | |||
|
|||
import com.sun.source.tree.ExpressionStatementTree; | |||
import com.sun.source.tree.Tree; | |||
|
|||
/** | |||
* A {@link BugChecker} that flags {@link ExpressionStatementTree}s that satisfy both of the | |||
* following: | |||
* <ul> | |||
* <li>The text of the tree is the same as the given {@code expressionStatement}, and | |||
* <li>The given {@code matcher} matches the tree | |||
* </ul> | |||
* | |||
* <p>Useful for testing {@link Matcher}s. | |||
*/ | |||
@BugPattern(name = "MatcherChecker", | |||
summary = "Checker that flags the given expression statement if the given matcher matches", | |||
category = ONE_OFF, maturity = MATURE, severity = ERROR) | |||
public class MatcherChecker extends BugChecker implements ExpressionStatementTreeMatcher { | |||
private final String expressionStatement; | |||
private final Matcher<Tree> matcher; | |||
|
|||
public MatcherChecker(String expressionStatement, Matcher<Tree> matcher) { | |||
this.expressionStatement = expressionStatement; | |||
this.matcher = matcher; | |||
} | |||
|
|||
@Override | |||
public Description matchExpressionStatement(ExpressionStatementTree tree, VisitorState state) { | |||
return (tree.toString().equals(expressionStatement) && matcher.matches(tree, state)) | |||
? describeMatch(tree) | |||
: Description.NO_MATCH; | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.