Skip to content

Commit b4fa5e5

Browse files
committed
Update Reflections.kt
1 parent 6c90f18 commit b4fa5e5

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

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

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,29 @@ import java.util.Objects
2727
val cache = mutableMapOf<Int, Reflections>()
2828

2929
/**
30-
* Retrieves all instances of the specified type `T`.
30+
* This function retrieves or create a reflection instance to avoid redundant
31+
* reflection calls
3132
*
32-
* The function caches the results based on the configuration provided via the [block] lambda to avoid redundant
33-
* reflection calls.
33+
* Every time you instantiate a [Reflections] class, it scans the entire classloader and caches its result in a store
34+
*/
35+
inline fun reflectionCache(block: ConfigurationBuilder.() -> Unit): Reflections {
36+
val config = ConfigurationBuilder().apply(block)
37+
val cacheKey = Objects.hash(config.classLoaders, config.urls, config.scanners, config.inputsFilter)
38+
39+
return cache.getOrPut(cacheKey) { Reflections(config) }
40+
}
41+
42+
/**
43+
* Retrieves all instances of the specified type `T`.
3444
*
3545
* @param T The type of instances to retrieve.
3646
* @param block A configuration lambda to customize the [ConfigurationBuilder] used to configure Reflections.
3747
*
3848
* @return A list of instances of type `T`
3949
*/
40-
inline fun <reified T : Any> getInstances(block: ConfigurationBuilder.() -> Unit = { forPackage("com.lambda") }): List<T> {
41-
val config = ConfigurationBuilder().apply(block)
42-
val cacheKey = Objects.hash(config.classLoaders, config.urls, config.scanners, config.inputsFilter)
43-
44-
// Use previously scanned classes or create a new reflections
45-
val reflections = cache.getOrPut(cacheKey) { Reflections(config) }
46-
47-
return reflections.getSubTypesOf(T::class.java)
48-
.mapNotNull { clazz -> createInstance<T>(clazz) }
49-
}
50+
inline fun <reified T : Any> getInstances(block: ConfigurationBuilder.() -> Unit = { forPackage("com.lambda") }) =
51+
reflectionCache(block).getSubTypesOf(T::class.java)
52+
.mapNotNull { createInstance<T>(it) }
5053

5154
/**
5255
* Retrieves all resource paths that match the given pattern.
@@ -59,15 +62,8 @@ inline fun <reified T : Any> getInstances(block: ConfigurationBuilder.() -> Unit
5962
*
6063
* @return A set of resource paths that match the specified pattern.
6164
*/
62-
inline fun getResources(pattern: String, block: ConfigurationBuilder.() -> Unit = { forPackage("com.lambda") }): Set<String> {
63-
val config = ConfigurationBuilder().apply(block)
64-
val cacheKey = Objects.hash(config.classLoaders, config.urls, config.scanners, config.inputsFilter)
65-
66-
// Use previously scanned classes or create a new reflections
67-
val reflections = cache.getOrPut(cacheKey) { Reflections(config) }
68-
69-
return reflections.getResources(pattern)
70-
}
65+
inline fun getResources(pattern: String, block: ConfigurationBuilder.() -> Unit = { forPackage("com.lambda") }) =
66+
reflectionCache(block).getResources(pattern)
7167

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

0 commit comments

Comments
 (0)