|
25 | 25 | import kotlin.coroutines.CoroutineContext;
|
26 | 26 | import kotlin.jvm.JvmClassMappingKt;
|
27 | 27 | import kotlin.reflect.KClass;
|
28 |
| -import kotlin.reflect.KClassifier; |
29 | 28 | import kotlin.reflect.KFunction;
|
30 | 29 | import kotlin.reflect.KParameter;
|
31 | 30 | import kotlin.reflect.KType;
|
32 | 31 | import kotlin.reflect.full.KCallables;
|
33 | 32 | import kotlin.reflect.full.KClasses;
|
| 33 | +import kotlin.reflect.full.KClassifiers; |
| 34 | +import kotlin.reflect.full.KTypes; |
34 | 35 | import kotlin.reflect.jvm.KCallablesJvm;
|
35 | 36 | import kotlin.reflect.jvm.KTypesJvm;
|
36 | 37 | import kotlin.reflect.jvm.ReflectJvmMapping;
|
|
58 | 59 | */
|
59 | 60 | public abstract class CoroutinesUtils {
|
60 | 61 |
|
| 62 | + private static final KType flowType = KClassifiers.getStarProjectedType(JvmClassMappingKt.getKotlinClass(Flow.class)); |
| 63 | + |
| 64 | + private static final KType monoType = KClassifiers.getStarProjectedType(JvmClassMappingKt.getKotlinClass(Mono.class)); |
| 65 | + |
| 66 | + private static final KType publisherType = KClassifiers.getStarProjectedType(JvmClassMappingKt.getKotlinClass(Publisher.class)); |
| 67 | + |
61 | 68 | /**
|
62 | 69 | * Convert a {@link Deferred} instance to a {@link Mono}.
|
63 | 70 | */
|
@@ -137,18 +144,15 @@ public static Publisher<?> invokeSuspendingFunction(CoroutineContext context, Me
|
137 | 144 | .filter(result -> result != Unit.INSTANCE)
|
138 | 145 | .onErrorMap(InvocationTargetException.class, InvocationTargetException::getTargetException);
|
139 | 146 |
|
140 |
| - KClassifier returnType = function.getReturnType().getClassifier(); |
141 |
| - if (returnType != null) { |
142 |
| - if (returnType.equals(JvmClassMappingKt.getKotlinClass(Flow.class))) { |
143 |
| - return mono.flatMapMany(CoroutinesUtils::asFlux); |
144 |
| - } |
145 |
| - else if (returnType.equals(JvmClassMappingKt.getKotlinClass(Mono.class))) { |
146 |
| - return mono.flatMap(o -> ((Mono<?>)o)); |
147 |
| - } |
148 |
| - else if (returnType instanceof KClass<?> kClass && |
149 |
| - Publisher.class.isAssignableFrom(JvmClassMappingKt.getJavaClass(kClass))) { |
150 |
| - return mono.flatMapMany(o -> ((Publisher<?>)o)); |
151 |
| - } |
| 147 | + KType returnType = function.getReturnType(); |
| 148 | + if (KTypes.isSubtypeOf(returnType, flowType)) { |
| 149 | + return mono.flatMapMany(CoroutinesUtils::asFlux); |
| 150 | + } |
| 151 | + else if (KTypes.isSubtypeOf(returnType, monoType)) { |
| 152 | + return mono.flatMap(o -> ((Mono<?>)o)); |
| 153 | + } |
| 154 | + else if (KTypes.isSubtypeOf(returnType, publisherType)) { |
| 155 | + return mono.flatMapMany(o -> ((Publisher<?>)o)); |
152 | 156 | }
|
153 | 157 | return mono;
|
154 | 158 | }
|
|
0 commit comments