Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. package delegation
  2.  
  3. import kotlin.properties.ReadOnlyProperty
  4. import kotlin.reflect.KProperty
  5.  
  6. class Foo: Injectable by ScopeWrapper() {
  7. val bar: Bar by inject()
  8.  
  9. }
  10.  
  11. class Bar
  12.  
  13. interface Injectable {
  14. var scope: Any?
  15. }
  16.  
  17. class ScopeWrapper: Injectable {
  18.  
  19. override var scope: Any? = null
  20.  
  21. }
  22.  
  23. fun Foo(scope: Any): Foo = Foo().apply { this.scope = scope }
  24.  
  25. val foo = Foo("jfdksjief")
  26.  
  27. //used for the `by` fields injection
  28. class InjectionDelegate<OWNER: Injectable, T>(val obj: OWNER, val clazz: Class<T>) : ReadOnlyProperty<OWNER, T> {
  29. override fun getValue(thisRef: OWNER, property: KProperty<*>): T {
  30. TODO()
  31. // obj.scope.getInstance(clazz)
  32. }
  33. }
  34.  
  35. //provides a delegate per field
  36. //I actually wonder about the byte code cost in business classes
  37. //that would use a scope as a parameter, it seems to me the cost
  38. //of so many inline function overrides for reified types can be huge
  39. inline fun <reified T> Injectable.inject(): InjectionDelegate<Injectable, T> {
  40. return InjectionDelegate(this, T::class.java)
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement