From 164efe4c4da1e49c2c3131b96cb6b3221fb1dbe5 Mon Sep 17 00:00:00 2001 From: Sergey Karashevich Date: Thu, 18 Jan 2018 20:53:11 +0300 Subject: [PATCH] [gui-test] expand jar's classpath to compose the module's classpath Sometimes Windows doesn't accept long classpath as to command line limitations, than we need to compose a fake module xml with a classpath list and pass it to the Kotlin compiler. Unfortunately, if IDEA has been launched with a fake jar containing only a long classpath in its manifest, it doesn't work. This commit expand all classpath[any-number].jar and pathing.jar to the set of unique jar and class URLs and than we are composing a module xml to with a full classpath into a Kotlin compiler again. --- .../recorder/compile/KotlinCompileUtil.kt | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/platform/testGuiFramework/src/com/intellij/testGuiFramework/recorder/compile/KotlinCompileUtil.kt b/platform/testGuiFramework/src/com/intellij/testGuiFramework/recorder/compile/KotlinCompileUtil.kt index 1653cf439ece..42e888e8da21 100644 --- a/platform/testGuiFramework/src/com/intellij/testGuiFramework/recorder/compile/KotlinCompileUtil.kt +++ b/platform/testGuiFramework/src/com/intellij/testGuiFramework/recorder/compile/KotlinCompileUtil.kt @@ -21,6 +21,7 @@ import com.intellij.testGuiFramework.recorder.actions.PerformScriptAction import java.io.File import java.net.URL import java.nio.file.Paths +import java.util.jar.JarFile object KotlinCompileUtil { @@ -44,11 +45,24 @@ object KotlinCompileUtil { return urls.toList() } - var list: List = (ServiceManager::class.java.classLoader.forcedUrls() - + PerformScriptAction::class.java.classLoader.forcedUrls()) + val set = mutableSetOf() + set.addAll(ServiceManager::class.java.classLoader.forcedUrls()) + set.addAll(PerformScriptAction::class.java.classLoader.forcedUrls()) if (!ApplicationManager.getApplication().isUnitTestMode) - list += ServiceManager::class.java.classLoader.forcedBaseUrls() - return list + set.addAll(ServiceManager::class.java.classLoader.forcedBaseUrls()) + expandClasspathInJar(set) + return set.toList() + } + + private fun expandClasspathInJar(setOfUrls: MutableSet) { + val classpathUrl = setOfUrls.firstOrNull{Regex("classpath\\d*.jar").containsMatchIn(it.path) || it.path.endsWith("pathing.jar")} + if (classpathUrl != null) { + val classpathFile = Paths.get(classpathUrl.toURI()).toFile() + val classpathLine = JarFile(classpathFile).manifest.mainAttributes.getValue("Class-Path") + val classpathList = classpathLine.split(" ").filter { it.startsWith("file") }.map { URL(it) } + setOfUrls.addAll(classpathList) + setOfUrls.remove(classpathUrl) + } } private fun URL.getParentURL() = File(this.file).parentFile.toURI().toURL()!! @@ -67,8 +81,8 @@ object KotlinCompileUtil { private fun ClassLoader.forcedBaseUrls(): List { try { - return ((this.javaClass.getMethod("getBaseUrls").invoke(this) as? List<*>)!!. - filter { it is URL && it.protocol == "file" && !it.file.endsWith("jar!") }) as List + return ((this.javaClass.getMethod("getBaseUrls").invoke( + this) as? List<*>)!!.filter { it is URL && it.protocol == "file" && !it.file.endsWith("jar!") }) as List } catch (e: NoSuchMethodException) {