diff --git a/compiler/tests-common/org/jetbrains/kotlin/test/MockLibraryUtil.kt b/compiler/tests-common/org/jetbrains/kotlin/test/MockLibraryUtil.kt index 26a92bd254b..e720ba299f0 100644 --- a/compiler/tests-common/org/jetbrains/kotlin/test/MockLibraryUtil.kt +++ b/compiler/tests-common/org/jetbrains/kotlin/test/MockLibraryUtil.kt @@ -17,31 +17,26 @@ package org.jetbrains.kotlin.test import com.intellij.openapi.util.io.FileUtil -import com.intellij.openapi.util.text.StringUtil -import com.intellij.util.ArrayUtil import com.intellij.util.io.ZipUtil +import org.jetbrains.kotlin.cli.common.CLICompiler import org.jetbrains.kotlin.cli.common.ExitCode import org.jetbrains.kotlin.cli.js.K2JSCompiler import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler import org.jetbrains.kotlin.codegen.forTestCompile.ForTestCompileRuntime import org.jetbrains.kotlin.preloading.ClassPreloadingUtils import org.jetbrains.kotlin.preloading.Preloader -import org.jetbrains.kotlin.utils.* import org.jetbrains.kotlin.utils.PathUtil - -import java.io.* +import org.junit.Assert.assertEquals +import java.io.ByteArrayOutputStream +import java.io.File +import java.io.FileOutputStream +import java.io.PrintStream import java.lang.ref.SoftReference -import java.lang.reflect.Method -import java.util.ArrayList -import java.util.Arrays -import java.util.Collections import java.util.regex.Pattern import java.util.zip.ZipOutputStream - -import org.junit.Assert.assertEquals +import kotlin.reflect.KClass object MockLibraryUtil { - private var compilerClassLoader = SoftReference(null) @JvmStatic @@ -69,19 +64,10 @@ object MockLibraryUtil { allowKotlinPackage: Boolean, vararg extraClasspath: String ): File { - try { - return compileLibraryToJar( - sourcesPath, - KotlinTestUtils.tmpDir("testLibrary-" + jarName), - jarName, - addSources, - allowKotlinPackage, - *extraClasspath) - } - catch (e: IOException) { - throw rethrow(e) - } - + return compileLibraryToJar( + sourcesPath, KotlinTestUtils.tmpDir("testLibrary-" + jarName), jarName, addSources, allowKotlinPackage, + *extraClasspath + ) } @JvmStatic @@ -93,76 +79,59 @@ object MockLibraryUtil { allowKotlinPackage: Boolean, vararg extraClasspath: String ): File { - try { - val classesDir = File(contentDir, "classes") + val classesDir = File(contentDir, "classes") - val srcFile = File(sourcesPath) - val kotlinFiles = FileUtil.findFilesByMask(Pattern.compile(".*\\.kt"), srcFile) - if (srcFile.isFile || !kotlinFiles.isEmpty()) { - compileKotlin(sourcesPath, classesDir, allowKotlinPackage, *extraClasspath) + val srcFile = File(sourcesPath) + val kotlinFiles = FileUtil.findFilesByMask(Pattern.compile(".*\\.kt"), srcFile) + if (srcFile.isFile || kotlinFiles.isNotEmpty()) { + compileKotlin(sourcesPath, classesDir, allowKotlinPackage, *extraClasspath) + } + + val javaFiles = FileUtil.findFilesByMask(Pattern.compile(".*\\.java"), srcFile) + if (javaFiles.isNotEmpty()) { + val classpath = mutableListOf() + classpath += ForTestCompileRuntime.runtimeJarForTests().path + classpath += KotlinTestUtils.getAnnotationsJar().path + classpath += extraClasspath + + // Probably no kotlin files were present, so dir might not have been created after kotlin compiler + if (classesDir.exists()) { + classpath += classesDir.path + } + else { + FileUtil.createDirectory(classesDir) } - val javaFiles = FileUtil.findFilesByMask(Pattern.compile(".*\\.java"), srcFile) - if (!javaFiles.isEmpty()) { - val classpath = ArrayList() - classpath.add(ForTestCompileRuntime.runtimeJarForTests().path) - classpath.add(KotlinTestUtils.getAnnotationsJar().path) - Collections.addAll(classpath, *extraClasspath) + val options = listOf( + "-classpath", classpath.joinToString(File.pathSeparator), + "-d", classesDir.path + ) - // Probably no kotlin files were present, so dir might not have been created after kotlin compiler - if (classesDir.exists()) { - classpath.add(classesDir.path) - } - else { - FileUtil.createDirectory(classesDir) - } - - val options = Arrays.asList( - "-classpath", StringUtil.join(classpath, File.pathSeparator), - "-d", classesDir.path - ) - - KotlinTestUtils.compileJavaFiles(javaFiles, options) - } - - return createJarFile(contentDir, classesDir, sourcesPath, jarName, addSources) - } - catch (e: IOException) { - throw rethrow(e) + KotlinTestUtils.compileJavaFiles(javaFiles, options) } + return createJarFile(contentDir, classesDir, sourcesPath, jarName, addSources) } - private fun compileJsLibraryToJar( - sourcesPath: String, - jarName: String, - addSources: Boolean - ): File { - try { - val contentDir = KotlinTestUtils.tmpDir("testLibrary-" + jarName) + private fun compileJsLibraryToJar(sourcesPath: String, jarName: String, addSources: Boolean): File { + val contentDir = KotlinTestUtils.tmpDir("testLibrary-" + jarName) - val outDir = File(contentDir, "out") - val outputFile = File(outDir, jarName + ".js") - compileKotlin2JS(sourcesPath, outputFile) - - return createJarFile(contentDir, outDir, sourcesPath, jarName, addSources) - } - catch (e: IOException) { - throw rethrow(e) - } + val outDir = File(contentDir, "out") + val outputFile = File(outDir, jarName + ".js") + compileKotlin2JS(sourcesPath, outputFile) + return createJarFile(contentDir, outDir, sourcesPath, jarName, addSources) } - @Throws(IOException::class) fun createJarFile(contentDir: File, dirToAdd: File, sourcesPath: String, jarName: String, addSources: Boolean): File { val jarFile = File(contentDir, jarName + ".jar") - val zip = ZipOutputStream(FileOutputStream(jarFile)) - ZipUtil.addDirToZipRecursively(zip, jarFile, dirToAdd, "", null, null) - if (addSources) { - ZipUtil.addDirToZipRecursively(zip, jarFile, File(sourcesPath), "src", null, null) + ZipOutputStream(FileOutputStream(jarFile)).use { zip -> + ZipUtil.addDirToZipRecursively(zip, jarFile, dirToAdd, "", null, null) + if (addSources) { + ZipUtil.addDirToZipRecursively(zip, jarFile, File(sourcesPath), "src", null, null) + } } - zip.close() return jarFile } @@ -177,19 +146,11 @@ object MockLibraryUtil { // Runs compiler in custom class loader to avoid effects caused by replacing Application with another one created in compiler. private fun runCompiler(compilerClass: Class<*>, args: List) { - try { - val outStream = ByteArrayOutputStream() - val compiler = compilerClass.newInstance() - val execMethod = compilerClass.getMethod("exec", PrintStream::class.java, Array::class.java) - - val invocationResult = execMethod.invoke(compiler, PrintStream(outStream), ArrayUtil.toStringArray(args)) as Enum<*> - - assertEquals(String(outStream.toByteArray()), ExitCode.OK.name, invocationResult.name) - } - catch (e: Throwable) { - throw rethrow(e) - } - + val outStream = ByteArrayOutputStream() + val compiler = compilerClass.newInstance() + val execMethod = compilerClass.getMethod("exec", PrintStream::class.java, Array::class.java) + val invocationResult = execMethod.invoke(compiler, PrintStream(outStream), args.toTypedArray()) as Enum<*> + assertEquals(String(outStream.toByteArray()), ExitCode.OK.name, invocationResult.name) } @JvmStatic @@ -203,72 +164,51 @@ object MockLibraryUtil { allowKotlinPackage: Boolean, vararg extraClasspath: String ) { - val classpath = ArrayList() + val classpath = mutableListOf() if (File(sourcesPath).isDirectory) { - classpath.add(sourcesPath) + classpath += sourcesPath } - Collections.addAll(classpath, *extraClasspath) + classpath += extraClasspath - val args = ArrayList() - args.add(sourcesPath) - args.add("-d") - args.add(outDir.absolutePath) - args.add("-classpath") - args.add(StringUtil.join(classpath, File.pathSeparator)) + val args = mutableListOf( + sourcesPath, + "-d", outDir.absolutePath, + "-classpath", classpath.joinToString(File.pathSeparator) + ) if (allowKotlinPackage) { - args.add("-Xallow-kotlin-package") + args += "-Xallow-kotlin-package" } runJvmCompiler(args) } private fun compileKotlin2JS(sourcesPath: String, outputFile: File) { - val args = ArrayList() - - args.add("-meta-info") - args.add("-output") - args.add(outputFile.absolutePath) - - args.add(sourcesPath) - - runJsCompiler(args) + runJsCompiler(listOf("-meta-info", "-output", outputFile.absolutePath, sourcesPath)) } fun compileKotlinModule(modulePath: String) { - runJvmCompiler(Arrays.asList("-no-stdlib", "-module", modulePath)) + runJvmCompiler(listOf("-no-stdlib", "-module", modulePath)) } private val compiler2JVMClass: Class<*> - @Synchronized get() = loadCompilerClass(K2JVMCompiler::class.java.name) + @Synchronized get() = loadCompilerClass(K2JVMCompiler::class) private val compiler2JSClass: Class<*> - @Synchronized get() = loadCompilerClass(K2JSCompiler::class.java.name) + @Synchronized get() = loadCompilerClass(K2JSCompiler::class) - @Synchronized private fun loadCompilerClass(compilerClassName: String): Class<*> { - try { - var classLoader = compilerClassLoader.get() - if (classLoader == null) { - classLoader = createCompilerClassLoader() - compilerClassLoader = SoftReference(classLoader) - } - return classLoader.loadClass(compilerClassName) + @Synchronized + private fun loadCompilerClass(compilerClass: KClass>): Class<*> { + val classLoader = compilerClassLoader.get() ?: createCompilerClassLoader().also { classLoader -> + compilerClassLoader = SoftReference(classLoader) } - catch (e: Throwable) { - throw rethrow(e) - } - + return classLoader.loadClass(compilerClass.java.name) } - @Synchronized private fun createCompilerClassLoader(): ClassLoader { - try { - val kotlinCompilerJar = File(PathUtil.getKotlinPathsForDistDirectory().libPath, "kotlin-compiler.jar") - return ClassPreloadingUtils.preloadClasses( - listOf(kotlinCompilerJar), Preloader.DEFAULT_CLASS_NUMBER_ESTIMATE, null, null, null - ) - } - catch (e: Throwable) { - throw rethrow(e) - } - + @Synchronized + private fun createCompilerClassLoader(): ClassLoader { + return ClassPreloadingUtils.preloadClasses( + listOf(PathUtil.getKotlinPathsForDistDirectory().compilerPath), + Preloader.DEFAULT_CLASS_NUMBER_ESTIMATE, null, null + ) } }