-
Notifications
You must be signed in to change notification settings - Fork 37.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
9 changed files
with
200 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...c/test/kotlin/org/springframework/beans/factory/support/ConstructorResolverKotlinTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.beans.factory.support | ||
|
||
import org.assertj.core.api.Assertions | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.beans.BeanWrapper | ||
import org.springframework.beans.factory.BeanCreationException | ||
import org.springframework.beans.factory.config.BeanDefinition | ||
import org.springframework.beans.testfixture.beans.factory.generator.factory.KotlinFactory | ||
|
||
class ConstructorResolverKotlinTests { | ||
|
||
@Test | ||
fun instantiateBeanInstanceWithBeanClassAndFactoryMethodName() { | ||
val beanFactory = DefaultListableBeanFactory() | ||
val beanDefinition: BeanDefinition = BeanDefinitionBuilder | ||
.rootBeanDefinition(KotlinFactory::class.java).setFactoryMethod("create") | ||
.beanDefinition | ||
val beanWrapper = instantiate(beanFactory, beanDefinition) | ||
Assertions.assertThat(beanWrapper.wrappedInstance).isEqualTo("test") | ||
} | ||
|
||
@Test | ||
fun instantiateBeanInstanceWithBeanClassAndSuspendingFactoryMethodName() { | ||
val beanFactory = DefaultListableBeanFactory() | ||
val beanDefinition: BeanDefinition = BeanDefinitionBuilder | ||
.rootBeanDefinition(KotlinFactory::class.java).setFactoryMethod("suspendingCreate") | ||
.beanDefinition | ||
Assertions.assertThatThrownBy { instantiate(beanFactory, beanDefinition, null) } | ||
.isInstanceOf(BeanCreationException::class.java) | ||
.hasMessageContaining("suspending functions are not supported") | ||
|
||
} | ||
|
||
private fun instantiate(beanFactory: DefaultListableBeanFactory, beanDefinition: BeanDefinition, | ||
vararg explicitArgs: Any?): BeanWrapper { | ||
return ConstructorResolver(beanFactory) | ||
.instantiateUsingFactoryMethod("testBean", (beanDefinition as RootBeanDefinition), explicitArgs) | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...rc/testFixtures/kotlin/org/springframework/beans/testfixture/beans/KotlinConfiguration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.beans.testfixture.beans | ||
|
||
class KotlinConfiguration { | ||
|
||
fun stringBean(): String { | ||
return "Hello" | ||
} | ||
|
||
suspend fun suspendingStringBean(): String { | ||
return "Hello" | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...in/org/springframework/beans/testfixture/beans/factory/generator/factory/KotlinFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.beans.testfixture.beans.factory.generator.factory | ||
|
||
class KotlinFactory { | ||
|
||
companion object { | ||
|
||
@JvmStatic | ||
fun create() = "test" | ||
|
||
@JvmStatic | ||
suspend fun suspendingCreate() = "test" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters