From e6f1a3ccf3dfba73d0de9f2bf17b8f97120c8961 Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Wed, 2 Aug 2017 13:04:12 +0300 Subject: [PATCH] Minor: Use Kotlin reflection in usage printing and update test data --- .../jetbrains/kotlin/cli/common/Usage.java | 19 ++++---- compiler/testData/cli/js-dce/dceHelp.out | 10 ++-- compiler/testData/cli/js/jsExtraHelp.out | 22 ++++----- compiler/testData/cli/js/jsHelp.out | 30 ++++++------ compiler/testData/cli/jvm/extraHelp.out | 46 +++++++++---------- compiler/testData/cli/jvm/help.out | 20 ++++---- 6 files changed, 75 insertions(+), 72 deletions(-) diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/common/Usage.java b/compiler/cli/src/org/jetbrains/kotlin/cli/common/Usage.java index 7864f296a57..e480256e1e7 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/common/Usage.java +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/common/Usage.java @@ -16,14 +16,17 @@ package org.jetbrains.kotlin.cli.common; +import com.intellij.util.containers.ContainerUtil; +import kotlin.jvm.JvmClassMappingKt; +import kotlin.reflect.KCallable; +import kotlin.reflect.KClass; +import kotlin.reflect.KProperty1; import kotlin.text.StringsKt; import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.cli.common.arguments.Argument; import org.jetbrains.kotlin.cli.common.arguments.CommonToolArguments; import org.jetbrains.kotlin.cli.common.arguments.ParseCommandLineArgumentsKt; -import java.lang.reflect.Method; - public class Usage { // The magic number 29 corresponds to the similar padding width in javac and scalac command line compilers private static final int OPTION_NAME_PADDING_WIDTH = 29; @@ -33,10 +36,10 @@ public class Usage { StringBuilder sb = new StringBuilder(); appendln(sb, "Usage: " + tool.executableScriptFileName() + " "); appendln(sb, "where " + (arguments.getExtraHelp() ? "advanced" : "possible") + " options include:"); - for (Class clazz = arguments.getClass(); clazz != null; clazz = clazz.getSuperclass()) { - for (Method method : clazz.getDeclaredMethods()) { - propertyUsage(sb, method, arguments.getExtraHelp()); - } + KClass kClass = JvmClassMappingKt.getKotlinClass(arguments.getClass()); + for (KCallable callable : kClass.getMembers()) { + if (!(callable instanceof KProperty1)) continue; + propertyUsage(sb, (KProperty1) callable, arguments.getExtraHelp()); } if (arguments.getExtraHelp()) { @@ -47,8 +50,8 @@ public class Usage { return sb.toString(); } - private static void propertyUsage(@NotNull StringBuilder sb, @NotNull Method method, boolean extraHelp) { - Argument argument = method.getAnnotation(Argument.class); + private static void propertyUsage(@NotNull StringBuilder sb, @NotNull KProperty1 property, boolean extraHelp) { + Argument argument = ContainerUtil.findInstance(property.getAnnotations(), Argument.class); if (argument == null) return; if (extraHelp != ParseCommandLineArgumentsKt.isAdvanced(argument)) return; diff --git a/compiler/testData/cli/js-dce/dceHelp.out b/compiler/testData/cli/js-dce/dceHelp.out index 90733142797..c31e8e7ca2c 100644 --- a/compiler/testData/cli/js-dce/dceHelp.out +++ b/compiler/testData/cli/js-dce/dceHelp.out @@ -1,11 +1,11 @@ Usage: kotlin-dce-js where possible options include: - -output-dir Output directory -keep List of fully-qualified names of declarations that shouldn't be eliminated - -help (-h) Print a synopsis of standard options + -output-dir Output directory -X Print a synopsis of advanced options - -version Display compiler version - -verbose Enable verbose logging output + -help (-h) Print a synopsis of standard options -nowarn Generate no warnings -OK \ No newline at end of file + -verbose Enable verbose logging output + -version Display compiler version +OK diff --git a/compiler/testData/cli/js/jsExtraHelp.out b/compiler/testData/cli/js/jsExtraHelp.out index 6b74e44c4ee..b85dba4ef8f 100644 --- a/compiler/testData/cli/js/jsExtraHelp.out +++ b/compiler/testData/cli/js/jsExtraHelp.out @@ -1,19 +1,19 @@ Usage: kotlinc-js where advanced options include: - -Xtyped-arrays Translate primitive arrays to JS typed arrays - -Xfriend-modules-disabled Disable internal declaration export -Xfriend-modules= Paths to friend modules - -Xno-inline Disable method inlining - -Xrepeat= Repeat compilation (for performance analysis) - -Xskip-metadata-version-check Load classes with bad metadata version anyway (incl. pre-release classes) + -Xfriend-modules-disabled Disable internal declaration export + -Xtyped-arrays Translate primitive arrays to JS typed arrays -Xallow-kotlin-package Allow compiling code in package 'kotlin' - -Xreport-output-files Report source to output files mapping - -Xplugin= Load plugins from the given classpath - -Xmulti-platform Enable experimental language support for multi-platform projects - -Xno-check-impl Do not check presence of 'impl' modifier in multi-platform projects - -Xintellij-plugin-root= Path to the kotlin-compiler.jar or directory where IntelliJ configuration files can be found -Xcoroutines={enable|warn|error} Enable coroutines or report warnings or errors on declarations and use sites of 'suspend' modifier + -Xintellij-plugin-root= Path to the kotlin-compiler.jar or directory where IntelliJ configuration files can be found + -Xmulti-platform Enable experimental language support for multi-platform projects + -Xno-check-impl Do not check presence of 'impl' modifier in multi-platform projects + -Xno-inline Disable method inlining + -Xplugin= Load plugins from the given classpath + -Xrepeat= Repeat compilation (for performance analysis) + -Xreport-output-files Report source to output files mapping + -Xskip-metadata-version-check Load classes with bad metadata version anyway (incl. pre-release classes) Advanced options are non-standard and may be changed or removed without any notice. -OK \ No newline at end of file +OK diff --git a/compiler/testData/cli/js/jsHelp.out b/compiler/testData/cli/js/jsHelp.out index d56b000ff20..c1334ac1393 100644 --- a/compiler/testData/cli/js/jsHelp.out +++ b/compiler/testData/cli/js/jsHelp.out @@ -1,29 +1,29 @@ Usage: kotlinc-js where possible options include: - -output Output file path - -no-stdlib Don't use bundled Kotlin stdlib -libraries Paths to Kotlin libraries with .meta.js and .kjsm files, separated by system path separator + -main {call,noCall} Whether a main function should be called + -meta-info Generate .meta.js and .kjsm files with metadata. Use to create a library + -module-kind { plain, amd, commonjs, umd } + Kind of a module generated by compiler + -no-stdlib Don't use bundled Kotlin stdlib + -output Output file path + -output-postfix Path to file which will be added to the end of output file + -output-prefix Path to file which will be added to the beginning of output file -source-map Generate source map + -source-map-embed-sources { always, never, inlining } + Embed source files into source map -source-map-prefix Prefix for paths in a source map -source-map-source-roots Base directories which are used to calculate relative paths to source files in source map - -source-map-embed-sources { always, never, inlining } - Embed source files into source map - -meta-info Generate .meta.js and .kjsm files with metadata. Use to create a library -target { v5 } Generate JS files for specific ECMA version - -module-kind { plain, amd, commonjs, umd } - Kind of a module generated by compiler - -main {call,noCall} Whether a main function should be called - -output-prefix Path to file which will be added to the beginning of output file - -output-postfix Path to file which will be added to the end of output file - -language-version Provide source compatibility with specified language version -api-version Allow to use declarations only from the specified version of bundled libraries + -X Print a synopsis of advanced options + -help (-h) Print a synopsis of standard options -kotlin-home Path to Kotlin compiler home directory, used for runtime libraries discovery + -language-version Provide source compatibility with specified language version -P plugin::= Pass an option to a plugin - -help (-h) Print a synopsis of standard options - -X Print a synopsis of advanced options - -version Display compiler version - -verbose Enable verbose logging output -nowarn Generate no warnings + -verbose Enable verbose logging output + -version Display compiler version OK diff --git a/compiler/testData/cli/jvm/extraHelp.out b/compiler/testData/cli/jvm/extraHelp.out index ac3988bc4d8..fe05cf5efa2 100644 --- a/compiler/testData/cli/jvm/extraHelp.out +++ b/compiler/testData/cli/jvm/extraHelp.out @@ -1,38 +1,38 @@ Usage: kotlinc-jvm where advanced options include: - -Xmodule-path= Paths where to find Java 9+ modules + -Xadd-compiler-builtins Add definitions of built-in declarations to the compilation classpath (useful with -no-stdlib) -Xadd-modules= Root modules to resolve in addition to the initial modules, or all modules on the module path if is ALL-MODULE-PATH - -Xno-call-assertions Don't generate not-null assertion after each invocation of method returning not-null - -Xno-param-assertions Don't generate not-null assertions on parameters of methods accessible from Java - -Xno-optimize Disable optimizations - -Xreport-perf Report detailed performance statistics -Xbuild-file= Path to the .xml build file to compile - -Xmultifile-parts-inherit Compile multifile classes as a hierarchy of parts and facade - -Xskip-runtime-version-check Allow Kotlin runtime libraries of incompatible versions in the classpath - -Xuse-old-class-files-reading Use old class files reading implementation (may slow down the build and should be used in case of problems with the new implementation) -Xdump-declarations-to= Path to JSON file to dump Java to Kotlin declaration mappings - -Xsingle-module Combine modules for source files and binary dependencies into a single module - -Xadd-compiler-builtins Add definitions of built-in declarations to the compilation classpath (useful with -no-stdlib) - -Xload-builtins-from-dependencies - Load definitions of built-in declarations from module dependencies, instead of from the compiler - -Xscript-resolver-environment= - Script resolver environment in key-value pairs (the value could be quoted and escaped) - -Xuse-javac Use javac for Java source and class files analysis + -Xmultifile-parts-inherit Compile multifile classes as a hierarchy of parts and facade + -Xmodule-path= Paths where to find Java 9+ modules -Xjavac-arguments= Java compiler arguments -Xjsr305-annotations={ignore|enable} Specify global behavior for JSR-305 nullability annotations: ignore, or treat as other supported nullability annotations - -Xno-inline Disable method inlining - -Xrepeat= Repeat compilation (for performance analysis) - -Xskip-metadata-version-check Load classes with bad metadata version anyway (incl. pre-release classes) + -Xload-builtins-from-dependencies + Load definitions of built-in declarations from module dependencies, instead of from the compiler + -Xno-call-assertions Don't generate not-null assertion after each invocation of method returning not-null + -Xno-optimize Disable optimizations + -Xno-param-assertions Don't generate not-null assertions on parameters of methods accessible from Java + -Xreport-perf Report detailed performance statistics + -Xscript-resolver-environment= + Script resolver environment in key-value pairs (the value could be quoted and escaped) + -Xsingle-module Combine modules for source files and binary dependencies into a single module + -Xskip-runtime-version-check Allow Kotlin runtime libraries of incompatible versions in the classpath + -Xuse-javac Use javac for Java source and class files analysis + -Xuse-old-class-files-reading Use old class files reading implementation (may slow down the build and should be used in case of problems with the new implementation) -Xallow-kotlin-package Allow compiling code in package 'kotlin' - -Xreport-output-files Report source to output files mapping - -Xplugin= Load plugins from the given classpath - -Xmulti-platform Enable experimental language support for multi-platform projects - -Xno-check-impl Do not check presence of 'impl' modifier in multi-platform projects - -Xintellij-plugin-root= Path to the kotlin-compiler.jar or directory where IntelliJ configuration files can be found -Xcoroutines={enable|warn|error} Enable coroutines or report warnings or errors on declarations and use sites of 'suspend' modifier + -Xintellij-plugin-root= Path to the kotlin-compiler.jar or directory where IntelliJ configuration files can be found + -Xmulti-platform Enable experimental language support for multi-platform projects + -Xno-check-impl Do not check presence of 'impl' modifier in multi-platform projects + -Xno-inline Disable method inlining + -Xplugin= Load plugins from the given classpath + -Xrepeat= Repeat compilation (for performance analysis) + -Xreport-output-files Report source to output files mapping + -Xskip-metadata-version-check Load classes with bad metadata version anyway (incl. pre-release classes) Advanced options are non-standard and may be changed or removed without any notice. OK diff --git a/compiler/testData/cli/jvm/help.out b/compiler/testData/cli/jvm/help.out index 62969ab0319..655702b591b 100644 --- a/compiler/testData/cli/jvm/help.out +++ b/compiler/testData/cli/jvm/help.out @@ -1,26 +1,26 @@ Usage: kotlinc-jvm where possible options include: - -d Destination for generated class files -classpath (-cp) Paths where to find user class files + -d Destination for generated class files -include-runtime Include Kotlin runtime in to resulting .jar + -java-parameters Generate metadata for Java 1.8 reflection on method parameters -jdk-home Path to JDK home directory to include into classpath, if differs from default JAVA_HOME + -jvm-target Target version of the generated JVM bytecode (1.6 or 1.8), default is 1.6 + -module-name Module name -no-jdk Don't include Java runtime into classpath - -no-stdlib Don't include Kotlin runtime into classpath -no-reflect Don't include Kotlin reflection implementation into classpath + -no-stdlib Don't include Kotlin runtime into classpath -script Evaluate the script file -script-templates Script definition template classes - -module-name Module name - -jvm-target Target version of the generated JVM bytecode (1.6 or 1.8), default is 1.6 - -java-parameters Generate metadata for Java 1.8 reflection on method parameters - -language-version Provide source compatibility with specified language version -api-version Allow to use declarations only from the specified version of bundled libraries + -X Print a synopsis of advanced options + -help (-h) Print a synopsis of standard options -kotlin-home Path to Kotlin compiler home directory, used for runtime libraries discovery + -language-version Provide source compatibility with specified language version -P plugin::= Pass an option to a plugin - -help (-h) Print a synopsis of standard options - -X Print a synopsis of advanced options - -version Display compiler version - -verbose Enable verbose logging output -nowarn Generate no warnings + -verbose Enable verbose logging output + -version Display compiler version OK