[Plugins] Introduce new API for registering compiler plugins

Original `ComponentRegistrar` exposes Project to its registration method,
  so plugins should manually register extensions to it. To prepare for
  possible unbound compiler from Project API in future new  `K2ComponentRegistrar`
  introduced which provides registration method without Project at all
This commit is contained in:
Dmitriy Novozhilov
2022-06-07 10:23:09 +03:00
committed by teamcity
parent bb996c1b27
commit 8b42638afa
56 changed files with 392 additions and 191 deletions
@@ -26,6 +26,7 @@ import org.jetbrains.kotlin.cli.jvm.config.configureJdkClasspathRoots
import org.jetbrains.kotlin.cli.jvm.config.addJvmClasspathRoots
import org.jetbrains.kotlin.cli.jvm.plugins.ServiceLoaderLite
import org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar
import org.jetbrains.kotlin.compiler.plugin.CompilerPluginRegistrar
import org.jetbrains.kotlin.config.*
import org.jetbrains.kotlin.daemon.common.CompileService
import org.jetbrains.kotlin.daemon.common.CompilerId
@@ -207,13 +208,12 @@ inline fun getValidId(counter: AtomicInteger, check: (Int) -> Boolean): Int {
fun CompilerConfiguration.configureScripting(compilerId: CompilerId) {
val error = try {
val componentRegistrars =
(this::class.java.classLoader as? URLClassLoader)?.let {
ServiceLoaderLite.loadImplementations(ComponentRegistrar::class.java, it)
} ?: ServiceLoaderLite.loadImplementations(
ComponentRegistrar::class.java, compilerId.compilerClasspath.map(::File), this::class.java.classLoader
)
val componentRegistrars = loadRegistrars<ComponentRegistrar>(compilerId)
addAll(ComponentRegistrar.PLUGIN_COMPONENT_REGISTRARS, componentRegistrars)
val compilerPluginRegistrars = loadRegistrars<CompilerPluginRegistrar>(compilerId)
addAll(CompilerPluginRegistrar.COMPILER_PLUGIN_REGISTRARS, compilerPluginRegistrars)
null
} catch (e: NoClassDefFoundError) {
e
@@ -227,3 +227,11 @@ fun CompilerConfiguration.configureScripting(compilerId: CompilerId) {
)
}
}
private inline fun <reified T : Any> CompilerConfiguration.loadRegistrars(compilerId: CompilerId): List<T> {
return (this::class.java.classLoader as? URLClassLoader)?.let {
ServiceLoaderLite.loadImplementations(T::class.java, it)
} ?: ServiceLoaderLite.loadImplementations(
T::class.java, compilerId.compilerClasspath.map(::File), this::class.java.classLoader
)
}