[Java] Refactor code in JavaShellCommandSpecsProvider#addOptionsFromVM

IDEA-359564

GitOrigin-RevId: c63bab58cca29b542087a0f1ece7ee423ef23da9
This commit is contained in:
Georgii Ustinov
2024-09-27 09:13:46 +00:00
committed by intellij-monorepo-bot
parent 1be5f98a5b
commit a00b5d5ce2
3 changed files with 60 additions and 106 deletions
@@ -4,7 +4,7 @@ java.command.terminal.help.option.description=Print help message to the {0} stre
java.command.terminal.jar.option.description=Launch the main class in a JAR file.
java.command.terminal.jar.option.argument.jar.file.text=jar file
java.command.terminal.D.option.description=Set system property.
java.command.terminal.D.option.argument.key.text=<name>=<value>
java.command.terminal.D.option.argument.text=<name>=<value>
java.command.terminal.version.option.description=Print the product version to the {0} stream and exit.
java.command.terminal.classpath.option.description=Class search path of directories and zip/jar files.
java.command.terminal.classpath.option.argument.path.text=filepath[{0}filepath]
@@ -70,11 +70,18 @@ java.command.terminal.source.option.argument.text=version
java.command.terminal.disable.@files.option.description=Disable further argument file expansion.
java.command.terminal.illegal.access.option.description=\\&lt;value\\&gt permit or deny access to members of types in named modules by code in unnamed modules. \\&lt;value\\&gt; is one of "deny", "permit", "warn", or "debug" This option will be removed in a future release.
java.command.terminal.illegal.access.option.argument.text=value
java.command.terminal.disable.system.assertions.option.description=Disables assertions in all system classes.
java.command.terminal.disable.assertions.option.description=Disables assertions with specified granularity.
java.command.terminal.enable.system.assertions.option.description=Enables assertions in all system classes.
java.command.terminal.enable.assertions.option.description=Enables assertions with specified granularity.
java.command.terminal.assertions.option.argument.text=<package>|<class>
java.command.terminal.disablesystemassertions.option.description=Disables assertions in all system classes.
java.command.terminal.dsa.option.description=Disables assertions in all system classes.
java.command.terminal.disableassertions.option.description=Disables assertions with specified granularity.
java.command.terminal.disableassertions.option.argument.text=<package>|<class>
java.command.terminal.da.option.description=Disables assertions with specified granularity.
java.command.terminal.da.option.argument.text=<package>|<class>
java.command.terminal.enablesystemassertions.option.description=Enables assertions in all system classes.
java.command.terminal.esa.option.description=Enables assertions in all system classes.
java.command.terminal.enableassertions.option.description=Enables assertions with specified granularity.
java.command.terminal.enableassertions.option.argument.text=<package>|<class>
java.command.terminal.ea.option.description=Enables assertions with specified granularity.
java.command.terminal.ea.option.argument.text=<package>|<class>
java.command.terminal.enable.preview.option.description=Allows classes to depend on preview features of the release.
java.command.terminal.verbose.option.description=Enables verbose output for the given subsystem.
java.command.terminal.verbose.option.argument.text.8=class|gc|gni
@@ -83,7 +90,8 @@ java.command.terminal.agentlib.option.description=Loads native agent library \\&
java.command.terminal.agentlib.option.argument.text=<libname>[=<options>]
java.command.terminal.agentpath.option.description=Loads native agent library by full \\&lt;pathname\\&gt;.
java.command.terminal.agentpath.option.argument.text=<pathname>[=<options>]
java.command.terminal.javaagent.option.description=Loads Java programming language agent by \\&ltjarpath\\&gt;.
java.command.terminal.javaagent.option.description=Loads Java programming language agent by \\&lt;jarpath\\&gt;.
java.command.terminal.javaagent.option.argument.text=<jarpath>[=<options>]
java.command.terminal.XX.option.description=Specify non-standard JVM-specific option.
error.stream.name=error
output.stream.name=output
@@ -19,9 +19,9 @@ class JavaShellCommandSpecsProvider : ShellCommandSpecsProvider {
private fun getSpecs(): ShellCommandSpec = ShellCommandSpec("java") {
dynamicOptions { terminalContext ->
val javaContext = JavaShellCommandContext.create(terminalContext) ?: return@dynamicOptions
addOptionsFromVM(javaContext.getJrePath())
val version = javaContext.getJavaVersion() ?: return@dynamicOptions
val javaContext = JavaShellCommandContext.create(terminalContext)
addOptionsFromVM(javaContext?.getJrePath())
val version = javaContext?.getJavaVersion() ?: return@dynamicOptions
if (version.isAtLeast(11)) {
addOptionsFromJava11()
} else if (version.isAtLeast(8)) {
@@ -42,14 +42,6 @@ class JavaShellCommandSpecsProvider : ShellCommandSpecsProvider {
}
description(JavaTerminalBundle.message("java.command.terminal.jar.option.description"))
}
option("-D") {
description(JavaTerminalBundle.message("java.command.terminal.D.option.description"))
repeatTimes = 0
separator = ""
argument {
displayName(JavaTerminalBundle.message("java.command.terminal.D.option.argument.key.text"))
}
}
option("-version") {
exclusiveOn = listOf("--version")
description(JavaTerminalBundle.message("java.command.terminal.version.option.description", errorStreamName))
@@ -66,58 +58,6 @@ class JavaShellCommandSpecsProvider : ShellCommandSpecsProvider {
exclusiveOn = listOf("--show-version")
description(JavaTerminalBundle.message("java.command.terminal.show.version.option.description", errorStreamName))
}
option("-dsa", "-enablesystemassertions") {
description(JavaTerminalBundle.message("java.command.terminal.disable.system.assertions.option.description"))
}
option("-esa", "-disablesystemassertions") {
description(JavaTerminalBundle.message("java.command.terminal.enable.system.assertions.option.description"))
}
option("-ea", "-enableassertions") {
repeatTimes = 0
separator = ":"
description(JavaTerminalBundle.message("java.command.terminal.enable.assertions.option.description"))
argument {
displayName(JavaTerminalBundle.message("java.command.terminal.assertions.option.argument.text"))
isOptional = true
}
}
option("-da", "-disableassertions") {
repeatTimes = 0
description(JavaTerminalBundle.message("java.command.terminal.disable.assertions.option.description"))
separator = ":"
argument {
displayName(JavaTerminalBundle.message("java.command.terminal.assertions.option.argument.text"))
isOptional = true
}
}
option("-agentlib") {
description(JavaTerminalBundle.message("java.command.terminal.agentlib.option.description"))
separator = ":"
repeatTimes = 0
argument {
displayName(JavaTerminalBundle.message("java.command.terminal.agentlib.option.argument.text"))
}
}
option("-agentpath") {
description(JavaTerminalBundle.message("java.command.terminal.agentpath.option.description"))
separator = ":"
repeatTimes = 0
argument {
displayName(JavaTerminalBundle.message("java.command.terminal.agentpath.option.argument.text"))
}
}
option("-javaagent") {
description(JavaTerminalBundle.message("java.command.terminal.javaagent.option.description"))
separator = ":"
repeatTimes = 0
argument {
displayName(JavaTerminalBundle.message("java.command.terminal.agentpath.option.argument.text"))
}
}
argument {
displayName(JavaTerminalBundle.message("java.command.terminal.argument.main.class.text"))
suggestions(ShellDataGenerators.fileSuggestionsGenerator())
@@ -125,13 +65,13 @@ class JavaShellCommandSpecsProvider : ShellCommandSpecsProvider {
}
private suspend fun ShellChildOptionsContext.addOptionsFromVM(path: String?) {
if (path == null) return
val optionsService = VMOptionsService.getInstance()
val jdkOptionsData = withContext(Dispatchers.IO) {
VMOptionsService.getInstance().getOrComputeOptionsForJdk(path).get() ?: return@withContext null
} ?: return
if (path == null) return@withContext null
optionsService.getOrComputeOptionsForJdk(path).get()
} ?: optionsService.getStandardOptions()
jdkOptionsData.options
.filter { (it.kind == VMOptionKind.Standard || it.kind == VMOptionKind.Product) &&
(it.variant == VMOptionVariant.X || it.variant == VMOptionVariant.DASH_DASH)}
.filter { (it.kind == VMOptionKind.Standard || it.kind == VMOptionKind.Product) && (it.variant != VMOptionVariant.XX)}
.toList()
.forEach {
val optionName = it.optionName
@@ -142,18 +82,13 @@ class JavaShellCommandSpecsProvider : ShellCommandSpecsProvider {
return@option
}
description(JavaTerminalBundle.message(getOptionBundleKey(optionName)))
if (!JavaTerminalBundle.isMessageInBundle(getOptionArgumentBundleKey(optionName))) return@option
if(it.variant == VMOptionVariant.DASH_DASH) {
if (KNOWN_REPETITIVE_OPTIONS.contains(presentableName)) repeatTimes = 0
if (KNOWN_OPTIONS_WITH_EMPTY_SEPARATOR.contains(presentableName)) separator = ""
} else if (it.variant == VMOptionVariant.X) {
if (!KNOWN_X_OPTIONS_WITH_ARGUMENT.contains(presentableName)) return@option
separator = it.variant.suffix()?.toString() ?: ""
}
val info = OPTION_UI_INFO_MAP[presentableName] ?: DEFAULT_UI_OPTION_INSTANCE
repeatTimes = info.repeatTimes
separator = info.separator
argument {
isOptional = info.isArgumentOptional
displayName(JavaTerminalBundle.message(getOptionArgumentBundleKey(optionName)))
}
}
@@ -219,29 +154,36 @@ class JavaShellCommandSpecsProvider : ShellCommandSpecsProvider {
private fun getCanonicalOptionName(option: String): String = option.replace(Regex("[:|\\-]"), ".").trim('=', '.')
}
private val KNOWN_X_OPTIONS_WITH_ARGUMENT = setOf(
"-Xms",
"-Xmx",
"-Xmn",
"-Xss",
"-Xbootclasspath:",
"-Xbootclasspath/a:",
"-Xbootclasspath/p:",
"-Xlog:",
"-Xloggc:",
private val OPTION_UI_INFO_MAP = mapOf(
"-Xms" to UIOptionInfo(separator = ""),
"-Xmx" to UIOptionInfo(separator = ""),
"-Xmn" to UIOptionInfo(separator = ""),
"-Xss" to UIOptionInfo(separator = ""),
"-Xbootclasspath:" to UIOptionInfo(separator = ""),
"-Xbootclasspath/a:" to UIOptionInfo(separator = ""),
"-Xbootclasspath/p:" to UIOptionInfo(separator = ""),
"-Xlog:" to UIOptionInfo(separator = ""),
"-Xloggc:" to UIOptionInfo(separator = ""),
"--add-opens" to UIOptionInfo(repeatTimes = 0),
"--patch-module" to UIOptionInfo(repeatTimes = 0),
"--limit-modules" to UIOptionInfo(repeatTimes = 0),
"--add-reads" to UIOptionInfo(repeatTimes = 0),
"--add-exports" to UIOptionInfo(repeatTimes = 0),
"--finalization=" to UIOptionInfo(separator = ""),
"--illegal-access=" to UIOptionInfo(separator = ""),
"-ea" to UIOptionInfo(separator = ":", isArgumentOptional = true),
"-da" to UIOptionInfo(separator = ":", isArgumentOptional = true),
"-enableassertions" to UIOptionInfo(separator = ":", isArgumentOptional = true),
"-disableassertions" to UIOptionInfo(separator = ":", isArgumentOptional = true),
"-agentlib:" to UIOptionInfo(separator = ""),
"-agentpath:" to UIOptionInfo(separator = ""),
"-javaagent:" to UIOptionInfo(separator = ""),
"-D" to UIOptionInfo(separator = "", repeatTimes = 0),
"-XX:" to UIOptionInfo(repeatTimes = 0)
)
private val KNOWN_OPTIONS_WITH_EMPTY_SEPARATOR = setOf(
"--finalization=",
"--illegal-access="
)
private val KNOWN_REPETITIVE_OPTIONS = setOf(
"--add-opens",
"--patch-module",
"--limit-modules",
"--add-reads",
"--add-exports",
)
private data class UIOptionInfo(val separator: String? = null, val repeatTimes: Int = 1, val isArgumentOptional: Boolean = false)
private val DEFAULT_UI_OPTION_INSTANCE = UIOptionInfo()
private val LOG = Logger.getInstance(JavaShellCommandSpecsProvider::class.java)