Refine BeanFactory extensions to avoid type erasure when possible

Closes gh-31439
This commit is contained in:
Sébastien Deleuze 2023-10-18 13:23:53 +02:00
parent a9d67878c5
commit fa57598327
2 changed files with 20 additions and 7 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2023 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.
@ -21,14 +21,17 @@ import org.springframework.core.ResolvableType
/**
* Extension for [BeanFactory.getBean] providing a `getBean<Foo>()` variant.
* This extension is not subject to type erasure and retains actual generic type arguments.
*
* @author Sebastien Deleuze
* @since 5.0
*/
inline fun <reified T : Any> BeanFactory.getBean(): T = getBean(T::class.java)
inline fun <reified T : Any> BeanFactory.getBean(): T =
getBeanProvider<T>().getObject()
/**
* Extension for [BeanFactory.getBean] providing a `getBean<Foo>("foo")` variant.
* Like the original Java method, this extension is subject to type erasure.
*
* @see BeanFactory.getBean(String, Class<T>)
* @author Sebastien Deleuze
@ -40,13 +43,14 @@ inline fun <reified T : Any> BeanFactory.getBean(name: String): T =
/**
* Extension for [BeanFactory.getBean] providing a `getBean<Foo>(arg1, arg2)` variant.
* This extension is not subject to type erasure and retains actual generic type arguments.
*
* @see BeanFactory.getBean(Class<T>, Object...)
* @author Sebastien Deleuze
* @since 5.0
*/
inline fun <reified T : Any> BeanFactory.getBean(vararg args:Any): T =
getBean(T::class.java, *args)
getBeanProvider<T>().getObject(*args)
/**
* Extension for [BeanFactory.getBeanProvider] providing a `getBeanProvider<Foo>()` variant.

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2023 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.
@ -16,10 +16,12 @@
package org.springframework.beans.factory
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import org.junit.jupiter.api.Test
import org.springframework.core.ResolvableType
import kotlin.reflect.full.createInstance
/**
* Mock object based tests for BeanFactory Kotlin extensions.
@ -32,8 +34,10 @@ class BeanFactoryExtensionsTests {
@Test
fun `getBean with reified type parameters`() {
val foo = Foo()
every { bf.getBeanProvider<Foo>(ofType<ResolvableType>()).getObject() } returns foo
bf.getBean<Foo>()
verify { bf.getBean(Foo::class.java) }
verify { bf.getBeanProvider<ObjectProvider<Foo>>(ofType<ResolvableType>()).getObject() }
}
@Test
@ -47,8 +51,10 @@ class BeanFactoryExtensionsTests {
fun `getBean with reified type parameters and varargs`() {
val arg1 = "arg1"
val arg2 = "arg2"
bf.getBean<Foo>(arg1, arg2)
verify { bf.getBean(Foo::class.java, arg1, arg2) }
val bar = Bar(arg1, arg2)
every { bf.getBeanProvider<Bar>(ofType<ResolvableType>()).getObject(arg1, arg2) } returns bar
bf.getBean<Bar>(arg1, arg2)
verify { bf.getBeanProvider<Bar>(ofType<ResolvableType>()).getObject(arg1, arg2) }
}
@Test
@ -58,4 +64,7 @@ class BeanFactoryExtensionsTests {
}
class Foo
@Suppress("UNUSED_PARAMETER")
class Bar(arg1: String, arg2: String)
}