提交 91412b0e 编写于 作者: B baratynskiy 提交者: Mikhail Glukhikh

Add tools.jar for JPS plugin

上级 71ba8e3a
......@@ -324,7 +324,7 @@ class JavacWrapper(javaFiles: Collection<File>,
}
}
return symbol?.let { SymbolBasedClass(it, this@JavacWrapper, it.classfile) }
return symbol.let { SymbolBasedClass(it, this@JavacWrapper, it.classfile) }
}
}
\ No newline at end of file
......@@ -28,28 +28,30 @@ import java.io.IOException;
import java.io.PrintStream;
import java.lang.ref.SoftReference;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity.ERROR;
public class CompilerRunnerUtil {
private static SoftReference<ClassLoader> ourClassLoaderRef = new SoftReference<ClassLoader>(null);
private static SoftReference<ClassLoader> ourClassLoaderRef = new SoftReference<>(null);
@NotNull
private static synchronized ClassLoader getOrCreateClassLoader(
@NotNull JpsCompilerEnvironment environment,
@NotNull File libPath
@NotNull List<File> paths
) throws IOException {
ClassLoader classLoader = ourClassLoaderRef.get();
if (classLoader == null) {
classLoader = ClassPreloadingUtils.preloadClasses(
Collections.singletonList(new File(libPath, "kotlin-compiler.jar")),
paths,
Preloader.DEFAULT_CLASS_NUMBER_ESTIMATE,
CompilerRunnerUtil.class.getClassLoader(),
environment.getClassesToLoadByParent()
);
ourClassLoaderRef = new SoftReference<ClassLoader>(classLoader);
ourClassLoaderRef = new SoftReference<>(classLoader);
}
return classLoader;
}
......@@ -78,7 +80,17 @@ public class CompilerRunnerUtil {
File libPath = getLibPath(environment.getKotlinPaths(), environment.getMessageCollector());
if (libPath == null) return null;
ClassLoader classLoader = getOrCreateClassLoader(environment, libPath);
List<File> paths = new ArrayList<>();
paths.add(new File(libPath, "kotlin-compiler.jar"));
if (Arrays.asList(arguments).contains("-Xuse-javac")) {
File toolsJar = getJdkToolsJar();
if (toolsJar != null) {
paths.add(toolsJar);
}
}
ClassLoader classLoader = getOrCreateClassLoader(environment, paths);
Class<?> kompiler = Class.forName(compilerClassName, true, classLoader);
Method exec = kompiler.getMethod(
......@@ -90,4 +102,28 @@ public class CompilerRunnerUtil {
return exec.invoke(kompiler.newInstance(), out, environment.getServices(), arguments);
}
@Nullable
static File getJdkToolsJar() throws IOException {
String javaHomePath = System.getProperty("java.home");
if (javaHomePath == null || javaHomePath.isEmpty()) {
return null;
}
File javaHome = new File(javaHomePath);
File toolsJar = new File(javaHome, "lib/tools.jar");
if (toolsJar.exists()) {
return toolsJar.getCanonicalFile();
}
// We might be inside jre.
if (javaHome.getName().equals("jre")) {
toolsJar = new File(javaHome.getParent(), "lib/tools.jar");
if (toolsJar.exists()) {
return toolsJar.getCanonicalFile();
}
}
return null;
}
}
......@@ -227,7 +227,8 @@ class JpsKotlinCompilerRunner : KotlinCompilerRunner<JpsCompilerEnvironment>() {
getOrCreateDaemonConnection {
val libPath = CompilerRunnerUtil.getLibPath(environment.kotlinPaths, environment.messageCollector)
val compilerPath = File(libPath, "kotlin-compiler.jar")
val compilerId = CompilerId.makeCompilerId(compilerPath)
val toolsJarPath = CompilerRunnerUtil.getJdkToolsJar()
val compilerId = CompilerId.makeCompilerId(listOfNotNull(compilerPath, toolsJarPath) )
val daemonOptions = configureDaemonOptions()
val clientFlagFile = KotlinCompilerClient.getOrCreateClientFlagFile(daemonOptions)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册