mirror of
https://github.com/jlengrand/error-prone-support.git
synced 2026-03-10 08:11:25 +00:00
Improve the templates
This commit is contained in:
@@ -2,13 +2,17 @@ package tech.picnic.errorprone.refastertemplates;
|
||||
|
||||
import com.google.errorprone.refaster.annotation.AfterTemplate;
|
||||
import com.google.errorprone.refaster.annotation.BeforeTemplate;
|
||||
import io.reactivex.Flowable;
|
||||
import io.reactivex.Maybe;
|
||||
import io.reactivex.*;
|
||||
import io.reactivex.functions.Function;
|
||||
import io.reactivex.functions.Predicate;
|
||||
import org.reactivestreams.Publisher;
|
||||
import reactor.adapter.rxjava.RxJava2Adapter;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
final class RxJavaToReactorTemplates {
|
||||
private RxJavaToReactorTemplates() {}
|
||||
@@ -47,6 +51,7 @@ final class RxJavaToReactorTemplates {
|
||||
}
|
||||
|
||||
// XXX: I don't think calling `next()` here is the right way...
|
||||
// Also look at the tests...
|
||||
static final class FlowableFirstElementInReactor<T> {
|
||||
@BeforeTemplate
|
||||
Maybe<T> before(Flowable<T> flowable) {
|
||||
@@ -55,23 +60,39 @@ final class RxJavaToReactorTemplates {
|
||||
|
||||
@AfterTemplate
|
||||
Maybe<T> after(Flowable<T> flowable) {
|
||||
return flowable
|
||||
.as(RxJava2Adapter::flowableToFlux)
|
||||
.next()
|
||||
.as(RxJava2Adapter::monoToMaybe);
|
||||
return flowable.as(RxJava2Adapter::flowableToFlux).next().as(RxJava2Adapter::monoToMaybe);
|
||||
}
|
||||
}
|
||||
|
||||
static final class MaybeSwitchIfEmptyInReactor<I> {
|
||||
@BeforeTemplate
|
||||
Single<I> before(Maybe<I> maybe, Callable<? extends Throwable> throwable) {
|
||||
return maybe.switchIfEmpty(Single.error(throwable));
|
||||
}
|
||||
|
||||
// default Single<Warehouse> getWarehouse(WarehouseId warehouseId) {
|
||||
// return getAllWarehouses()
|
||||
// .filter(warehouse -> warehouse.getId().equals(warehouseId))
|
||||
// .firstElement()
|
||||
// .switchIfEmpty(
|
||||
// Single.error(
|
||||
// itemNotFound(Warehouse.class.getName(), warehouseId.toString())
|
||||
// ::get));
|
||||
// }
|
||||
@AfterTemplate
|
||||
Single<I> after(Maybe<I> maybe, Supplier<? extends Throwable> throwable) {
|
||||
return maybe
|
||||
.as(RxJava2Adapter::maybeToMono)
|
||||
.switchIfEmpty(Mono.error(throwable))
|
||||
.as(RxJava2Adapter::monoToSingle);
|
||||
}
|
||||
}
|
||||
|
||||
static final class FlowableSwitchIfEmptyInReactor<I> {
|
||||
@BeforeTemplate
|
||||
Flowable<I> before(Flowable<I> flowable, Callable<? extends Throwable> throwable) {
|
||||
return flowable.switchIfEmpty(Flowable.error(throwable));
|
||||
}
|
||||
|
||||
@AfterTemplate
|
||||
Flowable<I> after(Flowable<I> flowable, Supplier<? extends Throwable> throwable) {
|
||||
return flowable
|
||||
.as(RxJava2Adapter::flowableToFlux)
|
||||
.switchIfEmpty(Flux.error(throwable))
|
||||
.as(RxJava2Adapter::fluxToFlowable);
|
||||
}
|
||||
}
|
||||
|
||||
static final class RemoveUnnecessaryConversion<I> {
|
||||
@BeforeTemplate
|
||||
@@ -84,40 +105,41 @@ final class RxJavaToReactorTemplates {
|
||||
return flux;
|
||||
}
|
||||
}
|
||||
}
|
||||
// static final class FlowableToMapInReactor<I, O> {
|
||||
// @BeforeTemplate
|
||||
// Single<Map<O, I>> before(Flowable<I> flowable, Function<? super I, ? extends O> function) {
|
||||
// return flowable.toMap(function);
|
||||
// }
|
||||
//
|
||||
// @AfterTemplate
|
||||
// Single<Map<O, I>> after(Flowable<I> flowable, java.util.function.Function<? super I, ?
|
||||
// extends O> function) {
|
||||
// return flowable.as(RxJava2Adapter::flowableToFlux)
|
||||
// .collectMap(function)
|
||||
// .as(RxJava2Adapter::monoToSingle);
|
||||
// }
|
||||
// }
|
||||
|
||||
// static final class FlowableToMapInReactor<I, O> {
|
||||
// @BeforeTemplate
|
||||
// Single<Map<O, I>> before(Flowable<I> flowable, Function<? super I, ? extends O> function) {
|
||||
// return flowable.toMap(function);
|
||||
// }
|
||||
//
|
||||
// @AfterTemplate
|
||||
// Single<Map<O, I>> after(Flowable<I> flowable, java.util.function.Function<? super I, ?
|
||||
// extends O> function) {
|
||||
// return flowable.as(RxJava2Adapter::flowableToFlux)
|
||||
// .collectMap(function)
|
||||
// .as(RxJava2Adapter::monoToSingle);
|
||||
// }
|
||||
// }
|
||||
|
||||
// Check this with Stephan.
|
||||
// static final class FlowableMapToFluxMapToFlowable<T, R> {
|
||||
// @BeforeTemplate
|
||||
// Flowable<R> before(Flowable<T> flowable, Function<? super T, ? extends R> function) {
|
||||
// return flowable.map(function);
|
||||
// }
|
||||
//
|
||||
// @AfterTemplate
|
||||
// Flowable<R> after(
|
||||
// Flowable<T> flowable, java.util.function.Function<? super T, ? extends R> function) {
|
||||
// return flowable
|
||||
// .as(RxJava2Adapter::flowableToFlux)
|
||||
// .map(function)
|
||||
// .as(RxJava2Adapter::fluxToFlowable);
|
||||
// // Moeten we hier ook iets doen met Refaster.canBeCoercedTo()
|
||||
// // omdat we moeten weten dat het geen Flux<Object> maar Flux<T> is...
|
||||
// }
|
||||
// }
|
||||
// static final class FlowableMapToFluxMapToFlowable<T, R> {
|
||||
// @BeforeTemplate
|
||||
// Flowable<R> before(Flowable<T> flowable, Function<? super T, ? extends R> function) {
|
||||
// return flowable.map(function);
|
||||
// }
|
||||
//
|
||||
// @AfterTemplate
|
||||
// Flowable<R> after(
|
||||
// Flowable<T> flowable, java.util.function.Function<? super T, ? extends R> function) {
|
||||
// return flowable
|
||||
// .as(RxJava2Adapter::flowableToFlux)
|
||||
// .map(function)
|
||||
// .as(RxJava2Adapter::fluxToFlowable); // <Flowable<T>>
|
||||
// // Moeten we hier ook iets doen met Refaster.canBeCoercedTo()
|
||||
// // omdat we moeten weten dat het geen Flux<Object> maar Flux<T> is...
|
||||
// }
|
||||
// }
|
||||
|
||||
// Stephan: Bad return type in method reference: cannot convert io.reactivex.Flowable<T> to
|
||||
// io.reactivex.Flowable<java.lang.Object}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,9 @@ import com.google.errorprone.BugCheckerRefactoringTestHelper;
|
||||
import com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import io.reactivex.Maybe;
|
||||
import io.reactivex.Single;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
@@ -164,6 +167,12 @@ public final class RefasterCheckTest {
|
||||
}
|
||||
|
||||
private void verifyRefactoring(String groupName, String templateNamePattern) {
|
||||
Single<Integer> integerSingle = Maybe.just(1)
|
||||
.switchIfEmpty(
|
||||
Single.error(
|
||||
() -> {
|
||||
throw new IllegalStateException();
|
||||
}));
|
||||
createRestrictedRefactoringTestHelper(templateNamePattern)
|
||||
.addInput(groupName + "TemplatesTestInput.java")
|
||||
.addOutput(groupName + "TemplatesTestOutput.java")
|
||||
|
||||
@@ -2,9 +2,11 @@ package tech.picnic.errorprone.bugpatterns;
|
||||
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import io.reactivex.Flowable;
|
||||
import io.reactivex.Maybe;
|
||||
import io.reactivex.Single;
|
||||
import reactor.adapter.rxjava.RxJava2Adapter;
|
||||
import reactor.core.publisher.Flux;
|
||||
import io.reactivex.Maybe;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
final class RxJavaToReactorTemplatesTest implements RefasterTemplateTestCase {
|
||||
Flowable<Object> testFlowableFlatMapInReactor() { // look at the return type...
|
||||
@@ -15,11 +17,29 @@ final class RxJavaToReactorTemplatesTest implements RefasterTemplateTestCase {
|
||||
return Flowable.just(1).filter(i -> i > 2);
|
||||
}
|
||||
|
||||
// ImmutableSet<Flowable<Integer>> testFlowableFirstElementInReactor() {
|
||||
// return ImmutableSet.of(
|
||||
// Maybe.just(1).toFlowable().firstElement(),
|
||||
// Maybe.empty().toFlowable().firstElement());
|
||||
// }
|
||||
ImmutableSet<Flowable<Integer>> testFlowableFirstElementInReactor() {
|
||||
return ImmutableSet.of(
|
||||
Flowable<Integer>.toMaybe(::evenFilter).firstElement(),
|
||||
Maybe.<Integer>empty().toFlowable().firstElement());
|
||||
}
|
||||
|
||||
Single<Integer> testMaybeSwitchIfEmptyInReactor() {
|
||||
return Maybe.just(1)
|
||||
.switchIfEmpty(
|
||||
Single.error(
|
||||
() -> {
|
||||
throw new IllegalStateException();
|
||||
}));
|
||||
}
|
||||
|
||||
Flowable<Integer> testFlowableSwitchIfEmptyInReactor() {
|
||||
return Flowable.just(1)
|
||||
.switchIfEmpty(
|
||||
Flowable.error(
|
||||
() -> {
|
||||
throw new IllegalStateException();
|
||||
}));
|
||||
}
|
||||
|
||||
Flux<Integer> testRemoveUnnecessaryConversion() {
|
||||
Flowable.just(1)
|
||||
|
||||
@@ -2,9 +2,11 @@ package tech.picnic.errorprone.bugpatterns;
|
||||
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import io.reactivex.Flowable;
|
||||
import io.reactivex.Maybe;
|
||||
import io.reactivex.Single;
|
||||
import reactor.adapter.rxjava.RxJava2Adapter;
|
||||
import reactor.core.publisher.Flux;
|
||||
import io.reactivex.Maybe;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
final class RxJavaToReactorTemplatesTest implements RefasterTemplateTestCase {
|
||||
Flowable<Object> testFlowableFlatMapInReactor() { // look at the return type...
|
||||
@@ -21,11 +23,33 @@ final class RxJavaToReactorTemplatesTest implements RefasterTemplateTestCase {
|
||||
.as(RxJava2Adapter::fluxToFlowable);
|
||||
}
|
||||
|
||||
// ImmutableSet<Maybe<Integer>> testFlowableFirstElementInReactor() {
|
||||
// return ImmutableSet.of(
|
||||
// Flowable.just(1).as(RxJava2Adapter::flowableToFlux).next().as(RxJava2Adapter::monoToMaybe),
|
||||
// Flowable.empty().as(RxJava2Adapter::flowableToFlux).next().as(RxJava2Adapter::monoToMaybe));
|
||||
// }
|
||||
ImmutableSet<Maybe<Integer>> testFlowableFirstElementInReactor() {
|
||||
return ImmutableSet.of(
|
||||
Flowable.just(1).as(RxJava2Adapter::flowableToFlux).next().as(RxJava2Adapter::monoToMaybe),
|
||||
Flowable.empty().as(RxJava2Adapter::flowableToFlux).next().as(RxJava2Adapter::monoToMaybe));
|
||||
}
|
||||
|
||||
Single<Integer> testMaybeSwitchIfEmptyInReactor() {
|
||||
return Maybe.just(1)
|
||||
.as(RxJava2Adapter::maybeToMono)
|
||||
.switchIfEmpty(
|
||||
Mono.error(
|
||||
() -> {
|
||||
throw new IllegalStateException();
|
||||
}))
|
||||
.as(RxJava2Adapter::monoToSingle);
|
||||
}
|
||||
|
||||
Flowable<Integer> testFlowableSwitchIfEmptyInReactor() {
|
||||
return Flowable.just(1)
|
||||
.as(RxJava2Adapter::flowableToFlux)
|
||||
.switchIfEmpty(
|
||||
Flux.error(
|
||||
() -> {
|
||||
throw new IllegalStateException();
|
||||
}))
|
||||
.as(RxJava2Adapter::fluxToFlowable);
|
||||
}
|
||||
|
||||
Flux<Integer> testRemoveUnnecessaryConversion() {
|
||||
Flowable.just(1)
|
||||
|
||||
Reference in New Issue
Block a user