Minor optimization for tests

This commit is contained in:
Pavel V. Talanov
2013-09-16 13:10:48 +04:00
parent dc9c3b1a97
commit c3a5a9b7b5
@@ -1,6 +1,7 @@
package org.jetbrains.jet.plugin; package org.jetbrains.jet.plugin;
import com.intellij.util.io.ZipUtil; import com.intellij.util.io.ZipUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.JetTestUtils; import org.jetbrains.jet.JetTestUtils;
import org.jetbrains.jet.cli.common.ExitCode; import org.jetbrains.jet.cli.common.ExitCode;
import org.jetbrains.jet.cli.jvm.K2JVMCompiler; import org.jetbrains.jet.cli.jvm.K2JVMCompiler;
@@ -9,6 +10,7 @@ import org.jetbrains.jet.utils.PathUtil;
import java.io.*; import java.io.*;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
@@ -16,6 +18,9 @@ import java.util.zip.ZipOutputStream;
import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertEquals;
public class MockLibraryUtil { public class MockLibraryUtil {
private static Class<?> compilerClass = null;
public static File compileLibraryToJar(String sourcesPath) { public static File compileLibraryToJar(String sourcesPath) {
try { try {
File contentDir = JetTestUtils.tmpDir("lib-content"); 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) { private static void compile(String sourcesPath, File outDir) {
try { try {
ByteArrayOutputStream outStream = new ByteArrayOutputStream(); ByteArrayOutputStream outStream = new ByteArrayOutputStream();
Class<?> compilerClass = getCompilerClass();
// 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());
Object compilerObject = compilerClass.newInstance(); Object compilerObject = compilerClass.newInstance();
Method execMethod = compilerClass.getMethod("exec", PrintStream.class, String[].class); 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() { private MockLibraryUtil() {
} }
} }