From 574461488638e951d55428ba806fdfa7dee3e44c Mon Sep 17 00:00:00 2001 From: Viktor Kolomeyko Date: Tue, 29 Jan 2019 10:48:33 +0000 Subject: [PATCH] ENT-3001: Make `subCommands` lazy val (#4668) This is to prevent initialising it multiple times. Please see linked Jira to understand how this can cause harm. --- .../src/main/kotlin/net/corda/cliutils/CordaCliWrapper.kt | 8 ++++---- .../net/corda/cliutils/InstallShellExtensionsParser.kt | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tools/cliutils/src/main/kotlin/net/corda/cliutils/CordaCliWrapper.kt b/tools/cliutils/src/main/kotlin/net/corda/cliutils/CordaCliWrapper.kt index 209262fc4a..7a61cc3a28 100644 --- a/tools/cliutils/src/main/kotlin/net/corda/cliutils/CordaCliWrapper.kt +++ b/tools/cliutils/src/main/kotlin/net/corda/cliutils/CordaCliWrapper.kt @@ -84,7 +84,7 @@ fun CordaCliWrapper.start(args: Array) { exitProcess(ExitCodes.SUCCESS) } catch (e: ExecutionException) { val throwable = e.cause ?: e - if (this.verbose || this.subCommands().any { it.verbose }) { + if (this.verbose || this.subCommands.any { it.verbose }) { throwable.printStackTrace() } else { } @@ -164,7 +164,7 @@ abstract class CordaCliWrapper(alias: String, description: String) : CliWrapperB registerConverter(Path::class.java) { Paths.get(it).toAbsolutePath().normalize() } commandSpec.name(alias) commandSpec.usageMessage().description(description) - subCommands().forEach { + subCommands.forEach { val subCommand = CommandLine(it) it.args = args subCommand.commandSpec.usageMessage().description(it.description) @@ -173,8 +173,8 @@ abstract class CordaCliWrapper(alias: String, description: String) : CliWrapperB } } - fun subCommands(): Set { - return additionalSubCommands() + installShellExtensionsParser + val subCommands: Set by lazy { + additionalSubCommands() + installShellExtensionsParser } override fun call(): Int { diff --git a/tools/cliutils/src/main/kotlin/net/corda/cliutils/InstallShellExtensionsParser.kt b/tools/cliutils/src/main/kotlin/net/corda/cliutils/InstallShellExtensionsParser.kt index 2759ca5700..6e44c65ec5 100644 --- a/tools/cliutils/src/main/kotlin/net/corda/cliutils/InstallShellExtensionsParser.kt +++ b/tools/cliutils/src/main/kotlin/net/corda/cliutils/InstallShellExtensionsParser.kt @@ -79,7 +79,7 @@ private class ShellExtensionsGenerator(val parent: CordaCliWrapper) { val autoCompleteFile = getAutoCompleteFileLocation(alias) autoCompleteFile.parent.createDirectories() val hierarchy = CommandLine(parent) - parent.subCommands().forEach { hierarchy.addSubcommand(it.alias, it)} + parent.subCommands.forEach { hierarchy.addSubcommand(it.alias, it)} val builder = StringBuilder(picocli.AutoComplete.bash(alias, hierarchy)) builder.append(jarVersion(alias))