Throw an exception for suspending factory methods
Suspending factory methods are not supported, and can have side effects, so it is better to fail explicitly for such use case. Closes gh-32719
This commit is contained in:
@@ -720,7 +720,7 @@ public class MethodParameter {
|
||||
else if (this.executable instanceof Constructor<?> constructor) {
|
||||
parameterNames = discoverer.getParameterNames(constructor);
|
||||
}
|
||||
if (parameterNames != null) {
|
||||
if (parameterNames != null && this.parameterIndex < parameterNames.length) {
|
||||
this.parameterName = parameterNames[this.parameterIndex];
|
||||
}
|
||||
this.parameterNameDiscoverer = null;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
* Copyright 2002-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -20,6 +20,7 @@ import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
import org.springframework.util.ReflectionUtils
|
||||
import kotlin.coroutines.Continuation
|
||||
|
||||
/**
|
||||
* Abstract tests for Kotlin [ParameterNameDiscoverer] aware implementations.
|
||||
@@ -46,6 +47,14 @@ abstract class AbstractReflectionParameterNameDiscovererKotlinTests(protected va
|
||||
assertThat(actualMethodParams).contains("message")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun getParameterNamesOnSuspendingFunction() {
|
||||
val method = ReflectionUtils.findMethod(CoroutinesMessageService::class.java, "sendMessage",
|
||||
String::class.java, Continuation::class.java)!!
|
||||
val actualMethodParams = parameterNameDiscoverer.getParameterNames(method)
|
||||
assertThat(actualMethodParams).containsExactly("message")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun getParameterNamesOnExtensionMethod() {
|
||||
val method = ReflectionUtils.findMethod(UtilityClass::class.java, "identity", String::class.java)!!
|
||||
@@ -65,4 +74,8 @@ abstract class AbstractReflectionParameterNameDiscovererKotlinTests(protected va
|
||||
fun String.identity() = this
|
||||
}
|
||||
|
||||
class CoroutinesMessageService {
|
||||
suspend fun sendMessage(message: String) = message
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -114,6 +114,27 @@ class MethodParameterKotlinTests {
|
||||
assertThat(returnGenericParameterType("suspendFun8")).isEqualTo(Object::class.java)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Parameter name for regular function`() {
|
||||
val methodParameter = returnMethodParameter("nullable", 0)
|
||||
methodParameter.initParameterNameDiscovery(KotlinReflectionParameterNameDiscoverer())
|
||||
assertThat(methodParameter.getParameterName()).isEqualTo("nullable")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Parameter name for suspending function`() {
|
||||
val methodParameter = returnMethodParameter("suspendFun", 0)
|
||||
methodParameter.initParameterNameDiscovery(KotlinReflectionParameterNameDiscoverer())
|
||||
assertThat(methodParameter.getParameterName()).isEqualTo("p1")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Continuation parameter name for suspending function`() {
|
||||
val methodParameter = returnMethodParameter("suspendFun", 1)
|
||||
methodParameter.initParameterNameDiscovery(KotlinReflectionParameterNameDiscoverer())
|
||||
assertThat(methodParameter.getParameterName()).isNull()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Continuation parameter is optional`() {
|
||||
val method = this::class.java.getDeclaredMethod("suspendFun", String::class.java, Continuation::class.java)
|
||||
@@ -126,8 +147,8 @@ class MethodParameterKotlinTests {
|
||||
private fun returnGenericParameterTypeName(funName: String) = returnGenericParameterType(funName).typeName
|
||||
private fun returnGenericParameterTypeBoundName(funName: String) = (returnGenericParameterType(funName) as TypeVariable<*>).bounds[0].typeName
|
||||
|
||||
private fun returnMethodParameter(funName: String) =
|
||||
MethodParameter(this::class.declaredFunctions.first { it.name == funName }.javaMethod!!, -1)
|
||||
private fun returnMethodParameter(funName: String, parameterIndex: Int = -1) =
|
||||
MethodParameter(this::class.declaredFunctions.first { it.name == funName }.javaMethod!!, parameterIndex)
|
||||
|
||||
@Suppress("unused_parameter")
|
||||
fun nullable(nullable: String?): Int? = 42
|
||||
|
||||
Reference in New Issue
Block a user