diff --git a/ChangeLog.md b/ChangeLog.md new file mode 100644 index 0000000..8d331a6 --- /dev/null +++ b/ChangeLog.md @@ -0,0 +1,17 @@ +## 0.1.2 + +* Add `UniqueComponentWrapper` that wraps an object unique-like component + +* Create interfaces `IUniqueComponent` and `INamedComonent` with default implementation, in case the + class supposed to be a unique component or named component already has its super class + +* Rework generic finding algorithm on `UniqueComponent` + +* Fix the bug that property delegate created by `annotatedInjector()` + instantiated `AnnotatedInjector` many times + +* Support removing components from scope + +## 0.1.1 + +* Initial release \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index 1992056..7d28fce 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -5,7 +5,7 @@ plugins { } group = "org.mechdancer" -version = "0.1.1" +version = "0.1.2" repositories { mavenCentral() } dependencies { diff --git a/src/main/kotlin/org/mechdancer/dependency/UniqueComponentWrapper.kt b/src/main/kotlin/org/mechdancer/dependency/UniqueComponentWrapper.kt index c1194e0..4107902 100644 --- a/src/main/kotlin/org/mechdancer/dependency/UniqueComponentWrapper.kt +++ b/src/main/kotlin/org/mechdancer/dependency/UniqueComponentWrapper.kt @@ -3,6 +3,16 @@ package org.mechdancer.dependency import kotlin.reflect.KClass import kotlin.reflect.safeCast +/** + * Wraps an object to a component which has a similar implementation to [UniqueComponent] + * + * This class is useful in the case that we can't modify some classes to make them components + * + * Invariance: the implementation in dependency manager and annotation injector enforce + * the invariance on the wrapped type, which means there is no chance to cast an instance + * to its supertype, and you should declare the dependency with the type exactly the same as you + * wrapped and set up in scope. + */ class UniqueComponentWrapper @PublishedApi internal constructor( val type: KClass<*>, val wrapped: T diff --git a/src/main/kotlin/org/mechdancer/dependency/annotated/AnnotatedInjector.kt b/src/main/kotlin/org/mechdancer/dependency/annotated/AnnotatedInjector.kt index c63146f..521ab15 100644 --- a/src/main/kotlin/org/mechdancer/dependency/annotated/AnnotatedInjector.kt +++ b/src/main/kotlin/org/mechdancer/dependency/annotated/AnnotatedInjector.kt @@ -36,6 +36,7 @@ class AnnotatedInjector(private val dependent: T, type: KClass) : Sc else it.returnType.jvmErasure as KClass ) { component -> (if (needsUnwrap) + // Invariance: See the note on [UniqueComponentWrapper] (component is UniqueComponentWrapper<*> && it.returnType.jvmErasure == component.type) else @@ -69,6 +70,7 @@ class AnnotatedInjector(private val dependent: T, type: KClass) : Sc else it.returnType.jvmErasure as KClass ) { component -> (if (needsUnwrap) + // Invariance: See the note on [UniqueComponentWrapper] component is UniqueComponentWrapper<*> && it.returnType.jvmErasure == component.type else diff --git a/src/main/kotlin/org/mechdancer/dependency/manager/Functions.kt b/src/main/kotlin/org/mechdancer/dependency/manager/Functions.kt index 2ab5d47..c705faf 100644 --- a/src/main/kotlin/org/mechdancer/dependency/manager/Functions.kt +++ b/src/main/kotlin/org/mechdancer/dependency/manager/Functions.kt @@ -164,6 +164,8 @@ inline fun > * Declare a strict [UniqueComponentWrapper] dependency with type [C] that wrappers type [T], * creating a delegate that obtains its value * + * Invariance: See the note on [UniqueComponentWrapper] + * * @return a property delegate */ inline fun , reified T> @@ -173,6 +175,8 @@ inline fun , reified T> * Declare a weak [UniqueComponentWrapper] dependency with type [C] that wrappers type [T], * creating a delegate that obtains its value * + * Invariance: See the note on [UniqueComponentWrapper] + * * @return a property delegate */ inline fun , reified T>