Skip to content

Commit

Permalink
Move usages of Matchers.adaptMatcherType to .toType, which is slightl…
Browse files Browse the repository at this point in the history
…y simpler.

Note that this isn't an entirely equivalent replacement as toType always returns
Matcher<Tree> whereas adaptMatcherType used to allow setting the returned
matcher's tree type. However, this setting didn't really have any effect: any
kind of tree is a legal input to the returned matcher. Since any place that
takes Matcher<Foo> should really take Matcher<? super Foo> (and can easily be
made to), toType's model tends to be easier to read at call sites.

RELNOTES: None.
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=90160315
  • Loading branch information
aragos authored and cushon committed Apr 6, 2015
1 parent 5de74f1 commit 55bd36e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 37 deletions.
Expand Up @@ -19,14 +19,15 @@
import static com.google.errorprone.BugPattern.Category.JDK;
import static com.google.errorprone.BugPattern.MaturityLevel.MATURE;
import static com.google.errorprone.BugPattern.SeverityLevel.WARNING;
import static com.google.errorprone.matchers.Matchers.adaptMatcherType;
import static com.google.errorprone.matchers.Matchers.allOf;
import static com.google.errorprone.matchers.Matchers.anyOf;
import static com.google.errorprone.matchers.Matchers.assignment;
import static com.google.errorprone.matchers.Matchers.binaryTree;
import static com.google.errorprone.matchers.Matchers.inSynchronized;
import static com.google.errorprone.matchers.Matchers.kindIs;
import static com.google.errorprone.matchers.Matchers.not;
import static com.google.errorprone.matchers.Matchers.sameVariable;
import static com.google.errorprone.matchers.Matchers.toType;

import com.google.errorprone.BugPattern;
import com.google.errorprone.VisitorState;
Expand Down Expand Up @@ -81,19 +82,6 @@ public boolean matches(UnaryTree tree, VisitorState state) {
};
}

/**
* Extracts the expression from an AssignmentTree and applies a matcher to it.
*/
private static Matcher<AssignmentTree> expressionFromAssignmentTree(
final Matcher <ExpressionTree> exprMatcher) {
return new Matcher<AssignmentTree>() {
@Override
public boolean matches(AssignmentTree tree, VisitorState state) {
return exprMatcher.matches(tree.getExpression(), state);
}
};
}

/**
* Extracts the variable from a CompoundAssignmentTree and applies a matcher to it.
*/
Expand Down Expand Up @@ -175,14 +163,17 @@ private static Matcher<AssignmentTree> assignmentIncrementDecrementMatcher(
variableFromAssignmentTree(
Matchers.<ExpressionTree>hasModifier(Modifier.VOLATILE)),
not(inSynchronized()),
expressionFromAssignmentTree(adaptMatcherType(ExpressionTree.class, BinaryTree.class,
Matchers.<BinaryTree>allOf(
Matchers.<BinaryTree>anyOf(
kindIs(Kind.PLUS),
kindIs(Kind.MINUS)),
binaryTree(
sameVariable(variable),
Matchers.<ExpressionTree>anything())))));
assignment(
Matchers.<ExpressionTree>anything(),
toType(
BinaryTree.class,
Matchers.<BinaryTree>allOf(
Matchers.<BinaryTree>anyOf(
kindIs(Kind.PLUS),
kindIs(Kind.MINUS)),
binaryTree(
sameVariable(variable),
Matchers.<ExpressionTree>anything())))));
}

@Override
Expand Down
23 changes: 8 additions & 15 deletions core/src/main/java/com/google/errorprone/matchers/Matchers.java
Expand Up @@ -1035,22 +1035,15 @@ static Matcher<Tree> isSymbol(java.lang.Class<? extends Symbol> symbolClass) {
}

/**
* Safely adapts a matcher on a subtype of Tree into a matcher on Tree. Fails if the tree
* node passed in is not an instance of the subtype, or the if the matcher does not match.
*
* @param returnTypeParam Type parameter of the Matcher that will be returned
* @param matcherTypeParam Type parameter of the Matcher passed in
* @param matcher The matcher to apply to the tree node
* Converts the given matcher to one that can be applied to any tree but is only executed when
* run on a tree of {@code type} and returns {@code false} for all other tree types.
*/
public static <S extends Tree, T extends S> Matcher<S> adaptMatcherType(
final Class<S> returnTypeParam, final Class<T> matcherTypeParam, final Matcher<T> matcher) {
return new Matcher<S>() {
public static <T extends Tree> Matcher<Tree> toType(
final Class<T> type, final Matcher<? super T> matcher) {
return new Matcher<Tree>() {
@Override
public boolean matches(S tree, VisitorState state) {
if (matcherTypeParam.isInstance(tree)) {
return matcher.matches(matcherTypeParam.cast(tree), state);
}
return false;
public boolean matches(Tree tree, VisitorState state) {
return type.isInstance(tree) && matcher.matches(type.cast(tree), state);
}
};
}
Expand Down Expand Up @@ -1131,7 +1124,7 @@ public boolean matches(EnhancedForLoopTree t, VisitorState state) {
* @param expressionMatcher The matcher to apply to the expression.
*/
public static Matcher<AssignmentTree> assignment(final Matcher<ExpressionTree> variableMatcher,
final Matcher<ExpressionTree> expressionMatcher) {
final Matcher<? super ExpressionTree> expressionMatcher) {
return new Matcher<AssignmentTree>() {
@Override
public boolean matches(AssignmentTree t, VisitorState state) {
Expand Down

0 comments on commit 55bd36e

Please sign in to comment.