|  | 
|  | 1 | +/* | 
|  | 2 | + * Copyright 2015-2025 the original author or authors. | 
|  | 3 | + * | 
|  | 4 | + * All rights reserved. This program and the accompanying materials are | 
|  | 5 | + * made available under the terms of the Eclipse Public License v2.0 which | 
|  | 6 | + * accompanies this distribution and is available at | 
|  | 7 | + * | 
|  | 8 | + * https://www.eclipse.org/legal/epl-v20.html | 
|  | 9 | + */ | 
|  | 10 | + | 
|  | 11 | +package org.junit.platform.commons.util; | 
|  | 12 | + | 
|  | 13 | +import static kotlin.jvm.JvmClassMappingKt.getJavaClass; | 
|  | 14 | +import static kotlin.reflect.full.KCallables.callSuspendBy; | 
|  | 15 | +import static kotlin.reflect.jvm.KCallablesJvm.isAccessible; | 
|  | 16 | +import static kotlin.reflect.jvm.KCallablesJvm.setAccessible; | 
|  | 17 | +import static kotlin.reflect.jvm.KTypesJvm.getJvmErasure; | 
|  | 18 | +import static kotlin.reflect.jvm.ReflectJvmMapping.getJavaType; | 
|  | 19 | +import static kotlinx.coroutines.BuildersKt.runBlocking; | 
|  | 20 | +import static org.junit.platform.commons.util.ExceptionUtils.throwAsUncheckedException; | 
|  | 21 | +import static org.junit.platform.commons.util.ReflectionUtils.getUnderlyingCause; | 
|  | 22 | + | 
|  | 23 | +import java.lang.reflect.Method; | 
|  | 24 | +import java.lang.reflect.Parameter; | 
|  | 25 | +import java.lang.reflect.Type; | 
|  | 26 | +import java.util.Arrays; | 
|  | 27 | +import java.util.HashMap; | 
|  | 28 | +import java.util.Map; | 
|  | 29 | + | 
|  | 30 | +import kotlin.Unit; | 
|  | 31 | +import kotlin.coroutines.EmptyCoroutineContext; | 
|  | 32 | +import kotlin.reflect.KFunction; | 
|  | 33 | +import kotlin.reflect.KParameter; | 
|  | 34 | +import kotlin.reflect.jvm.ReflectJvmMapping; | 
|  | 35 | + | 
|  | 36 | +class KotlinSuspendingFunctionUtils { | 
|  | 37 | + | 
|  | 38 | +	static Class<?> getReturnType(Method method) { | 
|  | 39 | +		var returnType = getJavaClass(getJvmErasure(getKotlinFunction(method).getReturnType())); | 
|  | 40 | +		if (Unit.class.equals(returnType)) { | 
|  | 41 | +			return void.class; | 
|  | 42 | +		} | 
|  | 43 | +		return returnType; | 
|  | 44 | +	} | 
|  | 45 | + | 
|  | 46 | +	static Type getGenericReturnType(Method method) { | 
|  | 47 | +		return getJavaType(getKotlinFunction(method).getReturnType()); | 
|  | 48 | +	} | 
|  | 49 | + | 
|  | 50 | +	static Parameter[] getParameters(Method method) { | 
|  | 51 | +		var parameterCount = method.getParameterCount(); | 
|  | 52 | +		if (parameterCount == 1) { | 
|  | 53 | +			return new Parameter[0]; | 
|  | 54 | +		} | 
|  | 55 | +		return Arrays.copyOf(method.getParameters(), parameterCount - 1); | 
|  | 56 | +	} | 
|  | 57 | + | 
|  | 58 | +	static Class<?>[] getParameterTypes(Method method) { | 
|  | 59 | +		var parameterCount = method.getParameterCount(); | 
|  | 60 | +		if (parameterCount == 1) { | 
|  | 61 | +			return new Class<?>[0]; | 
|  | 62 | +		} | 
|  | 63 | +		return Arrays.stream(method.getParameterTypes()).limit(parameterCount - 1).toArray(Class<?>[]::new); | 
|  | 64 | +	} | 
|  | 65 | + | 
|  | 66 | +	static Object invoke(Method method, Object target, Object[] args) { | 
|  | 67 | +		try { | 
|  | 68 | +			return invoke(getKotlinFunction(method), target, args); | 
|  | 69 | +		} | 
|  | 70 | +		catch (InterruptedException e) { | 
|  | 71 | +			throw throwAsUncheckedException(e); | 
|  | 72 | +		} | 
|  | 73 | +	} | 
|  | 74 | + | 
|  | 75 | +	private static <T> T invoke(KFunction<T> function, Object target, Object[] args) throws InterruptedException { | 
|  | 76 | +		if (!isAccessible(function)) { | 
|  | 77 | +			setAccessible(function, true); | 
|  | 78 | +		} | 
|  | 79 | +		return runBlocking(EmptyCoroutineContext.INSTANCE, (__, continuation) -> { | 
|  | 80 | +			try { | 
|  | 81 | +				return callSuspendBy(function, toArgumentMap(target, args, function), continuation); | 
|  | 82 | +			} | 
|  | 83 | +			catch (Exception e) { | 
|  | 84 | +				throw throwAsUncheckedException(getUnderlyingCause(e)); | 
|  | 85 | +			} | 
|  | 86 | +		}); | 
|  | 87 | +	} | 
|  | 88 | + | 
|  | 89 | +	private static Map<KParameter, Object> toArgumentMap(Object target, Object[] args, KFunction<?> function) { | 
|  | 90 | +		Map<KParameter, Object> arguments = new HashMap<>(args.length + 1); | 
|  | 91 | +		int index = 0; | 
|  | 92 | +		for (KParameter parameter : function.getParameters()) { | 
|  | 93 | +			switch (parameter.getKind()) { | 
|  | 94 | +				case INSTANCE -> arguments.put(parameter, target); | 
|  | 95 | +				case VALUE, EXTENSION_RECEIVER -> { | 
|  | 96 | +					arguments.put(parameter, args[index]); | 
|  | 97 | +					index++; | 
|  | 98 | +				} | 
|  | 99 | +			} | 
|  | 100 | +		} | 
|  | 101 | +		return arguments; | 
|  | 102 | +	} | 
|  | 103 | + | 
|  | 104 | +	private static KFunction<?> getKotlinFunction(Method method) { | 
|  | 105 | +		return Preconditions.notNull(ReflectJvmMapping.getKotlinFunction(method), | 
|  | 106 | +			() -> "Failed to get Kotlin function for method: " + method); | 
|  | 107 | +	} | 
|  | 108 | +} | 
0 commit comments