-
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.
Improve publisher before template matching
- Loading branch information
1 parent
49c3848
commit 881cc12
Showing
5 changed files
with
171 additions
and
44 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
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
37 changes: 37 additions & 0 deletions
37
...pport/src/main/java/tech/picnic/errorprone/refaster/matchers/IsFunctionReturningMono.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,37 @@ | ||
package tech.picnic.errorprone.refaster.matchers; | ||
|
||
import static com.google.errorprone.matchers.Matchers.isSameType; | ||
|
||
import com.google.errorprone.VisitorState; | ||
import com.google.errorprone.matchers.Matcher; | ||
import com.google.errorprone.suppliers.Suppliers; | ||
import com.sun.source.tree.ExpressionTree; | ||
import com.sun.source.tree.LambdaExpressionTree; | ||
import com.sun.source.tree.Tree; | ||
import com.sun.source.tree.Tree.Kind; | ||
|
||
/** | ||
* A matcher of lambda expressions that are of type {@code java.util.function.Function} that returns | ||
* a {@code reactor.core.publisher.Mono}. | ||
*/ | ||
public final class IsFunctionReturningMono implements Matcher<ExpressionTree> { | ||
private static final long serialVersionUID = 1L; | ||
private static final Matcher<Tree> MONO_TYPE = | ||
isSameType(Suppliers.typeFromString("reactor.core.publisher.Mono")); | ||
private static final Matcher<Tree> FUNCTION_TYPE = | ||
isSameType(Suppliers.typeFromString("java.util.function.Function")); | ||
|
||
/** Instantiates a new {@link IsFunctionReturningMono} instance. */ | ||
public IsFunctionReturningMono() {} | ||
|
||
@Override | ||
public boolean matches(ExpressionTree tree, VisitorState state) { | ||
if (tree.getKind() != Kind.LAMBDA_EXPRESSION) { | ||
return false; | ||
} | ||
|
||
LambdaExpressionTree lambdaExpressionTree = (LambdaExpressionTree) tree; | ||
return MONO_TYPE.matches(lambdaExpressionTree.getBody(), state) | ||
&& FUNCTION_TYPE.matches(lambdaExpressionTree, state); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...t/src/test/java/tech/picnic/errorprone/refaster/matchers/IsFunctionReturningMonoTest.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,44 @@ | ||
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.Test; | ||
|
||
final class IsFunctionReturningMonoTest { | ||
@Test | ||
void matches() { | ||
CompilationTestHelper.newInstance(MatcherTestChecker.class, getClass()) | ||
.addSourceLines( | ||
"A.java", | ||
"import java.util.function.Function;", | ||
"import java.util.function.Supplier;", | ||
"import reactor.core.publisher.Flux;", | ||
"import reactor.core.publisher.Mono;", | ||
"", | ||
"class A {", | ||
"// BUG: Diagnostic contains:", | ||
" Function<String, Mono<String>> positive = s -> Mono.just(s);", | ||
"", | ||
" Function<String, Flux<String>> negative = s -> Flux.just(s);", | ||
"", | ||
" Supplier<Mono<String>> negative2 = () -> Mono.just(\"s\");", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
/** A {@link BugChecker} that simply delegates to {@link IsFunctionReturningMono}. */ | ||
@BugPattern(summary = "Flags expressions matched by `IsFunctionReturningMono`", severity = ERROR) | ||
public static final class MatcherTestChecker extends AbstractMatcherTestChecker { | ||
private static final long serialVersionUID = 1L; | ||
|
||
// XXX: This is a false positive reported by Checkstyle. See | ||
// https://github.com/checkstyle/checkstyle/issues/10161#issuecomment-1242732120. | ||
@SuppressWarnings("RedundantModifier") | ||
public MatcherTestChecker() { | ||
super(new IsFunctionReturningMono()); | ||
} | ||
} | ||
} |