diff --git a/compiler/javac-wrapper/src/org/jetbrains/kotlin/javac/JavacWrapper.kt b/compiler/javac-wrapper/src/org/jetbrains/kotlin/javac/JavacWrapper.kt index 7f1e52f7867a5f56c4a731886fc6aeb727dc600a..f75024ac6016f960de04a837910c79b79b4f40bf 100644 --- a/compiler/javac-wrapper/src/org/jetbrains/kotlin/javac/JavacWrapper.kt +++ b/compiler/javac-wrapper/src/org/jetbrains/kotlin/javac/JavacWrapper.kt @@ -324,7 +324,7 @@ class JavacWrapper(javaFiles: Collection, } } - return symbol?.let { SymbolBasedClass(it, this@JavacWrapper, it.classfile) } + return symbol.let { SymbolBasedClass(it, this@JavacWrapper, it.classfile) } } } \ No newline at end of file diff --git a/jps-plugin/src/org/jetbrains/kotlin/compilerRunner/CompilerRunnerUtil.java b/jps-plugin/src/org/jetbrains/kotlin/compilerRunner/CompilerRunnerUtil.java index b4cdfee1ff9dc251bbbf65703852e1ed0cb399ca..7ee8d7e6ee9a669dc814616e1a45de19438c73d7 100644 --- a/jps-plugin/src/org/jetbrains/kotlin/compilerRunner/CompilerRunnerUtil.java +++ b/jps-plugin/src/org/jetbrains/kotlin/compilerRunner/CompilerRunnerUtil.java @@ -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 ourClassLoaderRef = new SoftReference(null); + private static SoftReference ourClassLoaderRef = new SoftReference<>(null); @NotNull private static synchronized ClassLoader getOrCreateClassLoader( @NotNull JpsCompilerEnvironment environment, - @NotNull File libPath + @NotNull List 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); + 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 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; + } + } diff --git a/jps-plugin/src/org/jetbrains/kotlin/compilerRunner/JpsKotlinCompilerRunner.kt b/jps-plugin/src/org/jetbrains/kotlin/compilerRunner/JpsKotlinCompilerRunner.kt index 5cdad1f9fe7400a698ccbfdadfc22776296c1d4b..5ee3047d185e83cfceaf87b09e7ece68f2202e54 100644 --- a/jps-plugin/src/org/jetbrains/kotlin/compilerRunner/JpsKotlinCompilerRunner.kt +++ b/jps-plugin/src/org/jetbrains/kotlin/compilerRunner/JpsKotlinCompilerRunner.kt @@ -227,7 +227,8 @@ class JpsKotlinCompilerRunner : KotlinCompilerRunner() { 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)