Skip to content

Commit fa5ce08

Browse files
committed
Eager loading of objects
1 parent cf5e16f commit fa5ce08

File tree

4 files changed

+80
-34
lines changed

4 files changed

+80
-34
lines changed

common/src/main/kotlin/com/lambda/Lambda.kt

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import com.google.gson.Gson
44
import com.google.gson.GsonBuilder
55
import com.lambda.config.serializer.BlockPosSerializer
66
import com.lambda.config.serializer.BlockSerializer
7-
import com.lambda.module.ModuleRegistry
8-
import com.lambda.module.modules.BoringModule
7+
import com.lambda.util.Eager
98
import net.minecraft.block.Block
109
import net.minecraft.client.MinecraftClient
1110
import net.minecraft.util.math.BlockPos
1211
import org.apache.logging.log4j.LogManager
1312
import org.apache.logging.log4j.Logger
13+
import org.reflections.Reflections
1414

1515
object Lambda {
1616
const val MOD_NAME = "Lambda"
@@ -27,39 +27,29 @@ object Lambda {
2727
.registerTypeAdapter(Block::class.java, BlockSerializer)
2828
.create()
2929

30-
init {
31-
// ToDo: Why do i have to call these?
32-
BoringModule
33-
ModuleRegistry
30+
fun initialize() {
31+
LOG.info("Initializing $MOD_NAME $VERSION")
3432

35-
// listener<KeyPressEvent> {
36-
// if (it.key != GLFW.GLFW_KEY_Z) {
37-
// return@listener
38-
// }
39-
//
40-
// taskContext {
41-
// HelloWorldTask()
42-
// .withDelay(500L)
43-
// .withTimeout(200L)
44-
// .withRepeats(10)
45-
// .withMaxAttempts(5)
46-
// .onSuccess {
47-
// LOG.info("Hello, World! Task completed")
48-
// }.onRepeat { repeats ->
49-
// LOG.info("Hello, World! Task $name repeated $repeats times")
50-
// }.onTimeout {
51-
// LOG.warn("Hello, World! Task $name timed out")
52-
// HelloWorldTask().execute()
53-
// }.onRetry {
54-
// LOG.warn("Hello, World! Task $name retrying")
55-
// }.onFailure { error ->
56-
// LOG.error("Hello, World! Task failed", error)
57-
// }.execute()
58-
// }
59-
// }
33+
initializeEagerObjects()
6034
}
6135

62-
fun initialize() {
63-
LOG.info("Initializing $MOD_NAME $VERSION")
36+
/**
37+
* Initializes all objects annotated with @[Eager].
38+
*/
39+
private fun initializeEagerObjects() {
40+
val reflections = Reflections("com.lambda")
41+
42+
val eagerTypes = reflections.getTypesAnnotatedWith(Eager::class.java)
43+
val eagerSubTypes = eagerTypes.flatMap { reflections.getSubTypesOf(it) }
44+
(eagerTypes + eagerSubTypes).forEach { eagerType ->
45+
try {
46+
val instanceField = eagerType.getDeclaredField("INSTANCE")
47+
instanceField.isAccessible = true
48+
val instance = instanceField.get(null)
49+
println("Initialized ${instance::class.simpleName}")
50+
} catch (e: Exception) {
51+
println("Failed to initialize ${eagerType.simpleName}")
52+
}
53+
}
6454
}
6555
}

common/src/main/kotlin/com/lambda/module/ModuleRegistry.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package com.lambda.module
22

33
import com.lambda.event.events.ClientEvent
44
import com.lambda.event.listener.UnsafeListener.Companion.unsafeListener
5+
import com.lambda.util.Eager
56
import org.reflections.Reflections
67
import org.reflections.scanners.Scanners
7-
import org.reflections.scanners.SubTypesScanner
88
import org.reflections.util.ClasspathHelper
99
import org.reflections.util.ConfigurationBuilder
1010

@@ -13,6 +13,7 @@ import org.reflections.util.ConfigurationBuilder
1313
*
1414
* @property modules A set of all [Module] instances in the system.
1515
*/
16+
@Eager
1617
object ModuleRegistry {
1718
private val modules = mutableSetOf<Module>()
1819

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.lambda.task.tasks
2+
3+
import com.lambda.Lambda.LOG
4+
import com.lambda.event.events.KeyPressEvent
5+
import com.lambda.event.listener.SafeListener.Companion.listener
6+
import com.lambda.threading.taskContext
7+
import org.lwjgl.glfw.GLFW
8+
9+
object TaskTester {
10+
init {
11+
listener<KeyPressEvent> {
12+
if (it.key != GLFW.GLFW_KEY_Z) {
13+
return@listener
14+
}
15+
16+
taskContext {
17+
HelloWorldTask()
18+
.withDelay(500L)
19+
.withTimeout(200L)
20+
.withRepeats(10)
21+
.withMaxAttempts(5)
22+
.onSuccess {
23+
LOG.info("Hello, World! Task completed")
24+
}.onRepeat { repeats ->
25+
LOG.info("Hello, World! Task $name repeated $repeats times")
26+
}.onTimeout {
27+
LOG.warn("Hello, World! Task $name timed out")
28+
HelloWorldTask().execute()
29+
}.onRetry {
30+
LOG.warn("Hello, World! Task $name retrying")
31+
}.onFailure { error ->
32+
LOG.error("Hello, World! Task failed", error)
33+
}.execute()
34+
}
35+
}
36+
}
37+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.lambda.util
2+
3+
/**
4+
* The [Eager] annotation is used to mark classes that should be initialized eagerly at startup.
5+
*
6+
* This annotation is typically used with object declarations in Kotlin,
7+
* which are lazily initialized by default.
8+
* By annotating an object with `@Eager`, you indicate that the object
9+
* should be initialized as soon as the program starts, rather than waiting until the object is first accessed.
10+
*
11+
* This annotation is also used to mark abstract classes whose subclasses should all be initialized eagerly.
12+
* When an abstract class is annotated with `@Eager`, all its subclasses are also initialized eagerly.
13+
*
14+
* Note: This annotation requires the use of reflection to find and initialize the annotated classes.
15+
* Therefore, it should be used judiciously to avoid potential performance issues.
16+
*/
17+
@Target(AnnotationTarget.CLASS)
18+
annotation class Eager

0 commit comments

Comments
 (0)