This repository has been archived by the owner on Jul 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
390 additions
and
327 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
2015-08-10 v1.1.0 | ||
|
||
Seperate API from core JAR. Core depends on API. a lot of small changes to structure for having independent scopes for injection, and cleaning. | ||
Sorry for the breaks, but they will be tiny little changes to clean up. | ||
|
||
API Changes: | ||
* new property delegates that require scope to be passed in. | ||
|
||
Core Changes: | ||
* delegates by default point a the top level scope unless you use the scope-specific version. | ||
* changed Injekt from object singleton that contained implementation to an var of type `InjektScope` (no API change, code should compile as-is but does need recompile) | ||
* [BREAKING CHANGE] changed default registry/factory package to `uy.kohesive.injekt.registry.default` although unlikely referenced from user code. | ||
* [BREAKING CHANGE] renamed `InjektInstanceFactory` to `InjektFactory` | ||
* [BREAKING CHANGE] renamed `Injekt.Property` with delegates removed, use other Delegates.* | ||
* [BREAKING CHANGE] `InkektRegistrar` is changed to only be a combination of two interfaces, `InjektRegistry` and `InjektFactory` and nothing else. | ||
* [BREAKING CHANGE] changed `InjektMain` / `InjektScopedMain` to also be a module with same `registerInjectables` method | ||
* [BREAKING CHANGE] changed `exportInjektables` to `registerInjectables` | ||
* [BREAKING CHANGE] changed other words in method names that used "injekt" to "inject" to be more natural, old versions deprecated to be removed in 1 release | ||
* Introduced `InjektScope` which allows different parts of the app to have different injection registries | ||
* Introduced `InjektScopedMain` which is like `InjektMain` but for a specified scope instead of the global scope. | ||
|
||
|
||
|
||
===== | ||
|
||
2015-08-04 v1.0.0 released to Maven Central | ||
|
||
First version: | ||
uy.kohesive.injekt:injekt-core:1.0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package uy.kohesive.injekt | ||
|
||
class InjektionException(msg: String) : RuntimeException(msg) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package uy.kohesive.injekt | ||
|
||
public interface InjektFactory { | ||
public fun <R> getInstance(forClass: Class<R>): R | ||
public fun <R, K> getKeyedInstance(forClass: Class<R>, key: K): R | ||
public fun <R> getLogger(expectedLoggerClass: Class<R>, name: String): R | ||
public fun <R> getLogger(expectedLoggerClass: Class<R>, forClass: Class<*>): R | ||
|
||
public final inline fun <reified T> get(): T = getInstance(javaClass<T>()) | ||
public final inline fun <reified T> get(key: Any): T = getKeyedInstance(javaClass<T>(), key) | ||
|
||
public final fun <T> get(forClass: Class<T>): T = getInstance(forClass) | ||
public final fun <T> get(forClass: Class<T>, key: Any): T = getKeyedInstance(forClass, key) | ||
|
||
public final inline fun <reified T> logger(byClass: Class<*>): T = getLogger(javaClass<T>(), byClass) | ||
public final inline fun <reified T> logger(byName: String): T = getLogger(javaClass<T>(), byName) | ||
public final inline fun <reified T> logger(byObject: Any): T = getLogger(javaClass<T>(), byObject.javaClass) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package uy.kohesive.injekt | ||
|
||
|
||
/** | ||
* A startup module that registers and uses singletons/object factories from a specific scope | ||
*/ | ||
public abstract class InjektScopedMain(public val scope: InjektScope) : InjektModule { | ||
init { | ||
scope.registrar.registerInjectables() | ||
} | ||
} | ||
|
||
/** | ||
* A package of injectable items that can be included into a scope of someone else | ||
*/ | ||
public interface InjektModule { | ||
internal fun registerWith(intoModule: InjektRegistrar) { | ||
intoModule.registerInjectables() | ||
} | ||
|
||
fun InjektRegistrar.registerInjectables() | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package uy.kohesive.injekt | ||
|
||
interface InjektRegistrar: InjektRegistry, InjektFactory { | ||
public fun importModule(submodule: InjektModule) { | ||
submodule.registerWith(this) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package uy.kohesive.injekt | ||
|
||
import kotlin.reflect.KClass | ||
|
||
public interface InjektRegistry { | ||
public fun <T : Any> addSingleton(forClass: Class<T>, singleInstance: T) | ||
public fun <R> addSingletonFactory(forClass: Class<R>, factoryCalledOnce: () -> R) | ||
public fun <R> addFactory(forClass: Class<R>, factoryCalledEveryTime: () -> R) | ||
public fun <R> addPerThreadFactory(forClass: Class<R>, factoryCalledOncePerThread: () -> R) | ||
public fun <R, K> addPerKeyFactory(forClass: Class<R>, forKeyClass: Class<K>, factoryCalledPerKey: (K) -> R) | ||
public fun <R, K> addPerThreadPerKeyFactory(forClass: Class<R>, forKeyClass: Class<K>, factoryCalledPerKeyPerThread: (K) -> R) | ||
public fun <R : Any> addLoggerFactory(forLoggerClass: Class<R>, factoryByName: (String) -> R, factoryByClass: (Class<*>) -> R) | ||
public fun <T> alias(existingRegisteredClass: Class<T>, otherClassesThatAreSame: List<Class<*>>) | ||
public fun <T> hasFactory(forClass: Class<T>): Boolean | ||
|
||
|
||
public final inline fun <reified T : Any> T.registerAsSingleton() { | ||
addSingleton(this) | ||
} | ||
|
||
public final inline fun <reified R> KClass<R>.registerSingletonFactory(@noinline factoryCalledOnce: () -> R) { | ||
addSingletonFactory(factoryCalledOnce) | ||
} | ||
|
||
public final inline fun <reified R> KClass<R>.registerFactory(@noinline factoryCalledEveryTime: () -> R) { | ||
addFactory(factoryCalledEveryTime) | ||
} | ||
|
||
public final inline fun <reified R> KClass<R>.registerPerThreadFactory(@noinline factoryCalledOncePerThread: () -> R) { | ||
addPerThreadFactory(factoryCalledOncePerThread) | ||
} | ||
|
||
public final inline fun <reified R, reified K> KClass<R>.registerPerKeyFactory(@noinline factoryCalledPerKey: (K) -> R) { | ||
addPerKeyFactory(factoryCalledPerKey) | ||
} | ||
|
||
public final inline fun <reified R, reified K> KClass<R>.registerPerThreadPerKeyFactory(@noinline factoryCalledPerKeyPerThread: (K) -> R) { | ||
addPerThreadPerKeyFactory(factoryCalledPerKeyPerThread) | ||
} | ||
|
||
public final inline fun <reified R> KClass<R>.registerLoggerFactory(@noinline factoryByName: (String) -> R, @noinline factoryByClass: (Class<*>) -> R) { | ||
addLoggerFactory(factoryByName, factoryByClass) | ||
} | ||
|
||
public final inline fun <reified T : Any> T.aliasOthersToMe(classes: List<Class<*>>) { | ||
alias(javaClass<T>(), classes) | ||
} | ||
|
||
public final inline fun <reified T : Any> addSingleton(singleInstance: T) { | ||
addSingleton(javaClass<T>(), singleInstance) | ||
} | ||
|
||
public final inline fun <reified R> addSingletonFactory(@noinline factoryCalledOnce: () -> R) { | ||
addSingletonFactory(javaClass<R>(), factoryCalledOnce) | ||
} | ||
|
||
public final inline fun <reified R> addFactory(@noinline factoryCalledEveryTime: () -> R) { | ||
addFactory(javaClass<R>(), factoryCalledEveryTime) | ||
} | ||
|
||
public final inline fun <reified R> addPerThreadFactory(@noinline factoryCalledOncePerThread: () -> R) { | ||
addPerThreadFactory(javaClass<R>(), factoryCalledOncePerThread) | ||
} | ||
|
||
public final inline fun <reified R, reified K> addPerKeyFactory(@noinline factoryCalledPerKey: (K) -> R) { | ||
addPerKeyFactory(javaClass<R>(), javaClass<K>(), factoryCalledPerKey) | ||
} | ||
|
||
public final inline fun <reified R, reified K> addPerThreadPerKeyFactory(@noinline factoryCalledPerKeyPerThread: (K) -> R) { | ||
addPerThreadPerKeyFactory(javaClass<R>(), javaClass<K>(), factoryCalledPerKeyPerThread) | ||
} | ||
|
||
public final inline fun <reified R> addLoggerFactory(@noinline factoryByName: (String) -> R, @noinline factoryByClass: (Class<*>) -> R) { | ||
addLoggerFactory(javaClass<R>(), factoryByName, factoryByClass) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package uy.kohesive.injekt | ||
|
||
import kotlin.properties.Delegates | ||
import kotlin.properties.ReadOnlyProperty | ||
|
||
|
||
/** | ||
* Not much difference than a InjektRegistrar for now... | ||
*/ | ||
public open class InjektScope(val registrar: InjektRegistrar) : InjektRegistrar by registrar{ | ||
override fun <T> alias(existingRegisteredClass: Class<T>, otherClassesThatAreSame: List<Class<*>>) { | ||
if (!hasFactory(existingRegisteredClass)) { | ||
throw InjektionException("Cannot alias anything to ${existingRegisteredClass.getName()}, it does not have a registered factory") | ||
} | ||
for (oneOther in otherClassesThatAreSame) { | ||
if (!oneOther.isAssignableFrom(existingRegisteredClass)) { | ||
throw InjektionException("Cannot alias ${oneOther.getName()} to ${existingRegisteredClass.getName()}, not compatible types") | ||
} | ||
} | ||
registrar.alias(existingRegisteredClass, otherClassesThatAreSame) | ||
} | ||
|
||
} | ||
|
||
public inline fun <reified T> Delegates.injectLazy(scope: InjektScope): ReadOnlyProperty<Any?, T> { | ||
return kotlin.properties.Delegates.lazy { scope.getInstance(javaClass<T>()) } | ||
} | ||
|
||
public inline fun <reified T> Delegates.injectValue(scope: InjektScope): ReadOnlyProperty<Any?, T> { | ||
val value: T = scope.getInstance(javaClass<T>()) | ||
return object : ReadOnlyProperty<Any?, T> { | ||
public override fun get(thisRef: Any?, desc: PropertyMetadata): T { | ||
return value | ||
} | ||
} | ||
} | ||
|
||
public inline fun <reified T> Delegates.injectLazy(scope: InjektScope, key: Any): ReadOnlyProperty<Any?, T> { | ||
return kotlin.properties.Delegates.lazy { | ||
scope.getKeyedInstance(javaClass<T>(), key) | ||
} | ||
} | ||
|
||
public inline fun <reified T> Delegates.injectValue(scope: InjektScope, key: Any): ReadOnlyProperty<Any?, T> { | ||
val value: T = scope.getKeyedInstance(javaClass<T>(), key) | ||
return object : ReadOnlyProperty<Any?, T> { | ||
public override fun get(thisRef: Any?, desc: PropertyMetadata): T { | ||
return value | ||
} | ||
} | ||
} | ||
|
||
public inline fun <reified R, reified T> Delegates.injectLogger(scope: InjektScope): ReadOnlyProperty<R, T> { | ||
val value: T = scope.getLogger(javaClass<T>(), javaClass<R>()) | ||
return object : ReadOnlyProperty<R, T> { | ||
public override fun get(thisRef: R, desc: PropertyMetadata): T { | ||
return value | ||
} | ||
} | ||
} | ||
|
||
public inline fun <reified R, reified T> Delegates.injectLogger(scope: InjektScope, byClass: Class<*>): ReadOnlyProperty<R, T> { | ||
val value: T = scope.getLogger(javaClass<T>(), byClass) | ||
return object : ReadOnlyProperty<R, T> { | ||
public override fun get(thisRef: R, desc: PropertyMetadata): T { | ||
return value | ||
} | ||
} | ||
} | ||
|
||
public inline fun <reified R, reified T> Delegates.injectLogger(scope: InjektScope, byName: String): ReadOnlyProperty<R, T> { | ||
val value: T = scope.getLogger(javaClass<T>(), byName) | ||
return object : ReadOnlyProperty<R, T> { | ||
public override fun get(thisRef: R, desc: PropertyMetadata): T { | ||
return value | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
dependencies { | ||
compile relativeProject(":injekt-api") | ||
} |
Oops, something went wrong.