From 5e261e344e6e74b7dc0f8898ba1cbcb5b82c990f Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Thu, 25 Dec 2014 19:39:50 +0300 Subject: [PATCH] Move ExitCode to cli-common, simplify KotlinCompilerRunner - check exit code's toString instead of integer code: more readable and stable this way - call newInstance() on arguments' classes instead of pointless constants --- .../jetbrains/jet/cli/common/ExitCode.java | 0 .../jet/compiler/runner/ArgumentUtils.java | 16 +++-- .../compiler/runner/CompilerRunnerUtil.java | 12 ---- .../compiler/runner/KotlinCompilerRunner.java | 60 ++++++++++++------- 4 files changed, 44 insertions(+), 44 deletions(-) rename compiler/cli/{ => cli-common}/src/org/jetbrains/jet/cli/common/ExitCode.java (100%) diff --git a/compiler/cli/src/org/jetbrains/jet/cli/common/ExitCode.java b/compiler/cli/cli-common/src/org/jetbrains/jet/cli/common/ExitCode.java similarity index 100% rename from compiler/cli/src/org/jetbrains/jet/cli/common/ExitCode.java rename to compiler/cli/cli-common/src/org/jetbrains/jet/cli/common/ExitCode.java diff --git a/ide-compiler-runner/src/org/jetbrains/jet/compiler/runner/ArgumentUtils.java b/ide-compiler-runner/src/org/jetbrains/jet/compiler/runner/ArgumentUtils.java index 28ef79eb622..5b7ef0b7fdb 100644 --- a/ide-compiler-runner/src/org/jetbrains/jet/compiler/runner/ArgumentUtils.java +++ b/ide-compiler-runner/src/org/jetbrains/jet/compiler/runner/ArgumentUtils.java @@ -31,22 +31,20 @@ public class ArgumentUtils { private ArgumentUtils() {} @NotNull - public static List convertArgumentsToStringList( - @NotNull T arguments, - @NotNull T defaultArguments - ) { + public static List convertArgumentsToStringList(@NotNull CommonCompilerArguments arguments) + throws InstantiationException, IllegalAccessException { List result = new ArrayList(); - convertArgumentsToStringList(arguments, defaultArguments, arguments.getClass(), result); + convertArgumentsToStringList(arguments, arguments.getClass().newInstance(), arguments.getClass(), result); result.addAll(arguments.freeArgs); return result; } - private static void convertArgumentsToStringList( - @NotNull T arguments, - @NotNull T defaultArguments, + private static void convertArgumentsToStringList( + @NotNull CommonCompilerArguments arguments, + @NotNull CommonCompilerArguments defaultArguments, @NotNull Class clazz, @NotNull List result - ) { + ) throws IllegalAccessException, InstantiationException { for (Field field : clazz.getDeclaredFields()) { Argument argument = field.getAnnotation(Argument.class); if (argument == null) continue; diff --git a/ide-compiler-runner/src/org/jetbrains/jet/compiler/runner/CompilerRunnerUtil.java b/ide-compiler-runner/src/org/jetbrains/jet/compiler/runner/CompilerRunnerUtil.java index fab64235c79..6ea40e41bcb 100644 --- a/ide-compiler-runner/src/org/jetbrains/jet/compiler/runner/CompilerRunnerUtil.java +++ b/ide-compiler-runner/src/org/jetbrains/jet/compiler/runner/CompilerRunnerUtil.java @@ -115,18 +115,6 @@ public class CompilerRunnerUtil { ); } - public static int getReturnCodeFromObject(@Nullable Object rc) throws Exception { - if (rc == null) { - return /* ExitCode.INTERNAL_ERROR */ 2; - } - else if ("org.jetbrains.jet.cli.common.ExitCode".equals(rc.getClass().getCanonicalName())) { - return (Integer) rc.getClass().getMethod("getCode").invoke(rc); - } - else { - throw new IllegalStateException("Unexpected return: " + rc); - } - } - @Nullable public static Object invokeExecMethod( @NotNull String compilerClassName, diff --git a/ide-compiler-runner/src/org/jetbrains/jet/compiler/runner/KotlinCompilerRunner.java b/ide-compiler-runner/src/org/jetbrains/jet/compiler/runner/KotlinCompilerRunner.java index 9fe5923f9d8..db45dc21a49 100644 --- a/ide-compiler-runner/src/org/jetbrains/jet/compiler/runner/KotlinCompilerRunner.java +++ b/ide-compiler-runner/src/org/jetbrains/jet/compiler/runner/KotlinCompilerRunner.java @@ -22,11 +22,12 @@ import com.intellij.util.Function; import com.intellij.util.containers.ContainerUtil; import com.intellij.util.xmlb.Accessor; import com.intellij.util.xmlb.XmlSerializerUtil; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.jet.cli.common.ExitCode; import org.jetbrains.jet.cli.common.arguments.CommonCompilerArguments; import org.jetbrains.jet.cli.common.arguments.K2JSCompilerArguments; import org.jetbrains.jet.cli.common.arguments.K2JVMCompilerArguments; -import org.jetbrains.jet.cli.common.messages.CompilerMessageLocation; -import org.jetbrains.jet.cli.common.messages.CompilerMessageSeverity; import org.jetbrains.jet.cli.common.messages.MessageCollector; import org.jetbrains.jet.cli.common.messages.MessageCollectorUtil; import org.jetbrains.jet.compiler.CompilerSettings; @@ -37,12 +38,12 @@ 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; +import static org.jetbrains.jet.cli.common.messages.CompilerMessageSeverity.INFO; 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"; - private static final K2JVMCompilerArguments DEFAULT_K2JVM_ARGUMENTS = new K2JVMCompilerArguments(); - private static final K2JSCompilerArguments DEFAULT_K2JS_ARGUMENTS = new K2JSCompilerArguments(); + private static final String INTERNAL_ERROR = ExitCode.INTERNAL_ERROR.toString(); public static void runK2JvmCompiler( CommonCompilerArguments commonArguments, @@ -56,8 +57,7 @@ public class KotlinCompilerRunner { K2JVMCompilerArguments arguments = mergeBeans(commonArguments, k2jvmArguments); setupK2JvmArguments(moduleFile, arguments); - runCompiler(K2JVM_COMPILER, arguments, compilerSettings.getAdditionalArguments(), - DEFAULT_K2JVM_ARGUMENTS, messageCollector, collector, environment); + runCompiler(K2JVM_COMPILER, arguments, compilerSettings.getAdditionalArguments(), messageCollector, collector, environment); } public static void runK2JsCompiler( @@ -74,55 +74,69 @@ public class KotlinCompilerRunner { K2JSCompilerArguments arguments = mergeBeans(commonArguments, k2jsArguments); setupK2JsArguments(outputFile, sourceFiles, libraryFiles, arguments); - runCompiler(K2JS_COMPILER, arguments, compilerSettings.getAdditionalArguments(), - DEFAULT_K2JS_ARGUMENTS, messageCollector, collector, environment); + runCompiler(K2JS_COMPILER, arguments, compilerSettings.getAdditionalArguments(), messageCollector, collector, environment); } private static void runCompiler( String compilerClassName, CommonCompilerArguments arguments, String additionalArguments, - CommonCompilerArguments defaultArguments, MessageCollector messageCollector, OutputItemsCollector collector, CompilerEnvironment environment ) { - List argumentsList = ArgumentUtils.convertArgumentsToStringList(arguments, defaultArguments); - argumentsList.addAll(StringUtil.split(additionalArguments, " ")); - ByteArrayOutputStream stream = new ByteArrayOutputStream(); PrintStream out = new PrintStream(stream); - int exitCode = execCompiler(compilerClassName, ArrayUtil.toStringArray(argumentsList), environment, out, messageCollector); + String exitCode = execCompiler(compilerClassName, arguments, additionalArguments, environment, out, messageCollector); BufferedReader reader = new BufferedReader(new StringReader(stream.toString())); CompilerOutputParser.parseCompilerMessagesFromReader(messageCollector, reader, collector); - if (exitCode != 0 && exitCode != 1) { + if (INTERNAL_ERROR.equals(exitCode)) { messageCollector.report(ERROR, "Compiler terminated with internal error", NO_LOCATION); } } - private static int execCompiler( + @NotNull + private static String execCompiler( String compilerClassName, - String[] arguments, + CommonCompilerArguments arguments, + String additionalArguments, CompilerEnvironment environment, PrintStream out, MessageCollector messageCollector ) { try { - messageCollector.report(CompilerMessageSeverity.INFO, - "Using kotlin-home = " + environment.getKotlinPaths().getHomePath(), - CompilerMessageLocation.NO_LOCATION); + messageCollector.report(INFO, "Using kotlin-home = " + environment.getKotlinPaths().getHomePath(), NO_LOCATION); - Object rc = CompilerRunnerUtil.invokeExecMethod(compilerClassName, arguments, environment, messageCollector, out); - // exec() returns a K2JVMCompiler.ExitCode object, that class is not accessible here, + List argumentsList = ArgumentUtils.convertArgumentsToStringList(arguments); + argumentsList.addAll(StringUtil.split(additionalArguments, " ")); + + Object rc = CompilerRunnerUtil.invokeExecMethod( + compilerClassName, ArrayUtil.toStringArray(argumentsList), environment, messageCollector, out + ); + + // exec() returns an ExitCode object, class of which is loaded with a different class loader, // so we take it's contents through reflection - return CompilerRunnerUtil.getReturnCodeFromObject(rc); + return getReturnCodeFromObject(rc); } catch (Throwable e) { MessageCollectorUtil.reportException(messageCollector, e); - return -1; + return INTERNAL_ERROR; + } + } + + @NotNull + private static String getReturnCodeFromObject(@Nullable Object rc) throws Exception { + if (rc == null) { + return INTERNAL_ERROR; + } + else if (ExitCode.class.getName().equals(rc.getClass().getName())) { + return rc.toString(); + } + else { + throw new IllegalStateException("Unexpected return: " + rc); } }