Skip to content

Commit

Permalink
Annotate mapper with @matches(IsLambdaExpressionOrMethodReference.class)
Browse files Browse the repository at this point in the history
  • Loading branch information
giovannizotta authored and Stephan202 committed Feb 19, 2023
1 parent adc1d2b commit f6cf53e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.google.errorprone.refaster.Refaster;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import com.google.errorprone.refaster.annotation.Matches;
import com.google.errorprone.refaster.annotation.MayOptionallyUse;
import com.google.errorprone.refaster.annotation.Placeholder;
import com.google.errorprone.refaster.annotation.UseImportPolicy;
Expand All @@ -26,6 +27,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;
import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation;
import tech.picnic.errorprone.refaster.matchers.IsLambdaExpressionOrMethodReference;

/** Refaster rules related to expressions dealing with {@link Stream}s. */
@OnlineDocumentation
Expand Down Expand Up @@ -383,10 +385,11 @@ boolean after(Stream<T> stream) {
}
}

// XXX: Introduce a `@Matches(IsLambdaExpressionOrMethodReference.class)` guard for the `mapper`.
static final class StreamMapToIntSum<T> {
@BeforeTemplate
int before(Stream<T> stream, Function<? super T, Integer> mapper) {
int before(
Stream<T> stream,
@Matches(IsLambdaExpressionOrMethodReference.class) Function<? super T, Integer> mapper) {
return stream.map(mapper).reduce(0, Integer::sum);
}

Expand All @@ -396,10 +399,11 @@ int after(Stream<T> stream, ToIntFunction<T> mapper) {
}
}

// XXX: Introduce a `@Matches(IsLambdaExpressionOrMethodReference.class)` guard for the `mapper`.
static final class StreamMapToDoubleSum<T> {
@BeforeTemplate
double before(Stream<T> stream, Function<? super T, Double> mapper) {
double before(
Stream<T> stream,
@Matches(IsLambdaExpressionOrMethodReference.class) Function<? super T, Double> mapper) {
return stream.map(mapper).reduce(0.0, Double::sum);
}

Expand All @@ -409,10 +413,11 @@ static final class StreamMapToDoubleSum<T> {
}
}

// XXX: Introduce a `@Matches(IsLambdaExpressionOrMethodReference.class)` guard for the `mapper`.
static final class StreamMapToLongSum<T> {
@BeforeTemplate
long before(Stream<T> stream, Function<? super T, Long> mapper) {
long before(
Stream<T> stream,
@Matches(IsLambdaExpressionOrMethodReference.class) Function<? super T, Long> mapper) {
return stream.map(mapper).reduce(0L, Long::sum);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.google.common.collect.Streams;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;
import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase;
Expand Down Expand Up @@ -140,20 +141,26 @@ boolean testStreamAllMatch2() {
}

ImmutableSet<Integer> testStreamMapToIntSum() {
Function<String, Integer> parseIntFunction = (String s) -> Integer.parseInt(s);
return ImmutableSet.of(
Stream.of(1).map(i -> i * 2).reduce(0, Integer::sum),
Stream.of("1").map(Integer::parseInt).reduce(0, Integer::sum));
Stream.of("1").map(Integer::parseInt).reduce(0, Integer::sum),
Stream.of("1").map(parseIntFunction).reduce(0, Integer::sum));
}

ImmutableSet<Double> testStreamMapToDoubleSum() {
Function<String, Double> parseDoubleFunction = (String s) -> Double.parseDouble(s);
return ImmutableSet.of(
Stream.of(1).map(i -> i * 2.0).reduce(0.0, Double::sum),
Stream.of("1").map(Double::parseDouble).reduce(0.0, Double::sum));
Stream.of("1").map(Double::parseDouble).reduce(0.0, Double::sum),
Stream.of("1").map(parseDoubleFunction).reduce(0.0, Double::sum));
}

ImmutableSet<Long> testStreamMapToLongSum() {
Function<String, Long> parseLongFunction = (String s) -> Long.parseLong(s);
return ImmutableSet.of(
Stream.of(1).map(i -> i * 2L).reduce(0L, Long::sum),
Stream.of("1").map(Long::parseLong).reduce(0L, Long::sum));
Stream.of("1").map(Long::parseLong).reduce(0L, Long::sum),
Stream.of("1").map(parseLongFunction).reduce(0L, Long::sum));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;
import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase;
Expand Down Expand Up @@ -139,18 +140,26 @@ boolean testStreamAllMatch2() {
}

ImmutableSet<Integer> testStreamMapToIntSum() {
Function<String, Integer> parseIntFunction = (String s) -> Integer.parseInt(s);
return ImmutableSet.of(
Stream.of(1).mapToInt(i -> i * 2).sum(), Stream.of("1").mapToInt(Integer::parseInt).sum());
Stream.of(1).mapToInt(i -> i * 2).sum(),
Stream.of("1").mapToInt(Integer::parseInt).sum(),
Stream.of("1").map(parseIntFunction).reduce(0, Integer::sum));
}

ImmutableSet<Double> testStreamMapToDoubleSum() {
Function<String, Double> parseDoubleFunction = (String s) -> Double.parseDouble(s);
return ImmutableSet.of(
Stream.of(1).mapToDouble(i -> i * 2.0).sum(),
Stream.of("1").mapToDouble(Double::parseDouble).sum());
Stream.of("1").mapToDouble(Double::parseDouble).sum(),
Stream.of("1").map(parseDoubleFunction).reduce(0.0, Double::sum));
}

ImmutableSet<Long> testStreamMapToLongSum() {
Function<String, Long> parseLongFunction = (String s) -> Long.parseLong(s);
return ImmutableSet.of(
Stream.of(1).mapToLong(i -> i * 2L).sum(), Stream.of("1").mapToLong(Long::parseLong).sum());
Stream.of(1).mapToLong(i -> i * 2L).sum(),
Stream.of("1").mapToLong(Long::parseLong).sum(),
Stream.of("1").map(parseLongFunction).reduce(0L, Long::sum));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package tech.picnic.errorprone.refaster.matchers;

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;

/** A matcher of lambda expressions or method references. */
public final class IsLambdaExpressionOrMethodReference implements Matcher<ExpressionTree> {
private static final long serialVersionUID = 1L;

/** Instantiates a new {@link IsLambdaExpressionOrMethodReference} instance. */
public IsLambdaExpressionOrMethodReference() {}

@Override
public boolean matches(ExpressionTree tree, VisitorState state) {
return isLambdaExpression(tree) || isMethodReference(tree);
}

private static boolean isLambdaExpression(ExpressionTree tree) {
return tree instanceof LambdaExpressionTree;
}

private static boolean isMethodReference(ExpressionTree tree) {
return tree instanceof MemberReferenceTree
&& ((MemberReferenceTree) tree).getMode() == MemberReferenceTree.ReferenceMode.INVOKE;
}
}

0 comments on commit f6cf53e

Please sign in to comment.