Skip to content

Commit

Permalink
(WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
Godin committed Nov 2, 2024
1 parent fef8cd0 commit 9c92fb7
Showing 1 changed file with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.lazy.declarations.FileBasedDeclarationProviderFactory
import java.io.File
import java.net.URLClassLoader

class Environment(
val classpath: List<String>,
kotlinLanguageVersion: LanguageVersion,
javaLanguageVersion: JvmTarget = JvmTarget.JVM_1_8,
numberOfThreads: Int? = null
numberOfThreads: Int? = null,
) {
val disposable = Disposer.newDisposable()
val configuration = compilerConfiguration(classpath, kotlinLanguageVersion, javaLanguageVersion, numberOfThreads)
Expand Down Expand Up @@ -114,6 +115,9 @@ fun compilerConfiguration(
numberOfThreads: Int?,
): CompilerConfiguration {
val classpathFiles = classpath.map(::File)

println(version(classpathFiles))

val versionSettings = LanguageVersionSettingsImpl(
languageVersion,
ApiVersion.createByLanguageVersion(languageVersion),
Expand All @@ -128,3 +132,22 @@ fun compilerConfiguration(
}
}

fun version(classpathFiles: List<File>): String {
class Loader(classpath: List<File>) : URLClassLoader(classpath.map { it.toURI().toURL() }.toTypedArray(), null) {
override fun loadClass(name: String): Class<*> {
if (name.startsWith("java.lang.")) {
return super.loadClass(name)
}
val loadedClass = findLoadedClass(name)
if (loadedClass != null) {
return loadedClass
}
return findClass(name);
}
}
Loader(classpathFiles).use {
// FIXME ClassNotFoundException, NoSuchFieldException
val kotlinVersion = it.loadClass("kotlin.KotlinVersion").getField("CURRENT").get(null)
return kotlinVersion.javaClass.getMethod("toString").invoke(kotlinVersion).toString()
}
}

0 comments on commit 9c92fb7

Please sign in to comment.