Simplify KotlinCompilerRunner and CompilerRunnerUtil

This commit is contained in:
Alexander Udalov
2014-12-24 20:59:13 +03:00
parent 9807643b0e
commit a14e0ac59e
2 changed files with 22 additions and 35 deletions
@@ -16,7 +16,6 @@
package org.jetbrains.jet.compiler.runner;
import com.intellij.util.Function;
import kotlin.Function1;
import kotlin.KotlinPackage;
import org.jetbrains.annotations.NotNull;
@@ -27,7 +26,9 @@ import org.jetbrains.jet.preloading.ClassPreloadingUtils;
import org.jetbrains.jet.utils.KotlinPaths;
import org.jetbrains.jet.utils.UtilsPackage;
import java.io.*;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.lang.ref.SoftReference;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
@@ -114,12 +115,6 @@ public class CompilerRunnerUtil {
);
}
private static void handleProcessTermination(int exitCode, @NotNull MessageCollector messageCollector) {
if (exitCode != 0 && exitCode != 1) {
messageCollector.report(ERROR, "Compiler terminated with exit code: " + exitCode, NO_LOCATION);
}
}
public static int getReturnCodeFromObject(@Nullable Object rc) throws Exception {
if (rc == null) {
return /* ExitCode.INTERNAL_ERROR */ 2;
@@ -155,19 +150,4 @@ public class CompilerRunnerUtil {
return exec.invoke(kompiler.newInstance(), out, environment.getServices(), arguments);
}
public static void outputCompilerMessagesAndHandleExitCode(
@NotNull MessageCollector messageCollector,
@NotNull OutputItemsCollector outputItemsCollector,
@NotNull Function<PrintStream, Integer> compilerRun
) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PrintStream out = new PrintStream(outputStream);
int exitCode = compilerRun.fun(out);
BufferedReader reader = new BufferedReader(new StringReader(outputStream.toString()));
CompilerOutputParser.parseCompilerMessagesFromReader(messageCollector, reader, outputItemsCollector);
handleProcessTermination(exitCode, messageCollector);
}
}
@@ -31,11 +31,13 @@ import org.jetbrains.jet.cli.common.messages.MessageCollector;
import org.jetbrains.jet.cli.common.messages.MessageCollectorUtil;
import org.jetbrains.jet.compiler.CompilerSettings;
import java.io.File;
import java.io.PrintStream;
import java.io.*;
import java.util.Collection;
import java.util.List;
import static org.jetbrains.jet.cli.common.messages.CompilerMessageLocation.NO_LOCATION;
import static org.jetbrains.jet.cli.common.messages.CompilerMessageSeverity.ERROR;
public class KotlinCompilerRunner {
private static final String K2JVM_COMPILER = "org.jetbrains.jet.cli.jvm.K2JVMCompiler";
private static final String K2JS_COMPILER = "org.jetbrains.jet.cli.js.K2JSCompiler";
@@ -77,23 +79,28 @@ public class KotlinCompilerRunner {
}
private static void runCompiler(
final String compilerClassName,
String compilerClassName,
CommonCompilerArguments arguments,
String additionalArguments,
CommonCompilerArguments defaultArguments,
final MessageCollector messageCollector,
MessageCollector messageCollector,
OutputItemsCollector collector,
final CompilerEnvironment environment
CompilerEnvironment environment
) {
final List<String> argumentsList = ArgumentUtils.convertArgumentsToStringList(arguments, defaultArguments);
List<String> argumentsList = ArgumentUtils.convertArgumentsToStringList(arguments, defaultArguments);
argumentsList.addAll(StringUtil.split(additionalArguments, " "));
CompilerRunnerUtil.outputCompilerMessagesAndHandleExitCode(messageCollector, collector, new Function<PrintStream, Integer>() {
@Override
public Integer fun(PrintStream stream) {
return execCompiler(compilerClassName, ArrayUtil.toStringArray(argumentsList), environment, stream, messageCollector);
}
});
ByteArrayOutputStream stream = new ByteArrayOutputStream();
PrintStream out = new PrintStream(stream);
int exitCode = execCompiler(compilerClassName, ArrayUtil.toStringArray(argumentsList), environment, out, messageCollector);
BufferedReader reader = new BufferedReader(new StringReader(stream.toString()));
CompilerOutputParser.parseCompilerMessagesFromReader(messageCollector, reader, collector);
if (exitCode != 0 && exitCode != 1) {
messageCollector.report(ERROR, "Compiler terminated with internal error", NO_LOCATION);
}
}
private static int execCompiler(