Add many templates and tiny improvements. Note: tests are broken now because building against picnic-error-prone

This commit is contained in:
Rick Ossendrijver
2021-09-11 12:01:42 +02:00
committed by Pieter Dirk Soels
parent 4cc885c6e4
commit 5777c510fd
13 changed files with 214 additions and 40 deletions

View File

@@ -9,6 +9,8 @@ import com.google.errorprone.refaster.annotation.BeforeTemplate;
import com.google.errorprone.refaster.annotation.UseImportPolicy;
import io.reactivex.Completable;
import io.reactivex.CompletableSource;
import io.reactivex.Single;
import io.reactivex.SingleSource;
import io.reactivex.functions.Action;
import java.util.concurrent.Callable;
import java.util.function.Supplier;
@@ -141,6 +143,23 @@ final class RxJavaCompletableToReactorTemplates {
// XXX: public final Observable andThen(ObservableSource)
// XXX: public final Flowable andThen(Publisher)
// XXX: public final Single andThen(SingleSource)
// XXX: Verify this case
static final class CompletableAndThenSingle<T> {
@BeforeTemplate
Single<T> before(Completable completable, SingleSource<T> source) {
return completable.andThen(source);
}
@AfterTemplate
Single<T> after(Completable completable, SingleSource<T> source) {
return completable
.as(RxJava2Adapter::completableToMono)
.then(Single.wrap(source).as(RxJava2Adapter::singleToMono))
.as(RxJava2Adapter::monoToSingle);
}
}
// XXX: public final Object as(CompletableConverter)
// XXX: public final void blockingAwait()
// XXX: public final boolean blockingAwait(long,TimeUnit)

View File

@@ -137,7 +137,6 @@ final class RxJavaFlowableToReactorTemplates {
}
}
// XXX: Use `CanBeCoercedTo`.
static final class FlowableErrorCallable<T> {
@BeforeTemplate
Flowable<T> before(Callable<? extends Throwable> throwable) {
@@ -145,11 +144,14 @@ final class RxJavaFlowableToReactorTemplates {
}
@AfterTemplate
Flowable<T> after(Supplier<? extends Throwable> throwable) {
return RxJava2Adapter.fluxToFlowable(Flux.error(throwable));
Flowable<T> after(Callable<? extends Throwable> throwable) {
return RxJava2Adapter.fluxToFlowable(
Flux.error(RxJava2ReactorMigrationUtil.callableAsSupplier(throwable)));
}
}
// XXX: Thing to remove the callableAssupplier for this.
static final class FlowableErrorThrowable<T> {
@BeforeTemplate
Flowable<T> before(Throwable throwable) {
@@ -372,6 +374,7 @@ final class RxJavaFlowableToReactorTemplates {
}
}
// XXX: MOVE?
static final class BiFunctionRemoveUtil<T, U, R> {
@BeforeTemplate
java.util.function.BiFunction<? super T, ? super U, ? extends R> before(
@@ -729,10 +732,9 @@ final class RxJavaFlowableToReactorTemplates {
// XXX: final Flowable lift(FlowableOperator)
// XXX: final Flowable limit(long)
// XXX: `Refaster.canBeCoercedTo(...)`.
static final class FlowableMap<I, T extends I, O> {
@BeforeTemplate
Flowable<O> before(Flowable<T> flowable, Function<I, O> function) {
Flowable<O> before(Flowable<T> flowable, @CanTransformToTargetType Function<I, O> function) {
return flowable.map(function);
}
@@ -750,7 +752,7 @@ final class RxJavaFlowableToReactorTemplates {
// XXX: final Flowable mergeWith(MaybeSource)
// XXX: final Flowable mergeWith(Publisher)
// XXX: Check whethter the `toFlowable()` is correct.
// XXX: Check whether the `toFlowable()` is correct.
static final class FlowableMergeWith<T> {
@BeforeTemplate

View File

@@ -14,6 +14,7 @@ import io.reactivex.Flowable;
import io.reactivex.Maybe;
import io.reactivex.MaybeSource;
import io.reactivex.Single;
import io.reactivex.SingleSource;
import io.reactivex.functions.Action;
import io.reactivex.functions.Function;
import java.util.Arrays;
@@ -98,6 +99,23 @@ final class RxJavaMaybeToReactorTemplates {
* tagId.map(Maybe::just).orElseGet(Maybe::empty)) - .flatMapSingleElement(this::getTagById) -
* .ignoreElement()); + .flatMapSingleElement(this::getTagById).as(RxJava2Adapter::maybeToMono).
*/
/// XXX: Check this one is required for case above.
abstract static class MaybeDeferFirst<T> {
@Placeholder
abstract Maybe<T> maybeProducer();
@BeforeTemplate
Maybe<T> before() {
return Maybe.defer(() -> maybeProducer());
}
@AfterTemplate
Maybe<T> after() {
return Mono.defer(() -> maybeProducer().as(RxJava2Adapter::maybeToMono))
.as(RxJava2Adapter::monoToMaybe);
}
}
abstract static class MaybeDefer<T> {
@Placeholder
abstract Maybe<T> maybeProducer();
@@ -125,7 +143,6 @@ final class RxJavaMaybeToReactorTemplates {
}
}
// XXX: Use `CanBeCoercedTo`.
static final class MaybeErrorCallable<T> {
@BeforeTemplate
Maybe<T> before(Callable<? extends Throwable> throwable) {
@@ -133,8 +150,10 @@ final class RxJavaMaybeToReactorTemplates {
}
@AfterTemplate
Maybe<T> after(Supplier<? extends Throwable> throwable) {
return RxJava2Adapter.monoToMaybe(Mono.error(throwable));
Maybe<T> after(Callable<? extends Throwable> throwable) {
return RxJava2Adapter.monoToMaybe(
Mono.error(
RxJavaToReactorTemplates.RxJava2ReactorMigrationUtil.callableAsSupplier(throwable)));
}
}
@@ -196,8 +215,36 @@ final class RxJavaMaybeToReactorTemplates {
// XXX: public static Maybe fromFuture(Future,long,TimeUnit)
// XXX: public static Maybe fromRunnable(Runnable)
static final class MaybeFromRunnable<T> {
@BeforeTemplate
Maybe<T> before(Runnable runnable) {
Maybe.fromRunnable(() -> {
int i = 1 + 1;
});
return Maybe.fromRunnable(runnable);
}
@AfterTemplate
Maybe<T> after(Runnable runnable) {
return RxJava2Adapter.monoToMaybe(Mono.fromRunnable(runnable));
}
}
// XXX: public static Maybe fromSingle(SingleSource)
// XXX: public static Maybe just(Object)
static final class MaybeJust<T> {
@BeforeTemplate
Maybe<T> before(T item) {
return Maybe.just(item);
}
@AfterTemplate
Maybe<T> after(T item) {
return RxJava2Adapter.monoToMaybe(Mono.just(item));
}
}
// XXX: public static Flowable merge(Iterable)
// XXX: public static Maybe merge(MaybeSource)
// XXX: public static Flowable merge(MaybeSource,MaybeSource)
@@ -451,19 +498,34 @@ final class RxJavaMaybeToReactorTemplates {
// XXX: public final void subscribe(MaybeObserver)
// XXX: public final Maybe subscribeOn(Scheduler)
// XXX: public final MaybeObserver subscribeWith(MaybeObserver)
// XXX: public final Maybe switchIfEmpty(MaybeSource)
// XXX: Make test
static final class MaybeSourceSwitchIfEmpty<S, T extends S> {
@BeforeTemplate
Maybe<S> before(Maybe<S> maybe, MaybeSource<T> maybeSource) {
return maybe.switchIfEmpty(maybeSource);
}
@AfterTemplate
Maybe<S> after(Maybe<S> maybe, MaybeSource<T> maybeSource) {
return maybe
.as(RxJava2Adapter::maybeToMono)
.switchIfEmpty(RxJava2Adapter.maybeToMono(Maybe.wrap(maybeSource)))
.as(RxJava2Adapter::monoToMaybe);
}
}
static final class MaybeSwitchIfEmpty<S, T extends S> {
@BeforeTemplate
Single<S> before(Maybe<S> maybe, Single<T> single) {
Single<S> before(Maybe<S> maybe, SingleSource<T> single) {
return maybe.switchIfEmpty(single);
}
@AfterTemplate
Single<S> after(Maybe<S> maybe, Single<T> single) {
Single<S> after(Maybe<S> maybe, SingleSource<T> single) {
return maybe
.as(RxJava2Adapter::maybeToMono)
.switchIfEmpty(single.as(RxJava2Adapter::singleToMono))
.switchIfEmpty(RxJava2Adapter.singleToMono(Single.wrap(single)))
.as(RxJava2Adapter::monoToSingle);
}
}

View File

@@ -44,8 +44,8 @@ final class RxJavaSingleToReactorTemplates {
}
@AfterTemplate
Single<T> after(Supplier<? extends Throwable> throwable) {
return RxJava2Adapter.monoToSingle(Mono.error(throwable));
Single<T> after(Callable<? extends Throwable> throwable) {
return RxJava2Adapter.monoToSingle(Mono.error(RxJavaToReactorTemplates.RxJava2ReactorMigrationUtil.callableAsSupplier(throwable)));
}
}
@@ -61,7 +61,6 @@ final class RxJavaSingleToReactorTemplates {
}
}
// XXX: Can be coerced to.
static final class SingleFromCallable<T> {
@BeforeTemplate
Single<T> before(Callable<? extends T> callable) {
@@ -91,7 +90,7 @@ final class RxJavaSingleToReactorTemplates {
@AfterTemplate
Single<T> after(T item) {
return Mono.just(item).as(RxJava2Adapter::monoToSingle);
return RxJava2Adapter.monoToSingle(Mono.just(item));
}
}
@@ -113,7 +112,19 @@ final class RxJavaSingleToReactorTemplates {
// XXX: public static Single unsafeCreate(SingleSource)
// XXX: public static Single using(Callable,Function,Consumer)
// XXX: public static Single using(Callable,Function,Consumer,boolean)
// XXX: public static Single wrap(SingleSource) --> Required
static final class SingleWrap<T> {
@BeforeTemplate
Single<T> before(Single<T> single) {
return Single.wrap(single);
}
@AfterTemplate
Single<T> after(Single<T> single) {
return single;
}
}
// XXX: public static Single zip(Iterable,Function)
// XXX: public static Single zip(SingleSource,SingleSource,BiFunction)
// XXX: public static Single zip(SingleSource,SingleSource,SingleSource,Function3)
@@ -159,7 +170,7 @@ final class RxJavaSingleToReactorTemplates {
// XXX: public final Single doOnSuccess(Consumer) --> Required
// XXX: public final Single doOnTerminate(Action)
// XXX: `function` type change; look into `Refaster.canBeCoercedTo(...)`.
// XXX: `function` type change; look into `Refaster.canBeCoercedTo(...)`. or to JdkPredicate.
static final class SingleFilter<S, T extends S> {
@BeforeTemplate
Maybe<T> before(Single<T> single, Predicate<S> predicate) {

View File

@@ -3,8 +3,10 @@ package tech.picnic.errorprone.refastertemplates;
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 io.reactivex.BackpressureStrategy;
import io.reactivex.functions.Action;
import io.reactivex.functions.BiFunction;
import io.reactivex.functions.Function;
import java.util.concurrent.Callable;
import java.util.function.Supplier;
@@ -22,6 +24,7 @@ public final class RxJavaToReactorTemplates {
Flux<T> before(Flux<T> flux, BackpressureStrategy strategy) {
return Refaster.anyOf(
RxJava2Adapter.fluxToFlowable(flux).as(RxJava2Adapter::flowableToFlux),
RxJava2Adapter.flowableToFlux(RxJava2Adapter.fluxToFlowable(flux)),
flux.as(RxJava2Adapter::fluxToObservable)
.toFlowable(strategy)
.as(RxJava2Adapter::flowableToFlux),
@@ -43,7 +46,8 @@ public final class RxJavaToReactorTemplates {
return Refaster.anyOf(
RxJava2Adapter.monoToCompletable(mono).as(RxJava2Adapter::completableToMono),
mono.as(RxJava2Adapter::monoToCompletable).as(RxJava2Adapter::completableToMono),
RxJava2Adapter.completableToMono(RxJava2Adapter.monoToCompletable(mono)));
RxJava2Adapter.completableToMono(RxJava2Adapter.monoToCompletable(mono)),
RxJava2Adapter.completableToMono(mono.as(RxJava2Adapter::monoToCompletable)));
}
@BeforeTemplate
@@ -51,9 +55,11 @@ public final class RxJavaToReactorTemplates {
return Refaster.anyOf(
RxJava2Adapter.monoToMaybe(mono).as(RxJava2Adapter::maybeToMono),
RxJava2Adapter.maybeToMono(RxJava2Adapter.monoToMaybe(mono)),
RxJava2Adapter.maybeToMono(mono.as(RxJava2Adapter::monoToMaybe)),
mono.as(RxJava2Adapter::monoToMaybe).as(RxJava2Adapter::maybeToMono),
RxJava2Adapter.monoToSingle(mono).as(RxJava2Adapter::singleToMono),
RxJava2Adapter.singleToMono(RxJava2Adapter.monoToSingle(mono)),
RxJava2Adapter.singleToMono(mono.as(RxJava2Adapter::monoToSingle)),
mono.as(RxJava2Adapter::monoToSingle).as(RxJava2Adapter::singleToMono));
}
@@ -63,18 +69,46 @@ public final class RxJavaToReactorTemplates {
}
}
static final class RemoveRedundantCast<T> {
// XXX: Does this make sense?
// This is what we want to fix: Mono.error(RxJava2ReactorMigrationUtil.callableAsSupplier(() ->
// new ItemNotFoundException("Banner", bannerId))));
static final class MonoErrorCallableSupplierUtil<T> {
@BeforeTemplate
T before(T object) {
return (T) object;
Mono<T> before(Callable<? extends Throwable> callable) {
return Mono.error(RxJava2ReactorMigrationUtil.callableAsSupplier(callable));
}
@AfterTemplate
T after(T object) {
return object;
Mono<T> after(Supplier<? extends Throwable> callable) {
return Mono.error(callable);
}
}
static final class RemoveUtilCallable<T> {
@BeforeTemplate
Supplier<T> before(@CanTransformToTargetType Callable<T> callable) {
return RxJava2ReactorMigrationUtil.callableAsSupplier(callable);
}
@AfterTemplate
Supplier<T> before(Supplier<T> callable) {
return callable;
}
}
// This triggerred something in PRP what should not be changed.
// static final class RemoveRedundantCast<T> {
// @BeforeTemplate
// T before(T object) {
// return (T) object;
// }
//
// @AfterTemplate
// T after(T object) {
// return object;
// }
// }
// XXX: Use `CanBeCoercedTo`
@SuppressWarnings("NoFunctionalReturnType")
static final class UnnecessaryConversion<I, O> {
@@ -196,6 +230,7 @@ public final class RxJavaToReactorTemplates {
/**
* XXX
*
* @param action XXX
* @return XXX
*/

View File

@@ -46,9 +46,10 @@ final class RxJavaFlowableToReactorTemplatesTest implements RefasterTemplateTest
Flowable<Object> testFlowableErrorCallable() {
return RxJava2Adapter.fluxToFlowable(
Flux.error(
() -> {
throw new IllegalStateException();
}));
RxJavaToReactorTemplates.RxJava2ReactorMigrationUtil.callableAsSupplier(
() -> {
throw new IllegalStateException();
})));
}
Flowable<Integer> testFlowableFromArray() {

View File

@@ -61,6 +61,16 @@ final class RxJavaMaybeToReactorTemplatesTest implements RefasterTemplateTestCas
return Maybe.fromFuture(new CompletableFuture<>());
}
Maybe<Integer> testMaybeFromRunnable() {
return Maybe.fromRunnable(() -> {
int i = 1 + 1;
});
}
Maybe<Integer> testMaybeJust() {
return Maybe.just(1);
}
Maybe<Integer> testMaybeWrap() {
return Maybe.wrap(Maybe.just(1));
}

View File

@@ -72,6 +72,18 @@ final class RxJavaMaybeToReactorTemplatesTest implements RefasterTemplateTestCas
return RxJava2Adapter.monoToMaybe(Mono.fromFuture(new CompletableFuture<>()));
}
Maybe<Integer> testMaybeFromRunnable() {
return RxJava2Adapter.monoToMaybe(
Mono.fromRunnable(
() -> {
int i = 1 + 1;
}));
}
Maybe<Integer> testMaybeJust() {
return RxJava2Adapter.monoToMaybe(Mono.just(1));
}
Maybe<Integer> testMaybeWrap() {
return Maybe.just(1);
}

View File

@@ -26,6 +26,10 @@ final class RxJavaSingleToReactorTemplatesTest implements RefasterTemplateTestCa
return Single.just(1);
}
Single<Integer> testSingleWrap() {
return Single.wrap(Single.just(1));
}
Maybe<Integer> testSingleFilter() {
return Single.just(1).filter(i -> i > 2);
}

View File

@@ -17,9 +17,10 @@ final class RxJavaSingleToReactorTemplatesTest implements RefasterTemplateTestCa
Single<Object> testSingleErrorCallable() {
return RxJava2Adapter.monoToSingle(
Mono.error(
() -> {
throw new IllegalStateException();
}));
RxJavaToReactorTemplates.RxJava2ReactorMigrationUtil.callableAsSupplier(
() -> {
throw new IllegalStateException();
})));
}
Single<Integer> testSingleFromCallable() {
@@ -29,7 +30,11 @@ final class RxJavaSingleToReactorTemplatesTest implements RefasterTemplateTestCa
}
Single<Integer> testSingleJust() {
return Mono.just(1).as(RxJava2Adapter::monoToSingle);
return RxJava2Adapter.monoToSingle(Mono.just(1));
}
Single<Integer> testSingleWrap() {
return Single.just(1);
}
Maybe<Integer> testSingleFilter() {

View File

@@ -7,6 +7,7 @@ import io.reactivex.Single;
import reactor.adapter.rxjava.RxJava2Adapter;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import tech.picnic.errorprone.refastertemplates.RxJavaToReactorTemplates;
final class RxJavaToReactorTemplatesTest implements RefasterTemplateTestCase {
@@ -39,4 +40,12 @@ final class RxJavaToReactorTemplatesTest implements RefasterTemplateTestCase {
return Mono.just(3).as(RxJava2Adapter::monoToMaybe).as(RxJava2Adapter::maybeToMono);
}
Mono<Integer> testMonoErrorCallableSupplierUtil() {
return Mono.just(1)
.switchIfEmpty(
Mono.error(
RxJavaToReactorTemplates.RxJava2ReactorMigrationUtil.callableAsSupplier(
() -> new IllegalStateException())));
}
}

View File

@@ -35,4 +35,8 @@ final class RxJavaToReactorTemplatesTest implements RefasterTemplateTestCase {
return Mono.just(3);
}
Mono<Integer> testMonoErrorCallableSupplierUtil() {
return Mono.just(1).switchIfEmpty(Mono.error(() -> new IllegalStateException()));
}
}

14
pom.xml
View File

@@ -1438,13 +1438,13 @@
fork, some other dependencies depend on the official (i.e.,
non-forked) `error_prone_annotations`. Here we fix which
version is pulled in. -->
<dependencies>
<dependency>
<groupId>com.google.errorprone</groupId>
<artifactId>error_prone_annotations</artifactId>
<version>${version.error-prone-orig}</version>
</dependency>
</dependencies>
<!-- <dependencies>-->
<!-- <dependency>-->
<!-- <groupId>com.google.errorprone</groupId>-->
<!-- <artifactId>error_prone_annotations</artifactId>-->
<!-- <version>${version.error-prone-orig}</version>-->
<!-- </dependency>-->
<!-- </dependencies>-->
</dependencyManagement>
<build>
<pluginManagement>