-
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.
Introduce
MonoOfPublishers
bug check
- Loading branch information
mohamedsamehsalah
committed
May 21, 2023
1 parent
3bbae43
commit d6838c0
Showing
3 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/MonoOfPublishers.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,58 @@ | ||
package tech.picnic.errorprone.bugpatterns; | ||
|
||
import static com.google.errorprone.BugPattern.LinkType.CUSTOM; | ||
import static com.google.errorprone.BugPattern.SeverityLevel.WARNING; | ||
import static com.google.errorprone.BugPattern.StandardTags.FRAGILE_CODE; | ||
import static tech.picnic.errorprone.bugpatterns.util.Documentation.BUG_PATTERNS_BASE_URL; | ||
import static tech.picnic.errorprone.bugpatterns.util.MoreTypes.generic; | ||
import static tech.picnic.errorprone.bugpatterns.util.MoreTypes.subOf; | ||
import static tech.picnic.errorprone.bugpatterns.util.MoreTypes.type; | ||
import static tech.picnic.errorprone.bugpatterns.util.MoreTypes.unbound; | ||
|
||
import com.google.auto.service.AutoService; | ||
import com.google.errorprone.BugPattern; | ||
import com.google.errorprone.VisitorState; | ||
import com.google.errorprone.bugpatterns.BugChecker; | ||
import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher; | ||
import com.google.errorprone.matchers.Description; | ||
import com.google.errorprone.suppliers.Supplier; | ||
import com.google.errorprone.suppliers.Suppliers; | ||
import com.google.errorprone.util.ASTHelpers; | ||
import com.sun.source.tree.MethodInvocationTree; | ||
import com.sun.tools.javac.code.Type; | ||
import org.reactivestreams.Publisher; | ||
import reactor.core.publisher.Mono; | ||
|
||
/** | ||
* A {@link BugChecker} that flags nesting of {@link Publisher Publishers} inside {@link Mono | ||
* Monos}. | ||
*/ | ||
@AutoService(BugChecker.class) | ||
@BugPattern( | ||
summary = | ||
"Avoid nesting `Publisher`s inside `Mono`s; the resultant code is hard to reason about", | ||
link = BUG_PATTERNS_BASE_URL + "MonoOfPublishers", | ||
linkType = CUSTOM, | ||
severity = WARNING, | ||
tags = FRAGILE_CODE) | ||
public final class MonoOfPublishers extends BugChecker implements MethodInvocationTreeMatcher { | ||
private static final long serialVersionUID = 1L; | ||
private static final Supplier<Type> MONO = | ||
Suppliers.typeFromString("reactor.core.publisher.Mono"); | ||
private static final Supplier<Type> MONO_OF_PUBLISHERS = | ||
VisitorState.memoize( | ||
generic(MONO, subOf(generic(type("org.reactivestreams.Publisher"), unbound())))); | ||
|
||
/** Instantiates a new {@link MonoOfPublishers} instance. */ | ||
public MonoOfPublishers() {} | ||
|
||
@Override | ||
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { | ||
Type type = MONO_OF_PUBLISHERS.get(state); | ||
if (type == null || !state.getTypes().isSubtype(ASTHelpers.getType(tree), type)) { | ||
return Description.NO_MATCH; | ||
} | ||
|
||
return describeMatch(tree); | ||
} | ||
} |
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
45 changes: 45 additions & 0 deletions
45
...-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/MonoOfPublishersTest.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,45 @@ | ||
package tech.picnic.errorprone.bugpatterns; | ||
|
||
import com.google.errorprone.CompilationTestHelper; | ||
import org.junit.jupiter.api.Test; | ||
|
||
final class MonoOfPublishersTest { | ||
@Test | ||
void identification() { | ||
CompilationTestHelper.newInstance(MonoOfPublishers.class, getClass()) | ||
.addSourceLines( | ||
"A.java", | ||
"import reactor.core.publisher.Flux;", | ||
"import reactor.core.publisher.Mono;", | ||
"", | ||
"class A {", | ||
" void m() {", | ||
" Mono.empty();", | ||
" Mono.just(1);", | ||
" // BUG: Diagnostic contains:", | ||
" Mono.just(Mono.empty());", | ||
" // BUG: Diagnostic contains:", | ||
" Mono.just(Mono.just(1));", | ||
" // BUG: Diagnostic contains:", | ||
" Mono.just(Flux.empty());", | ||
" // BUG: Diagnostic contains:", | ||
" Mono.just(Flux.just(1));", | ||
" // BUG: Diagnostic contains:", | ||
" Mono.just(1).map(Mono::just);", | ||
" // BUG: Diagnostic contains:", | ||
" Mono.just(1).map(Flux::just);", | ||
"", | ||
" Mono.justOrEmpty(null);", | ||
" // BUG: Diagnostic contains:", | ||
" Mono.justOrEmpty(Mono.just(1));", | ||
" // BUG: Diagnostic contains:", | ||
" Mono.justOrEmpty(Flux.just(1));", | ||
" // BUG: Diagnostic contains:", | ||
" Mono.justOrEmpty(1).map(Mono::just);", | ||
" // BUG: Diagnostic contains:", | ||
" Mono.justOrEmpty(1).map(Flux::just);", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
} |