diff --git a/compiler/cli/cli-common/src/org/jetbrains/jet/cli/common/arguments/CommonCompilerArguments.java b/compiler/cli/cli-common/src/org/jetbrains/jet/cli/common/arguments/CommonCompilerArguments.java index 7f51e11473c..9bdbef0ade1 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/jet/cli/common/arguments/CommonCompilerArguments.java +++ b/compiler/cli/cli-common/src/org/jetbrains/jet/cli/common/arguments/CommonCompilerArguments.java @@ -33,6 +33,8 @@ public abstract class CommonCompilerArguments extends CompilerArguments { public boolean help; @Argument(value = "suppress", description = "Suppress compiler messages by severity (warnings)") public String suppress; + @Argument(value = "printArgs", description = "Print commandline arguments") + public boolean printArgs; @Override public boolean isHelp() { @@ -58,6 +60,11 @@ public abstract class CommonCompilerArguments extends CompilerArguments { return verbose; } + @Override + public boolean isPrintArgs() { + return printArgs; + } + public void setTags(boolean tags) { this.tags = tags; } diff --git a/compiler/cli/cli-common/src/org/jetbrains/jet/cli/common/arguments/CompilerArguments.java b/compiler/cli/cli-common/src/org/jetbrains/jet/cli/common/arguments/CompilerArguments.java index 66549b57a0f..69870668c3c 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/jet/cli/common/arguments/CompilerArguments.java +++ b/compiler/cli/cli-common/src/org/jetbrains/jet/cli/common/arguments/CompilerArguments.java @@ -27,6 +27,7 @@ public abstract class CompilerArguments { public abstract boolean isTags(); public abstract boolean isVersion(); public abstract boolean isVerbose(); + public abstract boolean isPrintArgs(); public abstract String getSrc(); diff --git a/compiler/cli/src/com/sampullara/cli/ArgumentUtils.java b/compiler/cli/src/com/sampullara/cli/ArgumentUtils.java new file mode 100644 index 00000000000..dc2f08c9839 --- /dev/null +++ b/compiler/cli/src/com/sampullara/cli/ArgumentUtils.java @@ -0,0 +1,72 @@ +/* + * Copyright 2010-2013 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.sampullara.cli; + +import com.intellij.util.containers.ComparatorUtil; +import org.jetbrains.annotations.NotNull; + +import java.lang.reflect.Field; + +public class ArgumentUtils { + + private ArgumentUtils() {} + + @NotNull + public static String convertArgumentsToString(T arguments, T defaultArguments) { + StringBuilder result = new StringBuilder(); + convertArgumentsToString(arguments, defaultArguments, arguments.getClass(), result); + return result.toString(); + } + + public static void convertArgumentsToString(T arguments, T defaultArguments, Class clazz, StringBuilder result) { + Class superClazz = clazz.getSuperclass(); + if (superClazz != null) { + convertArgumentsToString(arguments, defaultArguments, superClazz, result); + } + + for (Field field : clazz.getDeclaredFields()) { + Argument argument = field.getAnnotation(Argument.class); + if (argument == null) continue; + + Object value; + Object defaultValue; + try { + value = field.get(arguments); + defaultValue = field.get(defaultArguments); + } + catch (IllegalAccessException ignored) { + // skip this field + continue; + } + + if (ComparatorUtil.equalsNullable(value, defaultValue)) continue; + + String name = Args.getAlias(argument); + if (name == null) { + name = Args.getName(argument, field); + } + + result.append("-").append(name).append(" "); + + Class fieldType = field.getType(); + if (fieldType != boolean.class && fieldType != Boolean.class) { + result.append(value).append(" "); + } + } + } + +} diff --git a/compiler/cli/src/org/jetbrains/jet/cli/common/CLICompiler.java b/compiler/cli/src/org/jetbrains/jet/cli/common/CLICompiler.java index 7c5a7199d69..b4904c62817 100644 --- a/compiler/cli/src/org/jetbrains/jet/cli/common/CLICompiler.java +++ b/compiler/cli/src/org/jetbrains/jet/cli/common/CLICompiler.java @@ -20,7 +20,9 @@ import com.google.common.base.Predicates; import com.google.common.collect.Lists; import com.intellij.openapi.Disposable; import com.intellij.openapi.util.Disposer; +import com.intellij.openapi.util.text.StringUtil; import com.sampullara.cli.Args; +import com.sampullara.cli.ArgumentUtils; import org.jetbrains.annotations.NotNull; import org.jetbrains.jet.cli.common.arguments.CompilerArguments; import org.jetbrains.jet.cli.common.messages.*; @@ -118,6 +120,7 @@ public abstract class CLICompiler { MessageRenderer messageRenderer = getMessageRenderer(arguments); errStream.print(messageRenderer.renderPreamble()); + printArgumentsIfNeeded(errStream, arguments, messageRenderer); printVersionIfNeeded(errStream, arguments, messageRenderer); MessageCollector collector = new PrintingMessageCollector(errStream, messageRenderer, arguments.isVerbose()); @@ -167,9 +170,11 @@ public abstract class CLICompiler { return arguments.isTags() ? MessageRenderer.TAGS : MessageRenderer.PLAIN; } - protected void printVersionIfNeeded(@NotNull PrintStream errStream, + protected void printVersionIfNeeded( + @NotNull PrintStream errStream, @NotNull A arguments, - @NotNull MessageRenderer messageRenderer) { + @NotNull MessageRenderer messageRenderer + ) { if (arguments.isVersion()) { String versionMessage = messageRenderer.render(CompilerMessageSeverity.INFO, "Kotlin Compiler version " + KotlinVersion.VERSION, @@ -178,6 +183,22 @@ public abstract class CLICompiler { } } + private void printArgumentsIfNeeded( + @NotNull PrintStream errStream, + @NotNull A arguments, + @NotNull MessageRenderer messageRenderer + ) { + if (arguments.isPrintArgs()) { + String freeArgs = StringUtil.join(arguments.freeArgs, ""); + String argumentsAsString = ArgumentUtils.convertArgumentsToString(arguments, createArguments()); + String printArgsMessage = messageRenderer.render(CompilerMessageSeverity.INFO, + "Invoking compiler " + getClass().getName() + + " with arguments " + argumentsAsString + freeArgs, + CompilerMessageLocation.NO_LOCATION); + errStream.println(printArgsMessage); + } + } + /** * Useful main for derived command line tools */ @@ -198,12 +219,6 @@ public abstract class CLICompiler { if (rc != OK) { System.err.println("exec() finished with " + rc + " return code"); } - if (Boolean.parseBoolean(System.getProperty("kotlin.print.cmd.args"))) { - System.out.println("Command line arguments:"); - for (String arg : args) { - System.out.println(arg); - } - } return rc; } catch (CompileEnvironmentException e) { diff --git a/compiler/testData/cli/help.out b/compiler/testData/cli/help.out index c302ec0c782..12245831ee3 100644 --- a/compiler/testData/cli/help.out +++ b/compiler/testData/cli/help.out @@ -18,4 +18,5 @@ Usage: org.jetbrains.jet.cli.common.arguments.K2JVMCompilerArguments -version [flag] Display compiler version -help (-h) [flag] Show help -suppress [String] Suppress compiler messages by severity (warnings) + -printArgs [flag] Print commandline arguments OK diff --git a/compiler/testData/cli/printArguments.out b/compiler/testData/cli/printArguments.out index e3519bc6647..7359c159a83 100644 --- a/compiler/testData/cli/printArguments.out +++ b/compiler/testData/cli/printArguments.out @@ -1,5 +1,3 @@ +INFO: Invoking compiler org.jetbrains.jet.cli.jvm.K2JVMCompiler with arguments -printArgs -script compiler/testData/cli/hello.ktscript hello -Command line arguments: --script -compiler/testData/cli/hello.ktscript OK diff --git a/compiler/testData/cli/wrongArgument.out b/compiler/testData/cli/wrongArgument.out index 2635874864b..90b8df5615a 100644 --- a/compiler/testData/cli/wrongArgument.out +++ b/compiler/testData/cli/wrongArgument.out @@ -19,4 +19,5 @@ Usage: org.jetbrains.jet.cli.common.arguments.K2JVMCompilerArguments -version [flag] Display compiler version -help (-h) [flag] Show help -suppress [String] Suppress compiler messages by severity (warnings) + -printArgs [flag] Print commandline arguments INTERNAL_ERROR diff --git a/compiler/tests/org/jetbrains/jet/cli/jvm/CliTest.java b/compiler/tests/org/jetbrains/jet/cli/jvm/CliTest.java index 3c44a2dea94..009797719ac 100644 --- a/compiler/tests/org/jetbrains/jet/cli/jvm/CliTest.java +++ b/compiler/tests/org/jetbrains/jet/cli/jvm/CliTest.java @@ -144,13 +144,7 @@ public class CliTest { @Test public void printArguments() { - try { - System.setProperty("kotlin.print.cmd.args", "true"); - executeCompilerCompareOutputJVM(new String[] {"-script", "compiler/testData/cli/hello.ktscript"}); - } - finally { - System.clearProperty("kotlin.print.cmd.args"); - } + executeCompilerCompareOutputJVM(new String[] {"-printArgs", "-script", "compiler/testData/cli/hello.ktscript"}); } @Test