Convert CompilerRunnerUtil: actual conversion
This commit is contained in:
@@ -14,115 +14,105 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.jetbrains.kotlin.compilerRunner;
|
package org.jetbrains.kotlin.compilerRunner
|
||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.kotlin.preloading.ClassPreloadingUtils
|
||||||
import org.jetbrains.kotlin.cli.common.messages.MessageCollector;
|
import org.jetbrains.kotlin.preloading.Preloader
|
||||||
import org.jetbrains.kotlin.preloading.ClassPreloadingUtils;
|
import org.jetbrains.kotlin.utils.KotlinPaths
|
||||||
import org.jetbrains.kotlin.preloading.Preloader;
|
|
||||||
import org.jetbrains.kotlin.utils.KotlinPaths;
|
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File
|
||||||
import java.io.IOException;
|
import java.io.PrintStream
|
||||||
import java.io.PrintStream;
|
import java.lang.ref.SoftReference
|
||||||
import java.lang.ref.SoftReference;
|
import java.util.ArrayList
|
||||||
import java.lang.reflect.Method;
|
import java.util.Arrays
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity.ERROR;
|
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity.ERROR
|
||||||
|
|
||||||
public class CompilerRunnerUtil {
|
object CompilerRunnerUtil {
|
||||||
|
private var ourClassLoaderRef = SoftReference<ClassLoader>(null)
|
||||||
|
|
||||||
private static SoftReference<ClassLoader> ourClassLoaderRef = new SoftReference<>(null);
|
internal val jdkToolsJar: File?
|
||||||
|
get() {
|
||||||
|
val javaHomePath = System.getProperty("java.home")
|
||||||
|
if (javaHomePath == null || javaHomePath.isEmpty()) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
val javaHome = File(javaHomePath)
|
||||||
|
var toolsJar = File(javaHome, "lib/tools.jar")
|
||||||
|
if (toolsJar.exists()) {
|
||||||
|
return toolsJar.canonicalFile
|
||||||
|
}
|
||||||
|
|
||||||
@NotNull
|
// We might be inside jre.
|
||||||
private static synchronized ClassLoader getOrCreateClassLoader(
|
if (javaHome.name == "jre") {
|
||||||
@NotNull JpsCompilerEnvironment environment,
|
toolsJar = File(javaHome.parent, "lib/tools.jar")
|
||||||
@NotNull List<File> paths
|
if (toolsJar.exists()) {
|
||||||
) throws IOException {
|
return toolsJar.canonicalFile
|
||||||
ClassLoader classLoader = ourClassLoaderRef.get();
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
@Synchronized
|
||||||
|
private fun getOrCreateClassLoader(
|
||||||
|
environment: JpsCompilerEnvironment,
|
||||||
|
paths: List<File>
|
||||||
|
): ClassLoader {
|
||||||
|
var classLoader = ourClassLoaderRef.get()
|
||||||
if (classLoader == null) {
|
if (classLoader == null) {
|
||||||
classLoader = ClassPreloadingUtils.preloadClasses(
|
classLoader = ClassPreloadingUtils.preloadClasses(
|
||||||
paths,
|
paths,
|
||||||
Preloader.DEFAULT_CLASS_NUMBER_ESTIMATE,
|
Preloader.DEFAULT_CLASS_NUMBER_ESTIMATE,
|
||||||
CompilerRunnerUtil.class.getClassLoader(),
|
CompilerRunnerUtil::class.java.classLoader,
|
||||||
environment.getClassesToLoadByParent()
|
environment.classesToLoadByParent
|
||||||
);
|
)
|
||||||
ourClassLoaderRef = new SoftReference<>(classLoader);
|
ourClassLoaderRef = SoftReference(classLoader)
|
||||||
}
|
}
|
||||||
return classLoader;
|
return classLoader!!
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
fun getLibPath(paths: KotlinPaths, messageCollector: MessageCollector): File? {
|
||||||
public static File getLibPath(@NotNull KotlinPaths paths, @NotNull MessageCollector messageCollector) {
|
val libs = paths.libPath
|
||||||
File libs = paths.getLibPath();
|
if (libs.exists() && !libs.isFile) return libs
|
||||||
if (libs.exists() && !libs.isFile()) return libs;
|
|
||||||
|
|
||||||
messageCollector.report(
|
messageCollector.report(
|
||||||
ERROR,
|
ERROR,
|
||||||
"Broken compiler at '" + libs.getAbsolutePath() + "'. Make sure plugin is properly installed",
|
"Broken compiler at '" + libs.absolutePath + "'. Make sure plugin is properly installed", null
|
||||||
null
|
)
|
||||||
);
|
|
||||||
|
|
||||||
return null;
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
fun invokeExecMethod(
|
||||||
public static Object invokeExecMethod(
|
compilerClassName: String,
|
||||||
@NotNull String compilerClassName,
|
arguments: Array<String>,
|
||||||
@NotNull String[] arguments,
|
environment: JpsCompilerEnvironment,
|
||||||
@NotNull JpsCompilerEnvironment environment,
|
out: PrintStream
|
||||||
@NotNull PrintStream out
|
): Any? {
|
||||||
) throws Exception {
|
val libPath = getLibPath(environment.kotlinPaths, environment.messageCollector) ?: return null
|
||||||
File libPath = getLibPath(environment.getKotlinPaths(), environment.getMessageCollector());
|
|
||||||
if (libPath == null) return null;
|
|
||||||
|
|
||||||
List<File> paths = new ArrayList<>();
|
val paths = ArrayList<File>()
|
||||||
paths.add(new File(libPath, "kotlin-compiler.jar"));
|
paths.add(File(libPath, "kotlin-compiler.jar"))
|
||||||
|
|
||||||
if (Arrays.asList(arguments).contains("-Xuse-javac")) {
|
if (Arrays.asList(*arguments).contains("-Xuse-javac")) {
|
||||||
File toolsJar = getJdkToolsJar();
|
val toolsJar = jdkToolsJar
|
||||||
if (toolsJar != null) {
|
if (toolsJar != null) {
|
||||||
paths.add(toolsJar);
|
paths.add(toolsJar)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ClassLoader classLoader = getOrCreateClassLoader(environment, paths);
|
val classLoader = getOrCreateClassLoader(environment, paths)
|
||||||
|
|
||||||
Class<?> kompiler = Class.forName(compilerClassName, true, classLoader);
|
val kompiler = Class.forName(compilerClassName, true, classLoader)
|
||||||
Method exec = kompiler.getMethod(
|
val exec = kompiler.getMethod(
|
||||||
"execAndOutputXml",
|
"execAndOutputXml",
|
||||||
PrintStream.class,
|
PrintStream::class.java,
|
||||||
Class.forName("org.jetbrains.kotlin.config.Services", true, classLoader),
|
Class.forName("org.jetbrains.kotlin.config.Services", true, classLoader),
|
||||||
String[].class
|
Array<String>::class.java
|
||||||
);
|
)
|
||||||
|
|
||||||
return exec.invoke(kompiler.newInstance(), out, environment.getServices(), arguments);
|
return exec.invoke(kompiler.newInstance(), out, environment.services, 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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -257,7 +257,7 @@ class JpsKotlinCompilerRunner : KotlinCompilerRunner<JpsCompilerEnvironment>() {
|
|||||||
getOrCreateDaemonConnection {
|
getOrCreateDaemonConnection {
|
||||||
val libPath = CompilerRunnerUtil.getLibPath(environment.kotlinPaths, environment.messageCollector)
|
val libPath = CompilerRunnerUtil.getLibPath(environment.kotlinPaths, environment.messageCollector)
|
||||||
val compilerPath = File(libPath, "kotlin-compiler.jar")
|
val compilerPath = File(libPath, "kotlin-compiler.jar")
|
||||||
val toolsJarPath = CompilerRunnerUtil.getJdkToolsJar()
|
val toolsJarPath = CompilerRunnerUtil.jdkToolsJar
|
||||||
val compilerId = CompilerId.makeCompilerId(listOfNotNull(compilerPath, toolsJarPath))
|
val compilerId = CompilerId.makeCompilerId(listOfNotNull(compilerPath, toolsJarPath))
|
||||||
val daemonOptions = configureDaemonOptions()
|
val daemonOptions = configureDaemonOptions()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user