io.micronaut.kotlin.context.BeanDefinitionRegistryExtensions.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of micronaut-kotlin-extension-functions Show documentation
Show all versions of micronaut-kotlin-extension-functions Show documentation
Includes extensions to Micronaut for Kotlin
/*
* Copyright 2017-2020 original 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 io.micronaut.kotlin.context
import io.micronaut.context.BeanDefinitionRegistry
import io.micronaut.context.BeanRegistration
import io.micronaut.inject.BeanDefinition
import io.micronaut.kotlin.inject.qualifierByStereotype
import io.micronaut.inject.qualifiers.Qualifiers
import kotlin.reflect.KClass
/**
* Extension for [BeanDefinitionRegistry.getBeanDefinition] providing a `getStereotypedBeanDefinition()` variant.
*
* @param T The bean type
* @param Q The stereotype type
* @return The [BeanDefinition]
* @author Alejandro Gomez
* @since 1.0.0
*/
inline fun BeanDefinitionRegistry.getStereotypedBeanDefinition(): BeanDefinition =
getBeanDefinition(T::class.java, qualifierByStereotype())
/**
* Extension for [BeanDefinitionRegistry.containsBean] providing a `containsBean()` variant.
*
* @param T The bean type
* @return True if contained
* @author Alejandro Gomez
* @since 1.0.0
*/
inline fun BeanDefinitionRegistry.containsBean() = containsBean(T::class.java)
/**
* Extension for [BeanDefinitionRegistry.containsBean] providing a `Foo::class in registry` variant.
*
* @param t The bean type
* @return True if contained
* @author Alejandro Gomez
* @since 1.0.0
*/
operator fun BeanDefinitionRegistry.contains(t: KClass) = containsBean(t.java)
/**
* Extension for [BeanDefinitionRegistry.containsBean] providing a `containsStereotypedBean()` variant.
*
* @param T The bean type
* @param Q The stereotype type
* @return True if contained
* @author Alejandro Gomez
* @since 1.0.0
*/
inline fun BeanDefinitionRegistry.containsStereotypedBean() =
containsBean(T::class.java, qualifierByStereotype())
/**
* Extension for [BeanDefinitionRegistry.containsBean] providing a `containsStereotypedBean()` variant.
*
* @param T The bean type
* @param name The stereotype name
* @return True if contained
* @author James Kleeh
* @since 3.0.0
*/
inline fun BeanDefinitionRegistry.containsStereotypedBean(name: String) =
containsBean(T::class.java, qualifierByStereotype(name))
/**
* Extension for [BeanDefinitionRegistry.containsBean] providing a `(Foo::class to Prototype::class) in registry` variant.
*
* @param t The pair containing the bean type and the stereotype type
* @return True if contained
* @author Alejandro Gomez
* @since 1.0.0
*/
inline operator fun BeanDefinitionRegistry.contains(t: Pair, KClass>) =
containsBean(t.first.java, Qualifiers.byStereotype(t.second.java))
/**
* Extension for [BeanDefinitionRegistry.findBeanDefinition] providing a `findStereotypedBeanDefinition()` variant.
*
* @param T The bean type
* @param Q The stereotype type
* @return The [BeanDefinition] or null if not present
* @author Alejandro Gomez
* @since 1.0.0
*/
inline fun BeanDefinitionRegistry.findStereotypedBeanDefinition(): BeanDefinition? =
findBeanDefinition(T::class.java, qualifierByStereotype()).orElse(null)
/**
* Extension for [BeanDefinitionRegistry.getBeanDefinitions] providing a `getBeanDefinitions()` variant.
*
* @param T The bean type
* @return A [Collection] of the [BeanDefinition]s
* @author Alejandro Gomez
* @since 1.0.0
*/
inline fun BeanDefinitionRegistry.getBeanDefinitions(): Collection> = getBeanDefinitions(T::class.java)
/**
* Extension for [BeanDefinitionRegistry.getBeanDefinitions] providing a `getStereotypedBeanDefinitions()` variant.
*
* @param Q The stereotype type
* @return A [Collection] of the [BeanDefinition]s
* @author Alejandro Gomez
* @since 1.0.0
*/
inline fun BeanDefinitionRegistry.getStereotypedBeanDefinitions(): Collection> =
getBeanDefinitions(qualifierByStereotype())
/**
* Extension for [BeanDefinitionRegistry.getBeanRegistrations] providing a `getBeanRegistrations()` variant.
*
* @param T The bean type
* @return A [Collection] of the [BeanRegistration]s
* @author Alejandro Gomez
* @since 1.0.0
*/
inline fun BeanDefinitionRegistry.getBeanRegistrations(): Collection> = getBeanRegistrations(T::class.java)
/**
* Extension for [BeanDefinitionRegistry.getActiveBeanRegistrations] providing a `getActiveBeanRegistrations()` variant.
*
* @param T The bean type
* @return A [Collection] of the [BeanRegistration]s
* @author Alejandro Gomez
* @since 0.0.6
*/
inline fun BeanDefinitionRegistry.getActiveBeanRegistrations(): Collection> =
getActiveBeanRegistrations(T::class.java)
/**
* Extension for [BeanDefinitionRegistry.getActiveBeanRegistrations] providing a `getStereotypedActiveBeanRegistrations()` variant.
*
* @param Q The stereotype type
* @return A [Collection] of the [BeanRegistration]s
* @author Alejandro Gomez
* @since 0.0.6
*/
inline fun BeanDefinitionRegistry.getStereotypedActiveBeanRegistrations(): Collection> =
getActiveBeanRegistrations(qualifierByStereotype())
/**
* Extension for [BeanDefinitionRegistry.findProxyTargetBeanDefinition] providing a `findProxyTargetBeanDefinition()` variant.
*
* @param T The bean type
* @param Q The stereotype type
* @return The original [BeanDefinition] or null if not present
* @author Alejandro Gomez
* @since 1.0.0
*/
inline fun BeanDefinitionRegistry.findProxyTargetBeanDefinition(): BeanDefinition? =
findProxyTargetBeanDefinition(T::class.java, qualifierByStereotype()).orElse(null)
/**
* Extension for [BeanDefinitionRegistry.findProxyBeanDefinition] providing a `findProxyBeanDefinition()` variant.
*
* @param T The bean type
* @param Q The stereotype type
* @return The original [BeanDefinition] or null if not present
* @author Alejandro Gomez
* @since 1.0.0
*/
inline fun BeanDefinitionRegistry.findProxyBeanDefinition(): BeanDefinition? =
findProxyBeanDefinition(T::class.java, qualifierByStereotype()).orElse(null)
/**
* Extension for [BeanDefinitionRegistry.getProxyTargetBeanDefinition] providing a `getProxyTargetBeanDefinition()` variant.
*
* @param T The bean type
* @param Q The stereotype type
* @return The original [BeanDefinition]
* @author Alejandro Gomez
* @since 1.0.0
*/
inline fun BeanDefinitionRegistry.getProxyTargetBeanDefinition(): BeanDefinition =
getProxyTargetBeanDefinition(T::class.java, qualifierByStereotype())
/**
* Extension for [BeanDefinitionRegistry.getBeanDefinition] providing a `getBeanDefinition()` variant.
*
* @param T The bean type
* @return The [BeanDefinition]
* @author Alejandro Gomez
* @since 1.0.0
*/
inline fun BeanDefinitionRegistry.getBeanDefinition(): BeanDefinition = getBeanDefinition(T::class.java)
/**
* Extension for [BeanDefinitionRegistry.findBeanDefinition] providing a `findBeanDefinition()` variant.
*
* @param T The bean type
* @return The [BeanDefinition] or null if not present
* @author Alejandro Gomez
* @since 1.0.0
*/
inline fun BeanDefinitionRegistry.findBeanDefinition(): BeanDefinition? = findBeanDefinition(T::class.java).orElse(null)
/**
* Extension for [BeanDefinitionRegistry.registerSingleton] providing a `registerStereotypedSingleton(singleton, true)` variant.
*
* @param T The bean type
* @param Q The stereotype type
* @param singleton The singleton bean
* @param inject Whether the singleton should be injected
* @return The [BeanDefinitionRegistry]
* @author Alejandro Gomez
* @since 1.0.0
*/
inline fun BeanDefinitionRegistry.registerStereotypedSingleton(singleton: T, inject: Boolean): BeanDefinitionRegistry =
registerSingleton(T::class.java, singleton, qualifierByStereotype(), inject)
/**
* Extension for [BeanDefinitionRegistry.registerSingleton] providing a `registerStereotypedSingleton(singleton, true)` variant.
*
* @param T The bean type
* @param singleton The singleton bean
* @param name The name of the stereotype
* @param inject Whether the singleton should be injected
* @return The [BeanDefinitionRegistry]
* @author James Kleeh
* @since 3.0.0
*/
inline fun BeanDefinitionRegistry.registerStereotypedSingleton(singleton: T, name: String, inject: Boolean): BeanDefinitionRegistry =
registerSingleton(T::class.java, singleton, qualifierByStereotype(name), inject)
/**
* Extension for [BeanDefinitionRegistry.registerSingleton] providing a `registerStereotypedSingleton(singleton)` variant.
*
* @param T The bean type
* @param Q The stereotype type
* @param singleton The singleton bean
* @return The [BeanDefinitionRegistry]
* @author Alejandro Gomez
* @since 1.0.0
*/
inline fun BeanDefinitionRegistry.registerStereotypedSingleton(singleton: T): BeanDefinitionRegistry =
registerSingleton(T::class.java, singleton, qualifierByStereotype())
/**
* Extension for [BeanDefinitionRegistry.registerSingleton] providing a `registerStereotypedSingleton(singleton)` variant.
*
* @param T The bean type
* @param singleton The singleton bean
* @param name The stereotype name
* @return The [BeanDefinitionRegistry]
* @author James Kleeh
* @since 3.0.0
*/
inline fun BeanDefinitionRegistry.registerStereotypedSingleton(singleton: T, name: String): BeanDefinitionRegistry =
registerSingleton(T::class.java, singleton, qualifierByStereotype(name))
/**
* Extension for [BeanDefinitionRegistry.registerSingleton] providing a `registerNotStereotypedSingleton(singleton)` variant.
*
* @param T The bean type
* @param singleton The singleton bean
* @return The [BeanDefinitionRegistry]
* @author Alejandro Gomez
* @since 1.0.0
*/
inline fun BeanDefinitionRegistry.registerNotStereotypedSingleton(singleton: T): BeanDefinitionRegistry =
registerSingleton(T::class.java, singleton)