Skip to content

Commit d7a6e73

Browse files
committed
Updated reflections
1 parent 88aa476 commit d7a6e73

File tree

2 files changed

+20
-66
lines changed

2 files changed

+20
-66
lines changed

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

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,10 @@ import com.lambda.config.serializer.GameProfileSerializer
2626
import com.lambda.config.serializer.ItemStackSerializer
2727
import com.lambda.config.serializer.KeyCodeSerializer
2828
import com.lambda.config.serializer.OptionalSerializer
29-
import com.lambda.core.Loadable
3029
import com.lambda.core.Loader
31-
import com.lambda.threading.awaitMainThread
3230
import com.lambda.threading.recordRenderCall
33-
import com.lambda.threading.runGameScheduled
3431
import com.lambda.util.KeyCode
35-
import com.lambda.util.reflections.createInstance
3632
import com.mojang.authlib.GameProfile
37-
import io.github.classgraph.ClassGraph
38-
import kotlinx.coroutines.Dispatchers
39-
import kotlinx.coroutines.runBlocking
40-
import kotlinx.coroutines.withContext
4133
import net.minecraft.block.Block
4234
import net.minecraft.client.MinecraftClient
4335
import net.minecraft.item.ItemStack
@@ -77,29 +69,3 @@ object Lambda {
7769

7870
fun initialize(block: (Long) -> Unit) = recordRenderCall { Loader.initialize().apply(block) }
7971
}
80-
81-
/**
82-
* Execute a block of code on the main thread
83-
*/
84-
suspend fun <T> executeOnMainThread(block: () -> T): T {
85-
return withContext(Dispatchers.Main) {
86-
println("Executing on ${Thread.currentThread().name}")
87-
block()
88-
}
89-
}
90-
91-
suspend fun main() {
92-
val classes = ClassGraph()
93-
.enableAllInfo()
94-
.scan()
95-
.use { result ->
96-
result.getClassesImplementing(Loadable::class.java)
97-
.map { Class.forName(it.name) }
98-
}
99-
100-
val loadables = classes.mapNotNull {
101-
executeOnMainThread { createInstance<Loadable>(it) }
102-
}
103-
104-
loadables.forEach { println(it.load()) }
105-
}

common/src/main/kotlin/com/lambda/util/reflections/Reflections.kt

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,46 +20,34 @@ package com.lambda.util.reflections
2020
import com.lambda.util.extension.isObject
2121
import com.lambda.util.extension.objectInstance
2222
import io.github.classgraph.ClassGraph
23+
import io.github.classgraph.ClassInfo
24+
import io.github.classgraph.Resource
2325
import io.github.classgraph.ResourceList
26+
import io.github.classgraph.ScanResult
2427
import java.lang.reflect.Modifier
2528
import kotlin.jvm.java
2629

27-
/**
28-
* Retrieves all instances of the specified type `T`.
29-
*
30-
* @param T The type of instances to retrieve.
31-
* @param block A configuration lambda to customize the [ClassGraph] configuration.
32-
*
33-
* @return A list of instances of type `T`
34-
*/
35-
inline fun <reified T : Any> getInstances(block: ClassGraph.() -> Unit = { enableClassInfo(); acceptPackages("com.lambda") }): List<T> =
36-
ClassGraph().apply(block)
37-
.scan()
38-
.use { result ->
39-
val clazz = T::class.java
30+
val scanResult: ScanResult by lazy { ClassGraph().enableAllInfo().scan() }
4031

41-
return when {
42-
clazz.isInterface -> result.getClassesImplementing(T::class.java)
32+
inline fun <reified T : Any> getInstances(crossinline block: (ClassInfo) -> Boolean = { true }): List<T> {
33+
if (scanResult.isClosed) return emptyList()
4334

44-
clazz.isObject || Modifier.isAbstract(clazz.modifiers) ->
45-
result.getSubclasses(T::class.java)
35+
val clazz = T::class.java
4636

47-
else -> throw IllegalAccessException("class ${clazz.name} is neither an interface or abstract class")
48-
}.mapNotNull { createInstance<T>(Class.forName(it.name)) }
49-
}
37+
return when {
38+
clazz.isInterface -> scanResult.getClassesImplementing(clazz)
39+
.filter { block(it) }
5040

51-
/**
52-
* Retrieves all resource paths that match the given pattern wildcard.
53-
*
54-
* @param pattern The resource pattern to search for.
55-
* @param block A configuration lambda to customize the [ClassGraph] configuration.
56-
*
57-
* @return A [ResourceList]
58-
*/
59-
inline fun getResources(pattern: String, block: ClassGraph.() -> Unit = { enableAllInfo(); acceptPackages("com.lambda") }): ResourceList =
60-
ClassGraph().apply(block)
61-
.scan()
62-
.use { it.getResourcesMatchingWildcard(pattern) }
41+
clazz.isObject || Modifier.isAbstract(clazz.modifiers) -> scanResult.getSubclasses(clazz)
42+
.filter { block(it) }
43+
44+
else -> throw IllegalAccessException("class ${clazz.name} is neither an interface or open class")
45+
}.mapNotNull { createInstance<T>(Class.forName(it.name)) }
46+
}
47+
48+
inline fun getResources(pattern: String, crossinline block: (Resource) -> Boolean): ResourceList =
49+
scanResult.getResourcesMatchingWildcard(pattern)
50+
.filter { block(it) }
6351

6452
inline fun <reified T : Any> createInstance(clazz: Class<*>): T? {
6553
return when {

0 commit comments

Comments
 (0)