Remove object wrappers in Kotlin extensions
This commit also improve significantly Kotlin extensions documentation. Issue: SPR-15127
This commit is contained in:
@@ -1,14 +0,0 @@
|
||||
package org.springframework.context.annotation
|
||||
|
||||
/**
|
||||
* Extension for [AnnotationConfigApplicationContext] providing
|
||||
* `AnnotationConfigApplicationContext { }` style initialization.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @since 5.0
|
||||
*/
|
||||
object AnnotationConfigApplicationContextExtension {
|
||||
|
||||
fun AnnotationConfigApplicationContext(configure: AnnotationConfigApplicationContext.()->Unit) =
|
||||
AnnotationConfigApplicationContext().apply(configure)
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package org.springframework.context.annotation
|
||||
|
||||
/**
|
||||
* Extension for [AnnotationConfigApplicationContext] allowing
|
||||
* `AnnotationConfigApplicationContext { ... }` style initialization.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @since 5.0
|
||||
*/
|
||||
fun AnnotationConfigApplicationContext(configure: AnnotationConfigApplicationContext.()->Unit) =
|
||||
AnnotationConfigApplicationContext().apply(configure)
|
||||
@@ -1,65 +0,0 @@
|
||||
package org.springframework.context.support
|
||||
|
||||
import org.springframework.beans.factory.config.BeanDefinitionCustomizer
|
||||
import org.springframework.context.ApplicationContext
|
||||
import java.util.function.Supplier
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
/**
|
||||
* Extension for [GenericApplicationContext] providing [KClass] based API and
|
||||
* avoiding specifying a class parameter for the [Supplier] based variant thanks to
|
||||
* Kotlin reified type parameters.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @since 5.0
|
||||
*/
|
||||
object GenericApplicationContextExtension {
|
||||
|
||||
/**
|
||||
* @see GenericApplicationContext.registerBean(Class<T>, BeanDefinitionCustomizer...)
|
||||
*/
|
||||
fun <T : Any> GenericApplicationContext.registerBean(beanClass: KClass<T>,
|
||||
vararg customizers: BeanDefinitionCustomizer) {
|
||||
registerBean(beanClass.java, *customizers)
|
||||
}
|
||||
|
||||
/**
|
||||
* @see GenericApplicationContext.registerBean(Class<T>, BeanDefinitionCustomizer...)
|
||||
*/
|
||||
inline fun <reified T : Any> GenericApplicationContext.registerBean(vararg customizers: BeanDefinitionCustomizer) {
|
||||
registerBean(T::class.java, *customizers)
|
||||
}
|
||||
|
||||
/**
|
||||
* @see GenericApplicationContext.registerBean(String, Class<T>, BeanDefinitionCustomizer...)
|
||||
*/
|
||||
fun <T : Any> GenericApplicationContext.registerBean(beanName: String, beanClass: KClass<T>,
|
||||
vararg customizers: BeanDefinitionCustomizer) {
|
||||
registerBean(beanName, beanClass.java, *customizers)
|
||||
}
|
||||
|
||||
/**
|
||||
* @see GenericApplicationContext.registerBean(String, Class<T>, BeanDefinitionCustomizer...)
|
||||
*/
|
||||
inline fun <reified T : Any> GenericApplicationContext.registerBean(beanName: String, vararg customizers: BeanDefinitionCustomizer) {
|
||||
registerBean(beanName, T::class.java, *customizers)
|
||||
}
|
||||
|
||||
/**
|
||||
* @see GenericApplicationContext.registerBean(Class<T>, Supplier<T>, BeanDefinitionCustomizer...)
|
||||
*/
|
||||
inline fun <reified T : Any> GenericApplicationContext.registerBean(
|
||||
vararg customizers: BeanDefinitionCustomizer, crossinline function: (ApplicationContext) -> T) {
|
||||
registerBean(T::class.java, Supplier { function.invoke(this) }, *customizers)
|
||||
}
|
||||
|
||||
/**
|
||||
* @see GenericApplicationContext.registerBean(String, Class<T>, Supplier<T>, BeanDefinitionCustomizer...)
|
||||
*/
|
||||
inline fun <reified T : Any> GenericApplicationContext.registerBean(name: String,
|
||||
vararg customizers: BeanDefinitionCustomizer, crossinline function: (ApplicationContext) -> T) {
|
||||
registerBean(name, T::class.java, Supplier { function.invoke(this) }, *customizers)
|
||||
}
|
||||
|
||||
fun GenericApplicationContext(configure: GenericApplicationContext.()->Unit) = GenericApplicationContext().apply(configure)
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package org.springframework.context.support
|
||||
|
||||
import org.springframework.beans.factory.config.BeanDefinitionCustomizer
|
||||
import org.springframework.context.ApplicationContext
|
||||
import java.util.function.Supplier
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
|
||||
/**
|
||||
* Extension for [GenericApplicationContext.registerBean] providing a [KClass] based variant.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @since 5.0
|
||||
*/
|
||||
fun <T : Any> GenericApplicationContext.registerBean(beanClass: KClass<T>,
|
||||
vararg customizers: BeanDefinitionCustomizer) {
|
||||
registerBean(beanClass.java, *customizers)
|
||||
}
|
||||
|
||||
/**
|
||||
* Extension for [GenericApplicationContext.registerBean] providing a `registerBean<Foo>()` variant.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @since 5.0
|
||||
*/
|
||||
inline fun <reified T : Any> GenericApplicationContext.registerBean(vararg customizers: BeanDefinitionCustomizer) {
|
||||
registerBean(T::class.java, *customizers)
|
||||
}
|
||||
|
||||
/**
|
||||
* Extension for [GenericApplicationContext.registerBean] providing a [KClass] based variant.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @since 5.0
|
||||
*/
|
||||
fun <T : Any> GenericApplicationContext.registerBean(beanName: String, beanClass: KClass<T>,
|
||||
vararg customizers: BeanDefinitionCustomizer) {
|
||||
registerBean(beanName, beanClass.java, *customizers)
|
||||
}
|
||||
|
||||
/**
|
||||
* Extension for [GenericApplicationContext.registerBean] providing a `registerBean<Foo>("foo")` variant.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @since 5.0
|
||||
*/
|
||||
inline fun <reified T : Any> GenericApplicationContext.registerBean(beanName: String, vararg customizers: BeanDefinitionCustomizer) {
|
||||
registerBean(beanName, T::class.java, *customizers)
|
||||
}
|
||||
|
||||
/**
|
||||
* Extension for [GenericApplicationContext.registerBean] providing a `registerBean { Foo() }` variant.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @since 5.0
|
||||
*/
|
||||
inline fun <reified T : Any> GenericApplicationContext.registerBean(
|
||||
vararg customizers: BeanDefinitionCustomizer, crossinline function: (ApplicationContext) -> T) {
|
||||
registerBean(T::class.java, Supplier { function.invoke(this) }, *customizers)
|
||||
}
|
||||
|
||||
/**
|
||||
* Extension for [GenericApplicationContext.registerBean] providing a `registerBean("foo") { Foo() }` variant.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @since 5.0
|
||||
*/
|
||||
inline fun <reified T : Any> GenericApplicationContext.registerBean(name: String,
|
||||
vararg customizers: BeanDefinitionCustomizer, crossinline function: (ApplicationContext) -> T) {
|
||||
registerBean(name, T::class.java, Supplier { function.invoke(this) }, *customizers)
|
||||
}
|
||||
|
||||
/**
|
||||
* Extension for [GenericApplicationContext] allowing `GenericApplicationContext { ... }` style initialization.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @since 5.0
|
||||
*/
|
||||
fun GenericApplicationContext(configure: GenericApplicationContext.()->Unit) = GenericApplicationContext().apply(configure)
|
||||
|
||||
@@ -17,22 +17,15 @@
|
||||
package org.springframework.ui
|
||||
|
||||
/**
|
||||
* Extension for [Model] providing idiomatic Kotlin API
|
||||
* Extension for [Model.addAttribute] providing Array like setter.
|
||||
*
|
||||
* ```kotlin
|
||||
* model["firstName"] = "Mario"
|
||||
* ```
|
||||
*
|
||||
* @author Mario Arias
|
||||
* @since 5.0
|
||||
*/
|
||||
object ModelExtension {
|
||||
|
||||
/** Array like setter for [Model]
|
||||
*
|
||||
* ```kotlin
|
||||
* model["firstName"] = "Mario"
|
||||
* ```
|
||||
*
|
||||
* @see Model.addAttribute
|
||||
*/
|
||||
operator fun Model.set(attributeName: String, attributeValue: Any) {
|
||||
this.addAttribute(attributeName, attributeValue)
|
||||
}
|
||||
operator fun Model.set(attributeName: String, attributeValue: Any) {
|
||||
this.addAttribute(attributeName, attributeValue)
|
||||
}
|
||||
@@ -17,22 +17,16 @@
|
||||
package org.springframework.ui
|
||||
|
||||
/**
|
||||
* Extension for [ModelMap] providing idiomatic Kotlin API
|
||||
*
|
||||
* Extension for [ModelMap] providing Array like setter.
|
||||
*
|
||||
* ```kotlin
|
||||
* model["firstName"] = "Mario"
|
||||
* ```
|
||||
*
|
||||
* @author Mario Arias
|
||||
* @since 5.0
|
||||
*/
|
||||
object ModelMapExtension {
|
||||
|
||||
/** Array like setter for [ModelMap]
|
||||
*
|
||||
* ```kotlin
|
||||
* model["firstName"] = "Mario"
|
||||
* ```
|
||||
*
|
||||
* @see ModelMap.addAttribute
|
||||
*/
|
||||
operator fun ModelMap.set(attributeName: String, attributeValue: Any) {
|
||||
this.addAttribute(attributeName, attributeValue)
|
||||
}
|
||||
operator fun ModelMap.set(attributeName: String, attributeValue: Any) {
|
||||
this.addAttribute(attributeName, attributeValue)
|
||||
}
|
||||
Reference in New Issue
Block a user