@@ -27,6 +27,8 @@ import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
2727import org.jetbrains.kotlin.cli.common.messages.MessageRenderer
2828import org.jetbrains.kotlin.cli.common.messages.PrintingMessageCollector
2929import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler
30+ import org.jetbrains.kotlin.cli.jvm.plugins.ServiceLoaderLite
31+ import org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar
3032import org.jetbrains.kotlin.config.JVMAssertionsMode
3133import org.jetbrains.kotlin.config.JvmDefaultMode
3234import org.jetbrains.kotlin.config.JvmTarget
@@ -470,26 +472,10 @@ class KotlinCompilation {
470472 }
471473 }
472474
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-
489475 val k2JvmArgs = commonK2JVMArgs().also {
490476 it.freeArgs = sourcePaths
491477
492- it.pluginClasspaths = (it.pluginClasspaths?.toList() ? : emptyList<String >() + resourcesPath.toString ())
478+ it.pluginClasspaths = (it.pluginClasspaths?.toList() ? : emptyList<String >() + getResourcesPath ())
493479 .distinct().toTypedArray()
494480 }
495481
@@ -502,6 +488,24 @@ class KotlinCompilation {
502488 )
503489 }
504490
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+
505509 /* * Performs the 3rd compilation step to compile Kotlin source files */
506510 private fun compileKotlin (sourceFiles : List <File >): ExitCode {
507511 val sources = sourceFiles +
0 commit comments