From 8e8d75792a8ae4f6dfd6eaa6786782a11c970c1f Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Fri, 15 Oct 2021 13:03:02 +0200 Subject: [PATCH] Many rewrites --- .../RxJavaCompletableToReactorTemplates.java | 6 +- .../RxJavaFlowableToReactorTemplates.java | 96 ++++++++++++------- .../RxJavaMaybeToReactorTemplates.java | 7 +- .../RxJavaSingleToReactorTemplates.java | 7 +- ...mpletableToReactorTemplatesTestOutput.java | 6 +- ...vaFlowableToReactorTemplatesTestInput.java | 14 +++ ...aFlowableToReactorTemplatesTestOutput.java | 4 +- ...JavaMaybeToReactorTemplatesTestOutput.java | 2 +- ...avaSingleToReactorTemplatesTestOutput.java | 4 +- 9 files changed, 91 insertions(+), 55 deletions(-) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/RxJavaCompletableToReactorTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/RxJavaCompletableToReactorTemplates.java index 54873260..1571ce87 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/RxJavaCompletableToReactorTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/RxJavaCompletableToReactorTemplates.java @@ -45,11 +45,11 @@ final class RxJavaCompletableToReactorTemplates { @AfterTemplate @UseImportPolicy(ImportPolicy.IMPORT_CLASS_DIRECTLY) Completable after(Iterable sources) { - return Mono.firstWithSignal( + return RxJava2Adapter.monoToCompletable( + Mono.firstWithSignal( Streams.stream(sources) .map(RxJava2Adapter::completableToMono) - .collect(toImmutableList())) - .as(RxJava2Adapter::monoToCompletable); + .collect(toImmutableList()))); } } diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/RxJavaFlowableToReactorTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/RxJavaFlowableToReactorTemplates.java index 5a20e35f..177c39c4 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/RxJavaFlowableToReactorTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/RxJavaFlowableToReactorTemplates.java @@ -10,7 +10,6 @@ import com.google.errorprone.refaster.ImportPolicy; 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.CanTransformToTargetType; import com.google.errorprone.refaster.annotation.Matches; import com.google.errorprone.refaster.annotation.MayOptionallyUse; import com.google.errorprone.refaster.annotation.Placeholder; @@ -28,7 +27,6 @@ import io.reactivex.flowables.GroupedFlowable; import io.reactivex.functions.BiFunction; import io.reactivex.functions.Function; import io.reactivex.functions.Predicate; -import java.time.Duration; import java.util.Collection; import java.util.Comparator; import java.util.List; @@ -37,7 +35,6 @@ import java.util.concurrent.Callable; import java.util.concurrent.TimeUnit; import org.reactivestreams.Publisher; import reactor.adapter.rxjava.RxJava2Adapter; -import reactor.core.Exceptions; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import reactor.test.StepVerifier; @@ -608,24 +605,44 @@ final class RxJavaFlowableToReactorTemplates { // XXX: final Flowable concatMapMaybe(Function,int) - static final class FlowableConcatMapMaybeDelayError { + // XXX: Better one below? + // static final class FlowableConcatMapMaybeDelayError { + // @BeforeTemplate + // Flowable before( + // Flowable flowable, Function> function) + // { + // return flowable.concatMapMaybeDelayError(function); + // } + // + // @AfterTemplate + // Flowable after( + // Flowable flowable, Function> function) + // { + // return RxJava2Adapter.fluxToFlowable( + // RxJava2Adapter.flowableToFlux(flowable) + // .concatMapDelayError( + // e -> + // Maybe.wrap( + // RxJavaReactorMigrationUtil.toJdkFunction( + // (Function>) function) + // .apply(e)) + // .toFlowable())); + // } + // } + static final class FlowableConcatMapMaybeDelayError> { @BeforeTemplate - Flowable before( - Flowable flowable, Function> function) { + Flowable before( + Flowable flowable, Function> function) { return flowable.concatMapMaybeDelayError(function); } @AfterTemplate - Flowable after( - Flowable flowable, Function> function) { + Flowable after(Flowable flowable, Function function) { return RxJava2Adapter.fluxToFlowable( RxJava2Adapter.flowableToFlux(flowable) .concatMapDelayError( e -> - Maybe.wrap( - RxJavaReactorMigrationUtil.toJdkFunction( - (Function>) function) - .apply(e)) + Maybe.wrap(RxJavaReactorMigrationUtil.toJdkFunction(function).apply(e)) .toFlowable())); } } @@ -862,45 +879,50 @@ final class RxJavaFlowableToReactorTemplates { // XXX: final Flowable flatMapIterable(Function,BiFunction,int) // XXX: final Flowable flatMapIterable(Function,int) - static final class FlowableFlatMapMaybe { - @BeforeTemplate - Flowable before( - Flowable flowable, Function> function) { - return flowable.flatMapMaybe(function); - } - - @AfterTemplate - Flowable after( - Flowable flowable, Function> function) { - return RxJava2Adapter.fluxToFlowable( - RxJava2Adapter.flowableToFlux(flowable) - .flatMap( - e -> - RxJava2Adapter.maybeToMono( - Maybe.wrap( - RxJavaReactorMigrationUtil.toJdkFunction( - (Function>) function) - .apply(e))))); - } - } + // XXX : Below is a better version. + // static final class FlowableFlatMapMaybe { + // @BeforeTemplate + // Flowable before( + // Flowable flowable, Function> function) + // { + // return flowable.flatMapMaybe(function); + // } + // + // @AfterTemplate + // Flowable after( + // Flowable flowable, Function> function) + // { + // return RxJava2Adapter.fluxToFlowable( + // RxJava2Adapter.flowableToFlux(flowable) + // .flatMap( + // e -> + // RxJava2Adapter.maybeToMono( + // Maybe.wrap( + // RxJavaReactorMigrationUtil.toJdkFunction( + // (Function>) function) + // .apply(e))))); + // } + // } // This one is now tested and good! + // XXX: Perhaps improved by improving the before template with ? extends and such. static final class FlowableFlatMapMaybeSecond< - S, T extends S, R, P extends R, Q extends MaybeSource

> { + S, T extends S, R, P extends R, M extends MaybeSource

> { @BeforeTemplate - Flowable before(Flowable flowable, Function function) { + Flowable before( + Flowable flowable, Function> function) { return flowable.flatMapMaybe(function); } @AfterTemplate - Flowable after(Flowable flowable, Function function) { + Flowable after(Flowable flowable, Function function) { return RxJava2Adapter.fluxToFlowable( RxJava2Adapter.flowableToFlux(flowable) .flatMap( e -> RxJava2Adapter.maybeToMono( Maybe.wrap( - RxJavaReactorMigrationUtil.toJdkFunction(function).apply(e))))); + RxJavaReactorMigrationUtil.toJdkFunction(function).apply(e))))); } } diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/RxJavaMaybeToReactorTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/RxJavaMaybeToReactorTemplates.java index ab9aec34..8a4f588c 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/RxJavaMaybeToReactorTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/RxJavaMaybeToReactorTemplates.java @@ -118,7 +118,7 @@ final class RxJavaMaybeToReactorTemplates { @AfterTemplate Maybe after() { return RxJava2Adapter.monoToMaybe( - Mono.defer(() -> maybeProducer().as(RxJava2Adapter::maybeToMono))); + Mono.defer(() -> RxJava2Adapter.maybeToMono(maybeProducer()))); } } @@ -133,7 +133,7 @@ final class RxJavaMaybeToReactorTemplates { @AfterTemplate Mono after() { - return Mono.defer(() -> maybeProducer().as(RxJava2Adapter::maybeToMono)); + return Mono.defer(() -> RxJava2Adapter.maybeToMono(maybeProducer())); } } @@ -429,7 +429,8 @@ final class RxJavaMaybeToReactorTemplates { static final class MaybeFlatMapFunction< I, T extends I, O, X extends O, M extends MaybeSource> { @BeforeTemplate - Maybe before(Maybe maybe, Function> function) { + Maybe before( + Maybe maybe, Function> function) { return maybe.flatMap(function); } diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/RxJavaSingleToReactorTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/RxJavaSingleToReactorTemplates.java index 0d9061a1..29451925 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/RxJavaSingleToReactorTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/RxJavaSingleToReactorTemplates.java @@ -61,7 +61,7 @@ final class RxJavaSingleToReactorTemplates { @AfterTemplate Single after() { return RxJava2Adapter.monoToSingle( - Mono.defer(() -> singleProducer().as(RxJava2Adapter::singleToMono))); + Mono.defer(() -> RxJava2Adapter.singleToMono(singleProducer()))); } } @@ -354,7 +354,7 @@ final class RxJavaSingleToReactorTemplates { Single after(Single single) { return RxJava2Adapter.monoToSingle( RxJava2Adapter.singleToMono(single) - .flatMap(v -> toSingleFunction(v).as(RxJava2Adapter::singleToMono))); + .flatMap(v -> RxJava2Adapter.singleToMono(toSingleFunction(v)))); } } @@ -581,8 +581,7 @@ final class RxJavaSingleToReactorTemplates { @AfterTemplate Single after(Single single, Function> function) { return RxJava2Adapter.monoToSingle( - single - .as(RxJava2Adapter::singleToMono) + RxJava2Adapter.singleToMono(single) .onErrorResume( err -> RxJava2Adapter.singleToMono( diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/RxJavaCompletableToReactorTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/RxJavaCompletableToReactorTemplatesTestOutput.java index bdec40d3..5c939e91 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/RxJavaCompletableToReactorTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/RxJavaCompletableToReactorTemplatesTestOutput.java @@ -20,11 +20,11 @@ final class RxJavaCompletableReactorTemplatesTest implements RefasterTemplateTes } Completable testCompletableAmb() { - return Mono.firstWithSignal( + return RxJava2Adapter.monoToCompletable( + Mono.firstWithSignal( Streams.stream(Arrays.asList(Completable.complete(), Completable.complete())) .map(RxJava2Adapter::completableToMono) - .collect(ImmutableList.toImmutableList())) - .as(RxJava2Adapter::monoToCompletable); + .collect(ImmutableList.toImmutableList()))); } Completable testCompletableComplete() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/RxJavaFlowableToReactorTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/RxJavaFlowableToReactorTemplatesTestInput.java index b0f5e2f4..3368361e 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/RxJavaFlowableToReactorTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/RxJavaFlowableToReactorTemplatesTestInput.java @@ -92,6 +92,20 @@ final class RxJavaFlowableToReactorTemplatesTest implements RefasterTemplateTest return Flowable.just(1).flatMapCompletable(integer2 -> Completable.complete()); } + Completable testFlowableFlatMapCompletableUnwrap() { + return RxJava2Adapter.monoToCompletable( + RxJava2Adapter.flowableToFlux(Flowable.just(1)) + .flatMap( + y -> + RxJava2Adapter.completableToMono( + Completable.wrap( + RxJavaReactorMigrationUtil.toJdkFunction( + (Function) + integer2 -> Completable.complete()) + .apply(y)))) + .then()); + } + Completable testFlowableUnwrapLambda() { return RxJava2Adapter.monoToCompletable( RxJava2Adapter.flowableToFlux(Flowable.just(1)) diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/RxJavaFlowableToReactorTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/RxJavaFlowableToReactorTemplatesTestOutput.java index d58d770e..34200637 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/RxJavaFlowableToReactorTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/RxJavaFlowableToReactorTemplatesTestOutput.java @@ -212,8 +212,8 @@ final class RxJavaFlowableToReactorTemplatesTest implements RefasterTemplateTest .concatMapDelayError( e -> Maybe.wrap( - RxJavaReactorMigrationUtil.toJdkFunction( - (Function>) Maybe::just) + RxJavaReactorMigrationUtil.>toJdkFunction( + Maybe::just) .apply(e)) .toFlowable())); } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/RxJavaMaybeToReactorTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/RxJavaMaybeToReactorTemplatesTestOutput.java index 2f09e4ba..a7ff577b 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/RxJavaMaybeToReactorTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/RxJavaMaybeToReactorTemplatesTestOutput.java @@ -42,7 +42,7 @@ final class RxJavaMaybeToReactorTemplatesTest implements RefasterTemplateTestCas } Mono testMaybeDefer() { - return Mono.defer(() -> Maybe.just("test").as(RxJava2Adapter::maybeToMono)); + return Mono.defer(() -> RxJava2Adapter.maybeToMono(Maybe.just("test"))); } Maybe testMaybeEmpty() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/RxJavaSingleToReactorTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/RxJavaSingleToReactorTemplatesTestOutput.java index 6a48f63a..c057a281 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/RxJavaSingleToReactorTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/RxJavaSingleToReactorTemplatesTestOutput.java @@ -27,7 +27,7 @@ final class RxJavaSingleToReactorTemplatesTest implements RefasterTemplateTestCa Single testSingleDefer() { return RxJava2Adapter.monoToSingle( - Mono.defer(() -> Single.just(1).as(RxJava2Adapter::singleToMono))); + Mono.defer(() -> RxJava2Adapter.singleToMono(Single.just(1)))); } Single testSingleErrorCallable() { @@ -94,7 +94,7 @@ final class RxJavaSingleToReactorTemplatesTest implements RefasterTemplateTestCa Single testSingleFlatMapLambda() { return RxJava2Adapter.monoToSingle( RxJava2Adapter.singleToMono(Single.just(1)) - .flatMap(i -> Single.just(i * 2).as(RxJava2Adapter::singleToMono))); + .flatMap(i -> RxJava2Adapter.singleToMono(Single.just(i * 2)))); } Completable testSingleFlatMapCompletable() {