Skip to content

Commit

Permalink
Removed reflection remainings
Browse files Browse the repository at this point in the history
  • Loading branch information
IVIanuu committed Feb 25, 2024
1 parent 5d63136 commit 3295d20
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 90 deletions.
96 changes: 10 additions & 86 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,104 +1,28 @@
# Injekt

Next gen dependency injection library for Kotlin.
# Coffee maker sample from Dagger:
```kotlin
/** The main app responsible for brewing the coffee and printing the logs. */
fun main() {
val maker = inject<CoffeeMaker>()
val logger = inject<CoffeeLogger>()

maker.brew()
logger.logs.forEach { println(it) }
}

/** singleton scope */
object SingletonScope
@Provide val singletonScope = Scope<SingletonScope>()

/** A logger to logs steps while brewing coffee. */
@Provide @Scoped<SingletonScope> class CoffeeLogger {
private val _logs = mutableListOf<String>()
val logs: List<String> get() = _logs.toList()

fun log(msg: String) {
_logs += msg
}
}

/** A coffee maker to brew the coffee. */
@Provide class CoffeeMaker(
private val logger: CoffeeLogger,
heater: () -> Heater,
private val pump: Pump,
) {
private val heater by lazy(heater) // Create a possibly costly heater only when we use it.

fun brew() {
heater.on()
pump.pump()
logger.log(" [_]P coffee! [_]P ")
heater.off()
}
}

/** A heater to heat the coffee. */
interface Heater {
fun on()
fun off()
val isHot: Boolean
}
@Provide fun jsonParser() = JsonParser()

/** An electric heater to heat the coffee. */
@Provide @Scoped<SingletonScope> class ElectricHeater(private val logger: CoffeeLogger) : Heater {
override var isHot = false
private set
interface Http

override fun on() {
isHot = true
logger.log("~ ~ ~ heating ~ ~ ~")
}
@Provide class RealHttp : Http

override fun off() {
isHot = false
}
}
@Provide class Api(private val http: Http, private val jsonParser: JsonParser)

/** A pump to pump the coffee. */
interface Pump {
fun pump()
}
@Provide class Repository(private val api: Api)

/** A thermosiphon to pump the coffee. */
@Provide class Thermosiphon(private val logger: CoffeeLogger, private val heater: Heater) : Pump {
override fun pump() {
if (heater.isHot)
logger.log("=> => pumping => =>")
}
}
val repo = inject<Repository>()
```

# Setup
```kotlin
// in your gradle.properties
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8 \
-Dkotlin.daemon.jvm.options=--add-opens=java.base/java.lang=ALL-UNNAMED,--add-opens=java.base/java.lang.reflect=ALL-UNNAMED \
--add-opens=java.base/java.lang=ALL-UNNAMED \
--add-opens=java.base/java.lang.reflect=ALL-UNNAMED

// in your buildscript
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("com.ivianuu.injekt:injekt-gradle-plugin:${latest_version}")
}
plugins {
id("com.ivianuu.injekt") version latest_version
}

// in your app module
plugins {
apply("com.ivianuu.injekt")
repositories {
mavenCentral()
}

dependencies {
Expand Down
5 changes: 1 addition & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@
org.gradle.caching=true
org.gradle.configuration-cache=false
org.gradle.parallel=true
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8 \
-Dkotlin.daemon.jvm.options=--add-opens=java.base/java.lang=ALL-UNNAMED,--add-opens=java.base/java.lang.reflect=ALL-UNNAMED \
--add-opens=java.base/java.lang=ALL-UNNAMED \
--add-opens=java.base/java.lang.reflect=ALL-UNNAMED
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8

POM_DEVELOPER_URL=https\://github.com/IVIanuu
POM_LICENCE_URL=https\://www.apache.org/licenses/LICENSE-2.0.txt
Expand Down

0 comments on commit 3295d20

Please sign in to comment.