Component container add tools to specify default implementation of a component
This commit is contained in:
@@ -15,5 +15,6 @@
|
||||
<orderEntry type="library" scope="TEST" name="junit-4.12" level="project" />
|
||||
<orderEntry type="library" exported="" name="javax.inject" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="kotlin-test" level="project" />
|
||||
<orderEntry type="module" module-name="util.runtime" />
|
||||
</component>
|
||||
</module>
|
||||
@@ -42,7 +42,8 @@ fun Class<*>.getInfo(): ClassInfo {
|
||||
data class ClassInfo(
|
||||
val constructorInfo: ConstructorInfo?,
|
||||
val setterInfos: List<SetterInfo>,
|
||||
val registrations: List<Type>
|
||||
val registrations: List<Type>,
|
||||
val defaultImplementation: Class<*>?
|
||||
)
|
||||
|
||||
data class ConstructorInfo(
|
||||
@@ -56,7 +57,7 @@ data class SetterInfo(
|
||||
)
|
||||
|
||||
private fun traverseClass(c: Class<*>): ClassInfo {
|
||||
return ClassInfo(getConstructorInfo(c), getSetterInfos(c), getRegistrations(c))
|
||||
return ClassInfo(getConstructorInfo(c), getSetterInfos(c), getRegistrations(c), getDefaultImplementation(c))
|
||||
}
|
||||
|
||||
private fun getSetterInfos(c: Class<*>): List<SetterInfo> {
|
||||
@@ -99,6 +100,10 @@ private fun collectInterfacesRecursive(type: Type, result: MutableSet<Type>) {
|
||||
}
|
||||
}
|
||||
|
||||
private fun getDefaultImplementation(klass: Class<*>): Class<*>? {
|
||||
return klass.getAnnotation(DefaultImplementation::class.java)?.impl?.java
|
||||
}
|
||||
|
||||
private fun getRegistrations(klass: Class<*>): List<Type> {
|
||||
val registrations = ArrayList<Type>()
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ package org.jetbrains.kotlin.container
|
||||
|
||||
import java.lang.reflect.*
|
||||
|
||||
class InstanceComponentDescriptor(val instance: Any) : ComponentDescriptor {
|
||||
open class InstanceComponentDescriptor(val instance: Any) : ComponentDescriptor {
|
||||
|
||||
override fun getValue(): Any = instance
|
||||
override fun getRegistrations(): Iterable<Type> = instance::class.java.getInfo().registrations
|
||||
@@ -28,4 +28,8 @@ class InstanceComponentDescriptor(val instance: Any) : ComponentDescriptor {
|
||||
override fun toString(): String {
|
||||
return "Instance: ${instance::class.java.simpleName}"
|
||||
}
|
||||
}
|
||||
|
||||
class DefaultInstanceComponentDescriptor(instance: Any): InstanceComponentDescriptor(instance) {
|
||||
override fun toString() = "Default instance: ${instance.javaClass.simpleName}"
|
||||
}
|
||||
@@ -148,4 +148,10 @@ class ImplicitSingletonTypeComponentDescriptor(container: ComponentContainer, kl
|
||||
override fun toString(): String {
|
||||
return "Implicit: ${klass.simpleName}"
|
||||
}
|
||||
}
|
||||
|
||||
class DefaultSingletonTypeComponentDescriptor(container: ComponentContainer, klass: Class<*>) : SingletonTypeComponentDescriptor(container, klass) {
|
||||
override fun toString(): String {
|
||||
return "Default: ${klass.simpleName}"
|
||||
}
|
||||
}
|
||||
@@ -161,18 +161,32 @@ class ComponentStorage(val myId: String, parent: ComponentStorage?) : ValueResol
|
||||
is ParameterizedType -> type.rawType as? Class<*>
|
||||
else -> null
|
||||
}
|
||||
if (rawType == null)
|
||||
continue
|
||||
|
||||
if (!Modifier.isAbstract(rawType.modifiers) && !rawType.isPrimitive) {
|
||||
val implicitDescriptor = ImplicitSingletonTypeComponentDescriptor(context.container, rawType)
|
||||
adhocDescriptors.add(implicitDescriptor)
|
||||
collectAdhocComponents(context, implicitDescriptor, visitedTypes, adhocDescriptors)
|
||||
}
|
||||
val implicitDependency = rawType?.let { getImplicitlyDefinedDependency(context, it) } ?: continue
|
||||
|
||||
adhocDescriptors.add(implicitDependency)
|
||||
collectAdhocComponents(context, implicitDependency, visitedTypes, adhocDescriptors)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun getImplicitlyDefinedDependency(context: ComponentResolveContext, rawType: Class<*>): ComponentDescriptor? {
|
||||
if (!Modifier.isAbstract(rawType.modifiers) && !rawType.isPrimitive) {
|
||||
return ImplicitSingletonTypeComponentDescriptor(context.container, rawType)
|
||||
}
|
||||
|
||||
val defaultImplementation = rawType.getInfo().defaultImplementation
|
||||
if (defaultImplementation != null && defaultImplementation.getInfo().constructorInfo != null) {
|
||||
return DefaultSingletonTypeComponentDescriptor(context.container, defaultImplementation)
|
||||
}
|
||||
|
||||
if (defaultImplementation != null) {
|
||||
return defaultImplementation.getField("INSTANCE")?.get(null)?.let(::DefaultInstanceComponentDescriptor)
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
private fun injectProperties(instance: Any, context: ValueResolveContext) {
|
||||
val classInfo = instance::class.java.getInfo()
|
||||
|
||||
|
||||
+47
@@ -311,4 +311,51 @@ class ComponentContainerTest {
|
||||
assertTrue(a === bc1.get<B>().a)
|
||||
assertTrue(a === bc1.get<A>())
|
||||
}
|
||||
|
||||
@DefaultImplementation(impl = Impl::class)
|
||||
abstract class I
|
||||
|
||||
class Impl : I()
|
||||
|
||||
class Impl2: I()
|
||||
|
||||
class Use(val i: I)
|
||||
|
||||
@Test
|
||||
fun default_implementation() {
|
||||
class Use(val i: I)
|
||||
|
||||
val u = composeContainer("a") {
|
||||
useImpl<Use>()
|
||||
}.get<Use>()
|
||||
|
||||
assertTrue(u.i is Impl)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun non_default_implementation() {
|
||||
val ac = composeContainer("a") {
|
||||
useImpl<Impl2>()
|
||||
useImpl<Use>()
|
||||
}
|
||||
|
||||
val u = ac.get<Use>()
|
||||
assertTrue(u.i is Impl2)
|
||||
}
|
||||
|
||||
@DefaultImplementation(impl = A::class)
|
||||
interface S
|
||||
|
||||
object A: S
|
||||
|
||||
class UseS(val s: S)
|
||||
|
||||
@Test
|
||||
fun test_default_object() {
|
||||
val useS = composeContainer("s") {
|
||||
useImpl<UseS>()
|
||||
}.get<UseS>()
|
||||
|
||||
assertTrue(useS.s === A)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* Copyright 2010-2017 JetBrains s.r.o.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* http://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.jetbrains.kotlin.container
|
||||
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
/*Use to assist injection to provide a default implementation for a certain component and reduce boilerplate in injector code.
|
||||
* Argument class must be a non-abstract component class or a kotlin object implementing target interface.
|
||||
* Avoid using when there is no clear 'default' behaviour for a component.
|
||||
* */
|
||||
annotation class DefaultImplementation(val impl: KClass<*>)
|
||||
Reference in New Issue
Block a user