KT-1636 Idea consumes the whole heap with Kotlin plugin
#KT-1636 fixed
This commit is contained in:
@@ -69,6 +69,13 @@ public class CompileEnvironment {
|
|||||||
this(MessageRenderer.PLAIN, false);
|
this(MessageRenderer.PLAIN, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* NOTE: It's very important to call dispose for every object of this class or there will be memory leaks.
|
||||||
|
* @see Disposer
|
||||||
|
*
|
||||||
|
* @param messageRenderer
|
||||||
|
* @param verbose
|
||||||
|
*/
|
||||||
public CompileEnvironment(MessageRenderer messageRenderer, boolean verbose) {
|
public CompileEnvironment(MessageRenderer messageRenderer, boolean verbose) {
|
||||||
this.verbose = verbose;
|
this.verbose = verbose;
|
||||||
myRootDisposable = new Disposable() {
|
myRootDisposable = new Disposable() {
|
||||||
@@ -170,38 +177,46 @@ public class CompileEnvironment {
|
|||||||
|
|
||||||
public boolean compileModuleScript(String moduleScriptFile, @Nullable String jarPath, @Nullable String outputDir, boolean jarRuntime) {
|
public boolean compileModuleScript(String moduleScriptFile, @Nullable String jarPath, @Nullable String outputDir, boolean jarRuntime) {
|
||||||
CompileEnvironment moduleCompilationEnvironment = copyEnvironment(false);
|
CompileEnvironment moduleCompilationEnvironment = copyEnvironment(false);
|
||||||
moduleCompilationEnvironment.myStdlib = myStdlib;
|
try {
|
||||||
|
moduleCompilationEnvironment.myStdlib = myStdlib;
|
||||||
|
|
||||||
List<Module> modules = moduleCompilationEnvironment.loadModuleScript(moduleScriptFile);
|
List<Module> modules = moduleCompilationEnvironment.loadModuleScript(moduleScriptFile);
|
||||||
|
|
||||||
if (modules == null) {
|
if (modules == null) {
|
||||||
throw new CompileEnvironmentException("Module script " + moduleScriptFile + " compilation failed");
|
throw new CompileEnvironmentException("Module script " + moduleScriptFile + " compilation failed");
|
||||||
}
|
|
||||||
|
|
||||||
if (modules.isEmpty()) {
|
|
||||||
throw new CompileEnvironmentException("No modules where defined by " + moduleScriptFile);
|
|
||||||
}
|
|
||||||
|
|
||||||
final String directory = new File(moduleScriptFile).getParent();
|
|
||||||
for (Module moduleBuilder : modules) {
|
|
||||||
CompileEnvironment compileEnvironment = copyEnvironment(verbose);
|
|
||||||
ClassFileFactory moduleFactory = compileEnvironment.compileModule(moduleBuilder, directory);
|
|
||||||
if (moduleFactory == null) {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
if (outputDir != null) {
|
|
||||||
writeToOutputDirectory(moduleFactory, outputDir);
|
if (modules.isEmpty()) {
|
||||||
|
throw new CompileEnvironmentException("No modules where defined by " + moduleScriptFile);
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
String path = jarPath != null ? jarPath : new File(directory, moduleBuilder.getModuleName() + ".jar").getPath();
|
final String directory = new File(moduleScriptFile).getParent();
|
||||||
|
for (Module moduleBuilder : modules) {
|
||||||
|
CompileEnvironment compileEnvironment = copyEnvironment(verbose);
|
||||||
try {
|
try {
|
||||||
writeToJar(moduleFactory, new FileOutputStream(path), null, jarRuntime);
|
ClassFileFactory moduleFactory = compileEnvironment.compileModule(moduleBuilder, directory);
|
||||||
} catch (FileNotFoundException e) {
|
if (moduleFactory == null) {
|
||||||
throw new CompileEnvironmentException("Invalid jar path " + path, e);
|
return false;
|
||||||
|
}
|
||||||
|
if (outputDir != null) {
|
||||||
|
writeToOutputDirectory(moduleFactory, outputDir);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
String path = jarPath != null ? jarPath : new File(directory, moduleBuilder.getModuleName() + ".jar").getPath();
|
||||||
|
try {
|
||||||
|
writeToJar(moduleFactory, new FileOutputStream(path), null, jarRuntime);
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
throw new CompileEnvironmentException("Invalid jar path " + path, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
compileEnvironment.dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
|
} finally {
|
||||||
|
moduleCompilationEnvironment.dispose();
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private CompileEnvironment copyEnvironment(boolean verbose) {
|
private CompileEnvironment copyEnvironment(boolean verbose) {
|
||||||
|
|||||||
@@ -262,7 +262,7 @@ public class JetCompiler implements TranslatingCompiler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static int execInProcess(File kotlinHome, VirtualFile outputDir, File scriptFile, PrintStream out, CompileContext context) {
|
private static int execInProcess(File kotlinHome, VirtualFile outputDir, File scriptFile, PrintStream out, CompileContext context) {
|
||||||
URLClassLoader loader = getOrCreateClassloader(kotlinHome, context);
|
URLClassLoader loader = getOrCreateClassLoader(kotlinHome, context);
|
||||||
try {
|
try {
|
||||||
String compilerClassName = "org.jetbrains.jet.cli.KotlinCompiler";
|
String compilerClassName = "org.jetbrains.jet.cli.KotlinCompiler";
|
||||||
Class<?> kompiler = Class.forName(compilerClassName, true, loader);
|
Class<?> kompiler = Class.forName(compilerClassName, true, loader);
|
||||||
@@ -288,13 +288,13 @@ public class JetCompiler implements TranslatingCompiler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static SoftReference<URLClassLoader> ourClassloaderRef = new SoftReference<URLClassLoader>(null);
|
private static SoftReference<URLClassLoader> ourClassLoaderRef = new SoftReference<URLClassLoader>(null);
|
||||||
|
|
||||||
private static URLClassLoader getOrCreateClassloader(File kotlinHome, CompileContext context) {
|
private static URLClassLoader getOrCreateClassLoader(File kotlinHome, CompileContext context) {
|
||||||
URLClassLoader answer = ourClassloaderRef.get();
|
URLClassLoader answer = ourClassLoaderRef.get();
|
||||||
if (answer == null) {
|
if (answer == null) {
|
||||||
answer = createClassloader(kotlinHome, context);
|
answer = createClassloader(kotlinHome, context);
|
||||||
ourClassloaderRef = new SoftReference<URLClassLoader>(answer);
|
ourClassLoaderRef = new SoftReference<URLClassLoader>(answer);
|
||||||
}
|
}
|
||||||
return answer;
|
return answer;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user