@@ -27,6 +27,8 @@ import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
27
27
import org.jetbrains.kotlin.cli.common.messages.MessageRenderer
28
28
import org.jetbrains.kotlin.cli.common.messages.PrintingMessageCollector
29
29
import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler
30
+ import org.jetbrains.kotlin.cli.jvm.plugins.ServiceLoaderLite
31
+ import org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar
30
32
import org.jetbrains.kotlin.config.JVMAssertionsMode
31
33
import org.jetbrains.kotlin.config.JvmDefaultMode
32
34
import org.jetbrains.kotlin.config.JvmTarget
@@ -470,26 +472,10 @@ class KotlinCompilation {
470
472
}
471
473
}
472
474
473
- val resourcesUri = URI .create(
474
- this ::class .java.classLoader
475
- .getResource(" META-INF/services/org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar" )
476
- ?.toString()?.removeSuffix(" /META-INF/services/org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar" )
477
- ? : throw AssertionError (" Could not get path to ComponentRegistrar service from META-INF" )
478
- )
479
-
480
- val resourcesPath = when (resourcesUri.scheme) {
481
- " jar" -> Paths .get(URI .create(resourcesUri.schemeSpecificPart.removeSuffix(" !" )))
482
- " file" -> Paths .get(resourcesUri)
483
- else -> throw IllegalStateException (
484
- " Don't know how to handle protocol of ComponentRegistrar plugin. " +
485
- " Did you include this library in a weird way? Only jar and file path are supported."
486
- )
487
- }.toAbsolutePath()
488
-
489
475
val k2JvmArgs = commonK2JVMArgs().also {
490
476
it.freeArgs = sourcePaths
491
477
492
- it.pluginClasspaths = (it.pluginClasspaths?.toList() ? : emptyList<String >() + resourcesPath.toString ())
478
+ it.pluginClasspaths = (it.pluginClasspaths?.toList() ? : emptyList<String >() + getResourcesPath ())
493
479
.distinct().toTypedArray()
494
480
}
495
481
@@ -502,6 +488,24 @@ class KotlinCompilation {
502
488
)
503
489
}
504
490
491
+ private fun getResourcesPath (): String {
492
+ val resourceName = " META-INF/services/org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar"
493
+ return this ::class .java.classLoader.getResources(resourceName)
494
+ .asSequence()
495
+ .mapNotNull { url ->
496
+ val uri = URI .create(url.toString().removeSuffix(" /$resourceName " ))
497
+ when (uri.scheme) {
498
+ " jar" -> Paths .get(URI .create(uri.schemeSpecificPart.removeSuffix(" !" )))
499
+ " file" -> Paths .get(uri)
500
+ else -> return @mapNotNull null
501
+ }.toAbsolutePath()
502
+ }
503
+ .find { resourcesPath ->
504
+ ServiceLoaderLite .findImplementations(ComponentRegistrar ::class .java, listOf (resourcesPath.toFile()))
505
+ .any { implementation -> implementation == KaptComponentRegistrar ::class .java.name }
506
+ }?.toString() ? : throw AssertionError (" Could not get path to ComponentRegistrar service from META-INF" )
507
+ }
508
+
505
509
/* * Performs the 3rd compilation step to compile Kotlin source files */
506
510
private fun compileKotlin (sourceFiles : List <File >): ExitCode {
507
511
val sources = sourceFiles +
0 commit comments