Skip to content

Commit cf7f2b7

Browse files
committed
Minor, refactor and simplify RuntimePackagePartProvider
Lazy was unnecessary because we effectively were immediately evaluating it after creation anyway
1 parent 0f2befd commit cf7f2b7

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

core/descriptors.runtime/src/org/jetbrains/kotlin/load/kotlin/reflect/RuntimePackagePartProvider.kt

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,25 @@ package org.jetbrains.kotlin.load.kotlin.reflect
1818

1919
import org.jetbrains.kotlin.descriptors.PackagePartProvider
2020
import org.jetbrains.kotlin.load.kotlin.ModuleMapping
21-
import java.io.InputStream
2221
import java.util.concurrent.ConcurrentHashMap
2322

24-
class RuntimePackagePartProvider(val classLoader : ClassLoader) : PackagePartProvider {
25-
26-
val module2Mapping = ConcurrentHashMap<String, Lazy<ModuleMapping>>()
23+
class RuntimePackagePartProvider(private val classLoader: ClassLoader) : PackagePartProvider {
24+
private val module2Mapping = ConcurrentHashMap<String, ModuleMapping>()
2725

2826
fun registerModule(moduleName: String) {
29-
module2Mapping.putIfAbsent(moduleName, lazy {
30-
val resourceAsStream: InputStream = classLoader.getResourceAsStream("META-INF/$moduleName.${ModuleMapping.MAPPING_FILE_EXT}") ?: return@lazy ModuleMapping.create()
31-
32-
try {
33-
return@lazy ModuleMapping.create(resourceAsStream.readBytes())
27+
val mapping = try {
28+
classLoader.getResourceAsStream("META-INF/$moduleName.${ModuleMapping.MAPPING_FILE_EXT}")?.let { stream ->
29+
ModuleMapping.create(stream.readBytes())
3430
}
35-
catch (e: Exception) {
36-
return@lazy ModuleMapping.create()
37-
}
38-
})
31+
}
32+
catch (e: Exception) {
33+
// TODO: do not swallow this exception?
34+
null
35+
}
36+
module2Mapping.putIfAbsent(moduleName, mapping ?: ModuleMapping.EMPTY)
3937
}
4038

41-
4239
override fun findPackageParts(packageFqName: String): List<String> {
43-
return module2Mapping.values.map { it.value.findPackageParts(packageFqName) }.filterNotNull().flatMap { it.parts }.distinct()
40+
return module2Mapping.values.mapNotNull { it.findPackageParts(packageFqName) }.flatMap { it.parts }.distinct()
4441
}
45-
}
42+
}

0 commit comments

Comments
 (0)