https://kotlinlang.org logo
#compiler
Title
# compiler
v

Vampire

01/26/2023, 7:16 PM
I try to parse a Kotlin script. I found this blog entry: https://jitinsharma.com/posts/parsing-kotlin-using-code-kotlin/ Unfortunately when I try it like that I get
Copy code
java.lang.IllegalStateException: LOGGING: Loading modules: [<http://java.se|java.se>, jdk.accessibility, jdk.attach, jdk.compiler, jdk.dynalink, jdk.httpserver, jdk.jartool, jdk.javadoc, jdk.jconsole, jdk.jdi, jdk.jfr, jdk.jshell, jdk.jsobject, jdk.management, jdk.management.jfr, jdk.naming.ldap, <http://jdk.net|jdk.net>, jdk.scripting.nashorn, jdk.sctp, jdk.security.auth, jdk.security.jgss, jdk.unsupported, jdk.unsupported.desktop, jdk.xml.dom, java.base, java.compiler, java.datatransfer, java.desktop, java.xml, java.instrument, java.logging, java.management, java.management.rmi, java.rmi, java.naming, java.net.http, java.prefs, java.scripting, java.security.jgss, java.security.sasl, java.sql, java.transaction.xa, java.sql.rowset, java.xml.crypto, jdk.internal.jvmstat, jdk.management.agent, jdk.jdwp.agent, jdk.internal.ed, jdk.internal.le, jdk.internal.opt] (no MessageCollector configured)
	at org.jetbrains.kotlin.cli.jvm.compiler.ClasspathRootsResolver.report(ClasspathRootsResolver.kt:312)
	at org.jetbrains.kotlin.cli.jvm.compiler.ClasspathRootsResolver.report$default(ClasspathRootsResolver.kt:310)
	at org.jetbrains.kotlin.cli.jvm.compiler.ClasspathRootsResolver.addModularRoots(ClasspathRootsResolver.kt:253)
	at org.jetbrains.kotlin.cli.jvm.compiler.ClasspathRootsResolver.computeRoots(ClasspathRootsResolver.kt:123)
	at org.jetbrains.kotlin.cli.jvm.compiler.ClasspathRootsResolver.convertClasspathRoots(ClasspathRootsResolver.kt:79)
	at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment.<init>(KotlinCoreEnvironment.kt:213)
	at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment.<init>(KotlinCoreEnvironment.kt:109)
	at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment$Companion.createForProduction(KotlinCoreEnvironment.kt:421)
    [...]
Any quick idea someone?
a

Adam S

01/26/2023, 7:22 PM
v

Vampire

01/26/2023, 7:29 PM
Yep, thanks, solved it via
Copy code
CompilerConfiguration().apply {
    put(MESSAGE_COLLECTOR_KEY, MessageCollector.NONE)
}
Yay, found it myself after asking 👀
Copy code
PsiManager
    .getInstance(
        KotlinCoreEnvironment
            .createForProduction(
                Disposer.newDisposable(),
                CompilerConfiguration().apply {
                    put(MESSAGE_COLLECTOR_KEY, MessageCollector.NONE)
                },
                JVM_CONFIG_FILES
            )
            .project
    )
    .findFile(
        CoreLocalVirtualFile(
            CoreLocalFileSystem(),
            file(workflowScript)
        )
    )
    .let { it as KtFile }
    .fileAnnotationList
    ?.annotationEntries
    ?.filter { it.shortName?.asString() == "Import" }
    ?.flatMap { it.valueArgumentList?.arguments ?: emptyList() }
    ?.mapNotNull { it.getArgumentExpression() as? KtStringTemplateExpression }
    ?.map { it.entries.first() }
    ?.mapNotNull { it as? KtLiteralStringTemplateEntry }
    ?.map { it.text }
36 Views