From c3a5a9b7b510ea55606296d5b64f68480c07441a Mon Sep 17 00:00:00 2001 From: "Pavel V. Talanov" Date: Mon, 16 Sep 2013 13:10:48 +0400 Subject: [PATCH] Minor optimization for tests --- .../jetbrains/jet/plugin/MockLibraryUtil.java | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/idea/tests/org/jetbrains/jet/plugin/MockLibraryUtil.java b/idea/tests/org/jetbrains/jet/plugin/MockLibraryUtil.java index b35439b3880..66df0c80511 100644 --- a/idea/tests/org/jetbrains/jet/plugin/MockLibraryUtil.java +++ b/idea/tests/org/jetbrains/jet/plugin/MockLibraryUtil.java @@ -1,6 +1,7 @@ package org.jetbrains.jet.plugin; import com.intellij.util.io.ZipUtil; +import org.jetbrains.annotations.NotNull; import org.jetbrains.jet.JetTestUtils; import org.jetbrains.jet.cli.common.ExitCode; import org.jetbrains.jet.cli.jvm.K2JVMCompiler; @@ -9,6 +10,7 @@ import org.jetbrains.jet.utils.PathUtil; import java.io.*; import java.lang.reflect.Method; +import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; import java.util.zip.ZipOutputStream; @@ -16,6 +18,9 @@ import java.util.zip.ZipOutputStream; import static junit.framework.Assert.assertEquals; public class MockLibraryUtil { + + private static Class compilerClass = null; + public static File compileLibraryToJar(String sourcesPath) { try { File contentDir = JetTestUtils.tmpDir("lib-content"); @@ -37,15 +42,11 @@ public class MockLibraryUtil { } } + // Runs compiler in custom class loader to avoid effects caused by replacing Application with another one created in compiler. private static void compile(String sourcesPath, File outDir) { try { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); - - // Running compiler in custom class loader to avoid effects caused by replacing Application with another one created in compiler. - File kotlinCompilerJar = new File(PathUtil.getKotlinPathsForDistDirectory().getLibPath(), "kotlin-compiler.jar"); - URLClassLoader classLoader = new URLClassLoader(new URL[] {kotlinCompilerJar.toURI().toURL()}, Object.class.getClassLoader()); - - Class compilerClass = classLoader.loadClass(K2JVMCompiler.class.getName()); + Class compilerClass = getCompilerClass(); Object compilerObject = compilerClass.newInstance(); Method execMethod = compilerClass.getMethod("exec", PrintStream.class, String[].class); @@ -61,6 +62,18 @@ public class MockLibraryUtil { } } + @NotNull + private static Class getCompilerClass() throws MalformedURLException, ClassNotFoundException { + + if (compilerClass == null) { + File kotlinCompilerJar = new File(PathUtil.getKotlinPathsForDistDirectory().getLibPath(), "kotlin-compiler.jar"); + URLClassLoader classLoader = new URLClassLoader(new URL[] {kotlinCompilerJar.toURI().toURL()}, Object.class.getClassLoader()); + + compilerClass = classLoader.loadClass(K2JVMCompiler.class.getName()); + } + return compilerClass; + } + private MockLibraryUtil() { } }