Update the ordering of the Templates and the tests to be in alphabetical order

This commit is contained in:
Rick Ossendrijver
2021-08-23 19:28:33 +02:00
parent ce4a1ce411
commit 8aba6197d3
3 changed files with 196 additions and 183 deletions

View File

@@ -24,39 +24,19 @@ import reactor.core.publisher.Mono;
public final class RxJavaToReactorTemplates {
private RxJavaToReactorTemplates() {}
static final class RemoveRedundantCast<T> {
static final class FluxToFlowableToFlux<T> {
@BeforeTemplate
T before(T object) {
return (T) object;
Flux<T> before(Flux<T> flux, BackpressureStrategy strategy) {
return Refaster.anyOf(
flux.as(RxJava2Adapter::fluxToObservable)
.toFlowable(strategy)
.as(RxJava2Adapter::flowableToFlux),
flux.as(RxJava2Adapter::fluxToFlowable).as(RxJava2Adapter::flowableToFlux));
}
@AfterTemplate
T after(T object) {
return object;
}
}
static final class MaybeCast<T> {
@BeforeTemplate
Maybe<T> before(Maybe<T> maybe) {
return maybe.cast(Refaster.<T>clazz());
}
@AfterTemplate
Maybe<T> after(Maybe<T> maybe) {
return maybe;
}
}
static final class MaybeWrap<T> {
@BeforeTemplate
Maybe<T> before(Maybe<T> maybe) {
return Maybe.wrap(maybe);
}
@AfterTemplate
Maybe<T> after(Maybe<T> maybe) {
return maybe;
Flux<T> after(Flux<T> flux) {
return flux;
}
}
@@ -169,7 +149,7 @@ public final class RxJavaToReactorTemplates {
}
}
// XXX: Change location and perhaps omit this one.
// XXX: Perhaps omit this one.
abstract static class MaybeDeferToMono<T> {
@Placeholder
abstract Maybe<T> maybeProducer();
@@ -185,37 +165,31 @@ public final class RxJavaToReactorTemplates {
}
}
/**
* XXX: Temporary solution, this could be fixed when we know whether the function throws an
* Exception.
*/
public static final class MyUtil {
static final class MaybeCast<T> {
@BeforeTemplate
Maybe<T> before(Maybe<T> maybe) {
return maybe.cast(Refaster.<T>clazz());
}
private MyUtil() {}
/**
* Temporary construct to convert functions that do not throw an exception
*
* <p>The idea is to convert a io.reactivex.functions.Function to a java.util.function.Function.
*
* @param function The function to convert
* @param <I> The input type
* @param <O> The output type
* @return the java.util.function.Function
*/
@SuppressWarnings({"IllegalCatch", "NoFunctionalReturnType"})
public static <I, O> java.util.function.Function<I, O> convert(
Function<? super I, ? extends O> function) {
return input -> {
try {
return function.apply(input);
} catch (Exception e) {
throw Exceptions.propagate(e);
}
};
@AfterTemplate
Maybe<T> after(Maybe<T> maybe) {
return maybe;
}
}
static final class MaybeWrap<T> {
@BeforeTemplate
Maybe<T> before(Maybe<T> maybe) {
return Maybe.wrap(maybe);
}
@AfterTemplate
Maybe<T> after(Maybe<T> maybe) {
return maybe;
}
}
// See the MyUtil for additional explanation.
static final class MaybeFlatMapFunction<I, T extends I, O, M extends MaybeSource<? extends O>> {
@BeforeTemplate
Maybe<O> before(Maybe<T> maybe, Function<I, M> function) {
@@ -235,28 +209,6 @@ public final class RxJavaToReactorTemplates {
}
}
// "Coersion" (find better name):
// instanceof (support this?)
// two functional interfaces with:
// B.return type extends A.return type
// A.param 1 type extends B.param 1 type
// ....
// B throws a subset of the exceptions thrown by A
// @CheckParameterCoersion
@SuppressWarnings("NoFunctionalReturnType")
static final class UnnecessaryConversion<I, O> {
@BeforeTemplate
java.util.function.Function<I, O> before(Function<I, O> function) {
return MyUtil.convert(function);
}
@AfterTemplate
java.util.function.Function<I, O> after(java.util.function.Function<I, O> function) {
return function;
}
}
abstract static class MaybeFlatMapLambda<S, T> {
@Placeholder
abstract Maybe<T> toMaybeFunction(@MayOptionallyUse S element);
@@ -321,6 +273,38 @@ public final class RxJavaToReactorTemplates {
}
}
static final class RemoveRedundantCast<T> {
@BeforeTemplate
T before(T object) {
return (T) object;
}
@AfterTemplate
T after(T object) {
return object;
}
}
// XXX: What should we do with the naming here? Since it is not entirely correct now.
static final class MonoToFlowableToMono<T> {
@BeforeTemplate
Mono<Void> before(Mono<Void> mono) {
return mono.as(RxJava2Adapter::monoToCompletable).as(RxJava2Adapter::completableToMono);
}
@BeforeTemplate
Mono<T> before2(Mono<T> mono) {
return Refaster.anyOf(
mono.as(RxJava2Adapter::monoToMaybe).as(RxJava2Adapter::maybeToMono),
mono.as(RxJava2Adapter::monoToSingle).as(RxJava2Adapter::singleToMono));
}
@AfterTemplate
Mono<T> after(Mono<T> mono) {
return mono;
}
}
// XXX: `function` type change; look into `Refaster.canBeCoercedTo(...)`.
static final class SingleFilter<S, T extends S> {
@BeforeTemplate
@@ -369,42 +353,6 @@ public final class RxJavaToReactorTemplates {
}
}
static final class FluxToFlowableToFlux<T> {
@BeforeTemplate
Flux<T> before(Flux<T> flux, BackpressureStrategy strategy) {
return Refaster.anyOf(
flux.as(RxJava2Adapter::fluxToObservable)
.toFlowable(strategy)
.as(RxJava2Adapter::flowableToFlux),
flux.as(RxJava2Adapter::fluxToFlowable).as(RxJava2Adapter::flowableToFlux));
}
@AfterTemplate
Flux<T> after(Flux<T> flux) {
return flux;
}
}
// XXX: Stephan, what should we do with the naming here? Since it is not entirely correct now.
static final class MonoToFlowableToMono<T> {
@BeforeTemplate
Mono<Void> before(Mono<Void> mono) {
return mono.as(RxJava2Adapter::monoToCompletable).as(RxJava2Adapter::completableToMono);
}
@BeforeTemplate
Mono<T> before2(Mono<T> mono) {
return Refaster.anyOf(
mono.as(RxJava2Adapter::monoToMaybe).as(RxJava2Adapter::maybeToMono),
mono.as(RxJava2Adapter::monoToSingle).as(RxJava2Adapter::singleToMono));
}
@AfterTemplate
Mono<T> after(Mono<T> mono) {
return mono;
}
}
// @Matches(value = DoesNotThrowException.class, arguments = "java.lang.Exception")
// @interface DoesNotThrowCheckedException {}
//
@@ -451,4 +399,57 @@ public final class RxJavaToReactorTemplates {
// return Mono.fromSupplier(callable);
// }
// }
/**
* XXX: Temporary solution, this could be fixed when we know whether the function throws an
* Exception.
*/
public static final class MyUtil {
private MyUtil() {}
/**
* Temporary construct to convert functions that do not throw an exception
*
* <p>The idea is to convert a io.reactivex.functions.Function to a java.util.function.Function.
*
* @param function The function to convert
* @param <I> The input type
* @param <O> The output type
* @return the java.util.function.Function
*/
@SuppressWarnings({"IllegalCatch", "NoFunctionalReturnType"})
public static <I, O> java.util.function.Function<I, O> convert(
Function<? super I, ? extends O> function) {
return input -> {
try {
return function.apply(input);
} catch (Exception e) {
throw Exceptions.propagate(e);
}
};
}
}
// "Coersion" (find better name):
// instanceof (support this?)
// two functional interfaces with:
// B.return type extends A.return type
// A.param 1 type extends B.param 1 type
// ....
// B throws a subset of the exceptions thrown by A
// @CheckParameterCoersion
@SuppressWarnings("NoFunctionalReturnType")
static final class UnnecessaryConversion<I, O> {
@BeforeTemplate
java.util.function.Function<I, O> before(Function<I, O> function) {
return MyUtil.convert(function);
}
@AfterTemplate
java.util.function.Function<I, O> after(java.util.function.Function<I, O> function) {
return function;
}
}
}

View File

@@ -11,20 +11,17 @@ import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
final class RxJavaToReactorTemplatesTest implements RefasterTemplateTestCase {
Maybe<String> testRemoveRedundantCast() {
return (Maybe<String>) Maybe.just("foo");
}
Maybe<String> testMaybeCastPositive() {
return Maybe.just("string").cast(String.class);
}
Flux<Integer> testFluxToFlowableToFlux() {
Flowable.just(1)
.as(RxJava2Adapter::flowableToFlux)
.map(e -> e + e)
.as(RxJava2Adapter::fluxToFlowable)
.as(RxJava2Adapter::flowableToFlux)
.flatMap(e -> ImmutableSet::of)
.as(RxJava2Adapter::fluxToFlowable);
Maybe<Object> testMaybeCastNegative() {
return Maybe.just("string").cast(Object.class);
}
Maybe<Integer> testMaybeWrap() {
return Maybe.wrap(Maybe.just(1));
return Flux.just(2).as(RxJava2Adapter::fluxToFlowable).as(RxJava2Adapter::flowableToFlux);
}
// XXX: Discuss with Stephan, look at the Publisher which is of type Flowable, that won't work...
@@ -62,6 +59,23 @@ final class RxJavaToReactorTemplatesTest implements RefasterTemplateTestCase {
}));
}
Maybe<String> testMaybeDeferToMono() {
return Maybe.just("test");
// XXX: Fill this in
}
Maybe<String> testMaybeCastPositive() {
return Maybe.just("string").cast(String.class);
}
Maybe<Object> testMaybeCastNegative() {
return Maybe.just("string").cast(Object.class);
}
Maybe<Integer> testMaybeWrap() {
return Maybe.wrap(Maybe.just(1));
}
// XXX: This should be fixed later with `Refaster.canBeCoercedTo(...)`
Maybe<Integer> testMaybeFlatMapFunction() {
Maybe.just(1).flatMap(this::exampleMethod);
@@ -102,28 +116,8 @@ final class RxJavaToReactorTemplatesTest implements RefasterTemplateTestCase {
}));
}
Maybe<Integer> testSingleFilter() {
return Single.just(1).filter(i -> i > 2);
}
Single<Integer> testSingleFlatMapLambda() {
return Single.just(1).flatMap(i -> Single.just(i * 2));
}
Single<Integer> testSingleMap() {
return Single.just(1).map(i -> i + 1);
}
Flux<Integer> testFluxToFlowableToFlux() {
Flowable.just(1)
.as(RxJava2Adapter::flowableToFlux)
.map(e -> e + e)
.as(RxJava2Adapter::fluxToFlowable)
.as(RxJava2Adapter::flowableToFlux)
.flatMap(e -> ImmutableSet::of)
.as(RxJava2Adapter::fluxToFlowable);
return Flux.just(2).as(RxJava2Adapter::fluxToFlowable).as(RxJava2Adapter::flowableToFlux);
Maybe<String> testRemoveRedundantCast() {
return (Maybe<String>) Maybe.just("foo");
}
Mono<Integer> testMonoToFlowableToMono() {
@@ -139,4 +133,16 @@ final class RxJavaToReactorTemplatesTest implements RefasterTemplateTestCase {
return Mono.just(3).as(RxJava2Adapter::monoToMaybe).as(RxJava2Adapter::maybeToMono);
}
Maybe<Integer> testSingleFilter() {
return Single.just(1).filter(i -> i > 2);
}
Single<Integer> testSingleFlatMapLambda() {
return Single.just(1).flatMap(i -> Single.just(i * 2));
}
Single<Integer> testSingleMap() {
return Single.just(1).map(i -> i + 1);
}
}

View File

@@ -12,20 +12,15 @@ import reactor.core.publisher.Mono;
import tech.picnic.errorprone.refastertemplates.RxJavaToReactorTemplates;
final class RxJavaToReactorTemplatesTest implements RefasterTemplateTestCase {
Maybe<String> testRemoveRedundantCast() {
return Maybe.just("foo");
}
Maybe<String> testMaybeCastPositive() {
return Maybe.just("string");
}
Flux<Integer> testFluxToFlowableToFlux() {
Flowable.just(1)
.as(RxJava2Adapter::flowableToFlux)
.map(e -> e + e)
.flatMap(e -> ImmutableSet::of)
.as(RxJava2Adapter::fluxToFlowable);
Maybe<Object> testMaybeCastNegative() {
return Maybe.just("string").cast(Object.class);
}
Maybe<Integer> testMaybeWrap() {
return Maybe.just(1);
return Flux.just(2);
}
// XXX: Discuss with Stephan, look at the Publisher which is of type Flowable, that won't work...
@@ -86,6 +81,23 @@ final class RxJavaToReactorTemplatesTest implements RefasterTemplateTestCase {
.as(RxJava2Adapter::fluxToFlowable);
}
Maybe<String> testMaybeDeferToMono() {
return Maybe.just("test");
// XXX: Fill this in
}
Maybe<String> testMaybeCastPositive() {
return Maybe.just("string");
}
Maybe<Object> testMaybeCastNegative() {
return Maybe.just("string").cast(Object.class);
}
Maybe<Integer> testMaybeWrap() {
return Maybe.just(1);
}
// XXX: This should be fixed later with `Refaster.canBeCoercedTo(...)`
Maybe<Integer> testMaybeFlatMapFunction() {
Maybe.just(1)
@@ -159,6 +171,22 @@ final class RxJavaToReactorTemplatesTest implements RefasterTemplateTestCase {
.as(RxJava2Adapter::monoToSingle);
}
Maybe<String> testRemoveRedundantCast() {
return Maybe.just("foo");
}
Mono<Integer> testMonoToFlowableToMono() {
Single.just(1)
.as(RxJava2Adapter::singleToMono)
.map(e -> e + e)
.filter(i -> i > 2)
.as(RxJava2Adapter::monoToSingle);
Mono.empty().then();
return Mono.just(3);
}
Maybe<Integer> testSingleFilter() {
return Single.just(1)
.as(RxJava2Adapter::singleToMono)
@@ -179,26 +207,4 @@ final class RxJavaToReactorTemplatesTest implements RefasterTemplateTestCase {
.map(i -> i + 1)
.as(RxJava2Adapter::monoToSingle);
}
Flux<Integer> testFluxToFlowableToFlux() {
Flowable.just(1)
.as(RxJava2Adapter::flowableToFlux)
.map(e -> e + e)
.flatMap(e -> ImmutableSet::of)
.as(RxJava2Adapter::fluxToFlowable);
return Flux.just(2);
}
Mono<Integer> testMonoToFlowableToMono() {
Single.just(1)
.as(RxJava2Adapter::singleToMono)
.map(e -> e + e)
.filter(i -> i > 2)
.as(RxJava2Adapter::monoToSingle);
Mono.empty().then();
return Mono.just(3);
}
}