Skip to content

Commit

Permalink
Refactor IsLambdaExpressionOrMethodReference with a delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
giovannizotta authored and Stephan202 committed Feb 19, 2023
1 parent f6cf53e commit acddd00
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 6 deletions.
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);
}
}
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());
}
}
}

0 comments on commit acddd00

Please sign in to comment.