Search in sources :

Example 1 with KFunction

use of kotlin.reflect.KFunction in project spring-framework by spring-projects.

the class CoroutinesUtils method invokeSuspendingFunction.

/**
 * Invoke a suspending function and converts it to {@link Mono} or
 * {@link Flux}.
 */
@SuppressWarnings("deprecation")
public static Publisher<?> invokeSuspendingFunction(Method method, Object target, Object... args) {
    KFunction<?> function = Objects.requireNonNull(ReflectJvmMapping.getKotlinFunction(method));
    if (method.isAccessible() && !KCallablesJvm.isAccessible(function)) {
        KCallablesJvm.setAccessible(function, true);
    }
    KClassifier classifier = function.getReturnType().getClassifier();
    Mono<Object> mono = MonoKt.mono(Dispatchers.getUnconfined(), (scope, continuation) -> KCallables.callSuspend(function, getSuspendedFunctionArgs(target, args), continuation)).filter(result -> !Objects.equals(result, Unit.INSTANCE)).onErrorMap(InvocationTargetException.class, InvocationTargetException::getTargetException);
    if (classifier != null && classifier.equals(JvmClassMappingKt.getKotlinClass(Flow.class))) {
        return mono.flatMapMany(CoroutinesUtils::asFlux);
    }
    return mono;
}
Also used : ReactorFlowKt(kotlinx.coroutines.reactor.ReactorFlowKt) JvmClassMappingKt(kotlin.jvm.JvmClassMappingKt) KCallables(kotlin.reflect.full.KCallables) GlobalScope(kotlinx.coroutines.GlobalScope) Deferred(kotlinx.coroutines.Deferred) Publisher(org.reactivestreams.Publisher) Dispatchers(kotlinx.coroutines.Dispatchers) Mono(reactor.core.publisher.Mono) InvocationTargetException(java.lang.reflect.InvocationTargetException) KClassifier(kotlin.reflect.KClassifier) Objects(java.util.Objects) Flow(kotlinx.coroutines.flow.Flow) MonoKt(kotlinx.coroutines.reactor.MonoKt) Flux(reactor.core.publisher.Flux) Unit(kotlin.Unit) ReflectJvmMapping(kotlin.reflect.jvm.ReflectJvmMapping) KFunction(kotlin.reflect.KFunction) KCallablesJvm(kotlin.reflect.jvm.KCallablesJvm) CoroutineStart(kotlinx.coroutines.CoroutineStart) Method(java.lang.reflect.Method) BuildersKt(kotlinx.coroutines.BuildersKt) KClassifier(kotlin.reflect.KClassifier) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 Objects (java.util.Objects)1 Unit (kotlin.Unit)1 JvmClassMappingKt (kotlin.jvm.JvmClassMappingKt)1 KClassifier (kotlin.reflect.KClassifier)1 KFunction (kotlin.reflect.KFunction)1 KCallables (kotlin.reflect.full.KCallables)1 KCallablesJvm (kotlin.reflect.jvm.KCallablesJvm)1 ReflectJvmMapping (kotlin.reflect.jvm.ReflectJvmMapping)1 BuildersKt (kotlinx.coroutines.BuildersKt)1 CoroutineStart (kotlinx.coroutines.CoroutineStart)1 Deferred (kotlinx.coroutines.Deferred)1 Dispatchers (kotlinx.coroutines.Dispatchers)1 GlobalScope (kotlinx.coroutines.GlobalScope)1 Flow (kotlinx.coroutines.flow.Flow)1 MonoKt (kotlinx.coroutines.reactor.MonoKt)1 ReactorFlowKt (kotlinx.coroutines.reactor.ReactorFlowKt)1 Publisher (org.reactivestreams.Publisher)1 Flux (reactor.core.publisher.Flux)1