Skip to content

Commit

Permalink
Do not allow variance
Browse files Browse the repository at this point in the history
  • Loading branch information
berberman committed Mar 9, 2022
1 parent 82038de commit 79893b7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import org.mechdancer.dependency.*
import kotlin.reflect.KClass
import kotlin.reflect.KProperty1
import kotlin.reflect.full.declaredMemberProperties
import kotlin.reflect.full.isSupertypeOf
import kotlin.reflect.full.starProjectedType
import kotlin.reflect.jvm.javaField
import kotlin.reflect.jvm.jvmErasure

Expand Down Expand Up @@ -38,9 +36,8 @@ class AnnotatedInjector<T : Any>(private val dependent: T, type: KClass<T>) : Sc
else it.returnType.jvmErasure as KClass<out Component>
) { component ->
(if (needsUnwrap)
component is UniqueComponentWrapper<*> && it.returnType.isSupertypeOf(
component.type.starProjectedType
)
(component is UniqueComponentWrapper<*> && it.returnType.jvmErasure ==
component.type)
else
true) && component.toPredicate(name)
}.also { dep ->
Expand Down Expand Up @@ -72,9 +69,8 @@ class AnnotatedInjector<T : Any>(private val dependent: T, type: KClass<T>) : Sc
else it.returnType.jvmErasure as KClass<out Component>
) { component ->
(if (needsUnwrap)
component is UniqueComponentWrapper<*> && it.returnType.isSupertypeOf(
component.type.starProjectedType
)
component is UniqueComponentWrapper<*> && it.returnType.jvmErasure ==
component.type
else
true) && component.toPredicate(name)
}.also { dep ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import org.mechdancer.dependency.INamedComponent
import org.mechdancer.dependency.IUniqueComponent
import org.mechdancer.dependency.UniqueComponentWrapper
import kotlin.reflect.KClass
import kotlin.reflect.full.isSubclassOf

/**
* Create a interface delegate of [ManagedHandler]
Expand Down Expand Up @@ -168,7 +167,7 @@ inline fun <reified C : INamedComponent<C>>
* @return a property delegate
*/
inline fun <reified C : UniqueComponentWrapper<T>, reified T>
DependencyManager.mustWrapped() = must<C, T>({ it.type.isSubclassOf(T::class) }) { it.wrapped }
DependencyManager.mustWrapped() = must<C, T>({ it.type == T::class }) { it.wrapped }

/**
* Declare a weak [UniqueComponentWrapper] dependency with type [C] that wrappers type [T],
Expand All @@ -177,9 +176,9 @@ inline fun <reified C : UniqueComponentWrapper<T>, reified T>
* @return a property delegate
*/
inline fun <reified C : UniqueComponentWrapper<T>, reified T>
DependencyManager.maybeWrapped() = must<C, T>({ it.type.isSubclassOf(T::class) }) { it.wrapped }
DependencyManager.maybeWrapped() = must<C, T>({ it.type == T::class }) { it.wrapped }

/**
* Wrap [this] to a unique component
*/
fun Any.wrapToUniqueComponent() = UniqueComponentWrapper(this)
inline fun <reified T : Any> T.wrapToUniqueComponent() = UniqueComponentWrapper(this)
19 changes: 18 additions & 1 deletion src/test/kotlin/org/mechdancer/dependency/TestWrapped.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ import org.mechdancer.dependency.manager.managedHandler
import org.mechdancer.dependency.manager.mustWrapped
import org.mechdancer.dependency.manager.wrapToUniqueComponent

class J
open class Common

class J : Common()

class K : UniqueComponent<K>(), Dependent, ManagedHandler by managedHandler() {
val j: J by manager.mustWrapped()
val int: Int by manager.mustWrapped()
val m: M by manager.mustWrapped()
val common: Common by manager.mustWrapped()
}

class L : UniqueComponent<L>(), Dependent {
Expand All @@ -28,25 +32,38 @@ class L : UniqueComponent<L>(), Dependent {
@Must
var int: Int? = null

@Unwrap
@Must
var common: Common? = null

override fun handle(scopeEvent: ScopeEvent) = injector.handle(scopeEvent)
}

class M : Common()

class TestWrapped {
@Test
fun test() {
val j = J()
val k = K()
val int = 233
val l = L()
val common = Common()
val m = M()
scope {
this += j.wrapToUniqueComponent()
this += int.wrapToUniqueComponent()
this += k
this += l
this += common.wrapToUniqueComponent()
this += m.wrapToUniqueComponent()
}
Assert.assertEquals(j, k.j)
Assert.assertEquals(int, k.int)
Assert.assertEquals(j, l.j)
Assert.assertEquals(int, l.int)
Assert.assertEquals(m, k.m)
Assert.assertEquals(common, l.common)
Assert.assertEquals(common, k.common)
}
}

0 comments on commit 79893b7

Please sign in to comment.