-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor
IsLambdaExpressionOrMethodReference
with a delegate
- Loading branch information
1 parent
f6cf53e
commit acddd00
Showing
2 changed files
with
103 additions
and
6 deletions.
There are no files selected for viewing
22 changes: 16 additions & 6 deletions
22
...in/java/tech/picnic/errorprone/refaster/matchers/IsLambdaExpressionOrMethodReference.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 | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,39 @@ | ||
package tech.picnic.errorprone.refaster.matchers; | ||
|
||
import static com.google.errorprone.matchers.Matchers.anyOf; | ||
import static com.sun.source.tree.MemberReferenceTree.ReferenceMode.INVOKE; | ||
|
||
import com.google.errorprone.VisitorState; | ||
import com.google.errorprone.matchers.Matcher; | ||
import com.sun.source.tree.ExpressionTree; | ||
import com.sun.source.tree.LambdaExpressionTree; | ||
import com.sun.source.tree.MemberReferenceTree; | ||
import com.sun.source.tree.Tree; | ||
|
||
/** A matcher of lambda expressions or method references. */ | ||
public final class IsLambdaExpressionOrMethodReference implements Matcher<ExpressionTree> { | ||
private static final long serialVersionUID = 1L; | ||
|
||
private static final Matcher<ExpressionTree> DELEGATE = | ||
anyOf(isLambdaExpression(), isMethodReference()); | ||
|
||
/** Instantiates a new {@link IsLambdaExpressionOrMethodReference} instance. */ | ||
public IsLambdaExpressionOrMethodReference() {} | ||
|
||
@Override | ||
public boolean matches(ExpressionTree tree, VisitorState state) { | ||
return isLambdaExpression(tree) || isMethodReference(tree); | ||
return DELEGATE.matches(tree, state); | ||
} | ||
|
||
private static boolean isLambdaExpression(ExpressionTree tree) { | ||
return tree instanceof LambdaExpressionTree; | ||
/** Returns a matcher that matches lambda expressions. */ | ||
public static <T extends Tree> Matcher<T> isLambdaExpression() { | ||
return (tree, state) -> tree instanceof LambdaExpressionTree; | ||
} | ||
|
||
private static boolean isMethodReference(ExpressionTree tree) { | ||
return tree instanceof MemberReferenceTree | ||
&& ((MemberReferenceTree) tree).getMode() == MemberReferenceTree.ReferenceMode.INVOKE; | ||
/** Returns a matcher that matches method references. */ | ||
public static <T extends Tree> Matcher<T> isMethodReference() { | ||
return (tree, state) -> | ||
tree instanceof MemberReferenceTree | ||
&& ((MemberReferenceTree) tree).getMode().equals(INVOKE); | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
...ava/tech/picnic/errorprone/refaster/matchers/IsLambdaExpressionOrMethodReferenceTest.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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package tech.picnic.errorprone.refaster.matchers; | ||
|
||
import static com.google.errorprone.BugPattern.SeverityLevel.ERROR; | ||
|
||
import com.google.errorprone.BugPattern; | ||
import com.google.errorprone.CompilationTestHelper; | ||
import com.google.errorprone.bugpatterns.BugChecker; | ||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.Test; | ||
|
||
final class IsLambdaExpressionOrMethodReferenceTest { | ||
|
||
@Disabled | ||
@Test | ||
void matches() { | ||
CompilationTestHelper.newInstance(MatcherTestChecker.class, getClass()) | ||
.addSourceLines( | ||
"A.java", | ||
"import java.util.function.Function;", | ||
"import java.util.stream.Stream;", | ||
"", | ||
"class A {", | ||
" Integer negative1() {", | ||
" Function<String, Integer> parseIntFunction = (String s) -> Integer.parseInt(s);", | ||
" return Stream.of(\"1\").map(parseIntFunction).reduce(0, Integer::sum);", | ||
" }", | ||
"", | ||
" Integer negative2() {", | ||
" Function<String, Integer> stringLengthMethodReference = String::length;", | ||
" return Stream.of(\"1\").map(stringLengthMethodReference).reduce(0, Integer::sum);", | ||
" }", | ||
"", | ||
" Double negative3() {", | ||
" Function<String, Double> parseDoubleFunction = new Function<String, Double>() {", | ||
" @Override", | ||
" public Double apply(String s) {", | ||
" return Double.parseDouble(s);", | ||
" }", | ||
" };", | ||
" return Stream.of(\"1\").map(parseDoubleFunction).reduce(0.0, Double::sum);", | ||
" }", | ||
"", | ||
" Long negative4() {", | ||
" class ParseLongFunction implements Function<String, Long> {", | ||
" @Override", | ||
" public Long apply(String s) {", | ||
" return Long.parseLong(s);", | ||
" }", | ||
" }", | ||
" return Stream.of(\"1\").map(new ParseLongFunction()).reduce(0L, Long::sum);", | ||
" }", | ||
"", | ||
" Integer positive1() {", | ||
" // BUG: Diagnostic contains:", | ||
" return Stream.of(1).map(i -> i * 2).reduce(0, Integer::sum);", | ||
" }", | ||
"", | ||
" Long positive2() {", | ||
" // BUG: Diagnostic contains:", | ||
" return Stream.of(1)", | ||
" .map(", | ||
" i -> {", | ||
" return i * 2L;", | ||
" })", | ||
" .reduce(0L, Long::sum);", | ||
" }", | ||
"", | ||
" Double positive3() {", | ||
" // BUG: Diagnostic contains:", | ||
" return Stream.of(\"1\").map(Double::parseDouble).reduce(0.0, Double::sum);", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
/** A {@link BugChecker} that simply delegates to {@link IsLambdaExpressionOrMethodReference}. */ | ||
@BugPattern( | ||
summary = "Flags expressions matched by `IsLambdaExpressionOrMethodReference`", | ||
severity = ERROR) | ||
public static final class MatcherTestChecker extends AbstractMatcherTestChecker { | ||
private static final long serialVersionUID = 1L; | ||
|
||
public MatcherTestChecker() { | ||
super(new IsLambdaExpressionOrMethodReference()); | ||
} | ||
} | ||
} |