@@ -20,46 +20,34 @@ package com.lambda.util.reflections
2020import com.lambda.util.extension.isObject
2121import com.lambda.util.extension.objectInstance
2222import io.github.classgraph.ClassGraph
23+ import io.github.classgraph.ClassInfo
24+ import io.github.classgraph.Resource
2325import io.github.classgraph.ResourceList
26+ import io.github.classgraph.ScanResult
2427import java.lang.reflect.Modifier
2528import 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
6452inline fun <reified T : Any > createInstance (clazz : Class <* >): T ? {
6553 return when {
0 commit comments