Tests: use preloader instead of URLClassLoader in MockLibraryUtil
This fixes runtime dependency of the compiler and thus allows to run tests which use MockLibraryUtil in case of an ABI-incompatible change; also it's slightly more efficient (noticeable when running a single test)
This commit is contained in:
@@ -19,6 +19,7 @@
|
|||||||
<orderEntry type="module" module-name="builtins-serializer" />
|
<orderEntry type="module" module-name="builtins-serializer" />
|
||||||
<orderEntry type="module" module-name="js.frontend" />
|
<orderEntry type="module" module-name="js.frontend" />
|
||||||
<orderEntry type="module" module-name="js.tests" />
|
<orderEntry type="module" module-name="js.tests" />
|
||||||
|
<orderEntry type="module" module-name="preloader" />
|
||||||
<orderEntry type="module" module-name="util" />
|
<orderEntry type="module" module-name="util" />
|
||||||
</component>
|
</component>
|
||||||
</module>
|
</module>
|
||||||
@@ -23,14 +23,13 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
import org.jetbrains.kotlin.cli.common.ExitCode;
|
import org.jetbrains.kotlin.cli.common.ExitCode;
|
||||||
import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler;
|
import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler;
|
||||||
import org.jetbrains.kotlin.codegen.forTestCompile.ForTestCompileRuntime;
|
import org.jetbrains.kotlin.codegen.forTestCompile.ForTestCompileRuntime;
|
||||||
|
import org.jetbrains.kotlin.preloading.ClassPreloadingUtils;
|
||||||
import org.jetbrains.kotlin.utils.PathUtil;
|
import org.jetbrains.kotlin.utils.PathUtil;
|
||||||
import org.jetbrains.kotlin.utils.UtilsPackage;
|
import org.jetbrains.kotlin.utils.UtilsPackage;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
import java.lang.ref.SoftReference;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.net.MalformedURLException;
|
|
||||||
import java.net.URL;
|
|
||||||
import java.net.URLClassLoader;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
@@ -42,7 +41,7 @@ import static org.junit.Assert.assertEquals;
|
|||||||
|
|
||||||
public class MockLibraryUtil {
|
public class MockLibraryUtil {
|
||||||
|
|
||||||
private static Class<?> compilerClass = null;
|
private static SoftReference<Class<?>> compilerClassRef = new SoftReference<Class<?>>(null);
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public static File compileLibraryToJar(
|
public static File compileLibraryToJar(
|
||||||
@@ -123,14 +122,15 @@ public class MockLibraryUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private static Class<?> getCompilerClass() throws MalformedURLException, ClassNotFoundException {
|
private static synchronized Class<?> getCompilerClass() throws IOException, ClassNotFoundException {
|
||||||
|
Class<?> compilerClass = compilerClassRef.get();
|
||||||
if (compilerClass == null) {
|
if (compilerClass == null) {
|
||||||
File kotlinCompilerJar = new File(PathUtil.getKotlinPathsForDistDirectory().getLibPath(), "kotlin-compiler.jar");
|
File kotlinCompilerJar = new File(PathUtil.getKotlinPathsForDistDirectory().getLibPath(), "kotlin-compiler.jar");
|
||||||
File kotlinRuntimeJar = new File(PathUtil.getKotlinPathsForDistDirectory().getLibPath(), "kotlin-runtime.jar");
|
ClassLoader classLoader =
|
||||||
URLClassLoader classLoader = new URLClassLoader(new URL[] {kotlinCompilerJar.toURI().toURL(), kotlinRuntimeJar.toURI().toURL()},
|
ClassPreloadingUtils.preloadClasses(Collections.singletonList(kotlinCompilerJar), 4096, null, null, null);
|
||||||
Object.class.getClassLoader());
|
|
||||||
|
|
||||||
compilerClass = classLoader.loadClass(K2JVMCompiler.class.getName());
|
compilerClass = classLoader.loadClass(K2JVMCompiler.class.getName());
|
||||||
|
compilerClassRef = new SoftReference<Class<?>>(compilerClass);
|
||||||
}
|
}
|
||||||
return compilerClass;
|
return compilerClass;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user