Skip to content

Commit

Permalink
Address comments to improve readability
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamedsamehsalah committed May 31, 2023
1 parent a27838d commit fc4bc6f
Showing 1 changed file with 19 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@
import com.google.errorprone.util.ASTHelpers;
import com.sun.source.tree.MethodInvocationTree;
import com.sun.tools.javac.code.Type;
import java.util.function.Function;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Mono;
import reactor.core.publisher.Flux;

/**
* A {@link BugChecker} that flags nesting of {@link Publisher Publishers} inside {@link Mono
* Monos}.
*/
/** A {@link BugChecker} that flags nesting of {@link Publisher Publishers}. */
@AutoService(BugChecker.class)
@BugPattern(
summary =
Expand All @@ -39,8 +37,7 @@ public final class NestedPublishers extends BugChecker implements MethodInvocati
private static final long serialVersionUID = 1L;
private static final Supplier<Type> PUBLISHER = type("org.reactivestreams.Publisher");
private static final Supplier<Type> PUBLISHER_OF_PUBLISHERS =
VisitorState.memoize(
generic(PUBLISHER, subOf(generic(type("org.reactivestreams.Publisher"), unbound()))));
VisitorState.memoize(generic(PUBLISHER, subOf(generic(PUBLISHER, unbound()))));
private static final Supplier<Type> GROUPED_FLUX =
VisitorState.memoize(
generic(
Expand All @@ -56,19 +53,28 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState
Type publisherOfPublisherType = PUBLISHER_OF_PUBLISHERS.get(state);
Type groupedFluxType = GROUPED_FLUX.get(state);
Type treeType = ASTHelpers.getType(tree);
if ((publisherOfPublisherType == null
|| !state.getTypes().isSubtype(treeType, publisherOfPublisherType))
|| (groupedFluxType != null
&& isTypeArgumentGroupedFlux(state, groupedFluxType, treeType))) {
if (!isNestedPublisher(state, publisherOfPublisherType, treeType)
|| isTypeArgumentGroupedFlux(state, groupedFluxType, treeType)) {
return Description.NO_MATCH;
}

return describeMatch(tree);
}

private static boolean isNestedPublisher(
VisitorState state, Type publisherOfPublisherType, Type treeType) {
return publisherOfPublisherType != null
&& state.getTypes().isSubtype(treeType, publisherOfPublisherType);
}

/**
* Excluding the type when it matches {@code Flux<GroupedFlux<K, V>>} to not flag usages of {@link
* Flux#groupBy(Function)}.
*/
private static boolean isTypeArgumentGroupedFlux(
VisitorState state, Type groupedFluxType, Type treeType) {
return treeType.getTypeArguments().stream()
.anyMatch(typez -> ASTHelpers.isSameType(typez, groupedFluxType, state));
return groupedFluxType != null
&& treeType.getTypeArguments().stream()
.anyMatch(typez -> ASTHelpers.isSameType(typez, groupedFluxType, state));
}
}

0 comments on commit fc4bc6f

Please sign in to comment.