Minor: reformat code

This commit is contained in:
Dmitry Petrov
2018-02-09 16:22:31 +03:00
parent a790195808
commit af79bbd247
3 changed files with 169 additions and 127 deletions
@@ -23,7 +23,8 @@ import org.jetbrains.kotlin.config.JvmTarget
class K2JVMCompilerArguments : CommonCompilerArguments() { class K2JVMCompilerArguments : CommonCompilerArguments() {
companion object { companion object {
@JvmStatic private val serialVersionUID = 0L @JvmStatic
private val serialVersionUID = 0L
} }
@Argument(value = "-d", valueDescription = "<directory|jar>", description = "Destination for generated class files") @Argument(value = "-d", valueDescription = "<directory|jar>", description = "Destination for generated class files")
@@ -97,10 +98,16 @@ class K2JVMCompilerArguments : CommonCompilerArguments() {
@Argument(value = "-Xno-call-assertions", description = "Don't generate not-null assertions for arguments of platform types") @Argument(value = "-Xno-call-assertions", description = "Don't generate not-null assertions for arguments of platform types")
var noCallAssertions: Boolean by FreezableVar(false) var noCallAssertions: Boolean by FreezableVar(false)
@Argument(value = "-Xno-receiver-assertions", description = "Don't generate not-null assertion for extension receiver arguments of platform types") @Argument(
value = "-Xno-receiver-assertions",
description = "Don't generate not-null assertion for extension receiver arguments of platform types"
)
var noReceiverAssertions: Boolean by FreezableVar(false) var noReceiverAssertions: Boolean by FreezableVar(false)
@Argument(value = "-Xno-param-assertions", description = "Don't generate not-null assertions on parameters of methods accessible from Java") @Argument(
value = "-Xno-param-assertions",
description = "Don't generate not-null assertions on parameters of methods accessible from Java"
)
var noParamAssertions: Boolean by FreezableVar(false) var noParamAssertions: Boolean by FreezableVar(false)
@Argument(value = "-Xstrict-java-nullability-assertions", description = "Generate nullability assertions for non-null Java expressions") @Argument(value = "-Xstrict-java-nullability-assertions", description = "Generate nullability assertions for non-null Java expressions")
@@ -119,13 +126,21 @@ class K2JVMCompilerArguments : CommonCompilerArguments() {
@Argument(value = "-Xreport-perf", description = "Report detailed performance statistics") @Argument(value = "-Xreport-perf", description = "Report detailed performance statistics")
var reportPerf: Boolean by FreezableVar(false) var reportPerf: Boolean by FreezableVar(false)
@Argument(value = "-Xbuild-file", deprecatedName = "-module", valueDescription = "<path>", description = "Path to the .xml build file to compile") @Argument(
value = "-Xbuild-file",
deprecatedName = "-module",
valueDescription = "<path>",
description = "Path to the .xml build file to compile"
)
var buildFile: String? by FreezableVar(null) var buildFile: String? by FreezableVar(null)
@Argument(value = "-Xmultifile-parts-inherit", description = "Compile multifile classes as a hierarchy of parts and facade") @Argument(value = "-Xmultifile-parts-inherit", description = "Compile multifile classes as a hierarchy of parts and facade")
var inheritMultifileParts: Boolean by FreezableVar(false) var inheritMultifileParts: Boolean by FreezableVar(false)
@Argument(value = "-Xskip-runtime-version-check", description = "Allow Kotlin runtime libraries of incompatible versions in the classpath") @Argument(
value = "-Xskip-runtime-version-check",
description = "Allow Kotlin runtime libraries of incompatible versions in the classpath"
)
var skipRuntimeVersionCheck: Boolean by FreezableVar(false) var skipRuntimeVersionCheck: Boolean by FreezableVar(false)
@Argument( @Argument(
@@ -145,10 +160,16 @@ class K2JVMCompilerArguments : CommonCompilerArguments() {
@Argument(value = "-Xsingle-module", description = "Combine modules for source files and binary dependencies into a single module") @Argument(value = "-Xsingle-module", description = "Combine modules for source files and binary dependencies into a single module")
var singleModule: Boolean by FreezableVar(false) var singleModule: Boolean by FreezableVar(false)
@Argument(value = "-Xadd-compiler-builtins", description = "Add definitions of built-in declarations to the compilation classpath (useful with -no-stdlib)") @Argument(
value = "-Xadd-compiler-builtins",
description = "Add definitions of built-in declarations to the compilation classpath (useful with -no-stdlib)"
)
var addCompilerBuiltIns: Boolean by FreezableVar(false) var addCompilerBuiltIns: Boolean by FreezableVar(false)
@Argument(value = "-Xload-builtins-from-dependencies", description = "Load definitions of built-in declarations from module dependencies, instead of from the compiler") @Argument(
value = "-Xload-builtins-from-dependencies",
description = "Load definitions of built-in declarations from module dependencies, instead of from the compiler"
)
var loadBuiltInsFromDependencies: Boolean by FreezableVar(false) var loadBuiltInsFromDependencies: Boolean by FreezableVar(false)
@Argument( @Argument(
@@ -168,7 +189,8 @@ class K2JVMCompilerArguments : CommonCompilerArguments() {
@Argument( @Argument(
value = "-Xjavac-arguments", value = "-Xjavac-arguments",
valueDescription = "<option[,]>", valueDescription = "<option[,]>",
description = "Java compiler arguments") description = "Java compiler arguments"
)
var javacArguments: Array<String>? by FreezableVar(null) var javacArguments: Array<String>? by FreezableVar(null)
@Argument( @Argument(
@@ -83,8 +83,7 @@ class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
val file = File(arg) val file = File(arg)
if (file.extension == JavaFileType.DEFAULT_EXTENSION) { if (file.extension == JavaFileType.DEFAULT_EXTENSION) {
configuration.addJavaSourceRoot(file) configuration.addJavaSourceRoot(file)
} } else {
else {
configuration.addKotlinSourceRoot(arg) configuration.addKotlinSourceRoot(arg)
if (file.isDirectory) { if (file.isDirectory) {
configuration.addJavaSourceRoot(file) configuration.addJavaSourceRoot(file)
@@ -120,10 +119,11 @@ class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
val jvmTarget = JvmTarget.fromString(arguments.jvmTarget!!) val jvmTarget = JvmTarget.fromString(arguments.jvmTarget!!)
if (jvmTarget != null) { if (jvmTarget != null) {
configuration.put(JVMConfigurationKeys.JVM_TARGET, jvmTarget) configuration.put(JVMConfigurationKeys.JVM_TARGET, jvmTarget)
} } else {
else { messageCollector.report(
messageCollector.report(ERROR, "Unknown JVM target version: ${arguments.jvmTarget}\n" + ERROR, "Unknown JVM target version: ${arguments.jvmTarget}\n" +
"Supported versions: ${JvmTarget.values().joinToString { it.description }}") "Supported versions: ${JvmTarget.values().joinToString { it.description }}"
)
} }
} }
@@ -159,8 +159,7 @@ class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
} }
KotlinToJVMBytecodeCompiler.compileModules(environment, buildFile, moduleChunk.modules) KotlinToJVMBytecodeCompiler.compileModules(environment, buildFile, moduleChunk.modules)
} } else if (arguments.script) {
else if (arguments.script) {
val sourcePath = arguments.freeArgs.first() val sourcePath = arguments.freeArgs.first()
configuration.addKotlinSourceRoot(sourcePath) configuration.addKotlinSourceRoot(sourcePath)
@@ -181,13 +180,11 @@ class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
val scriptArgs = arguments.freeArgs.subList(1, arguments.freeArgs.size) val scriptArgs = arguments.freeArgs.subList(1, arguments.freeArgs.size)
return KotlinToJVMBytecodeCompiler.compileAndExecuteScript(environment, scriptArgs) return KotlinToJVMBytecodeCompiler.compileAndExecuteScript(environment, scriptArgs)
} } else {
else {
if (destination != null) { if (destination != null) {
if (destination.endsWith(".jar")) { if (destination.endsWith(".jar")) {
configuration.put(JVMConfigurationKeys.OUTPUT_JAR, File(destination)) configuration.put(JVMConfigurationKeys.OUTPUT_JAR, File(destination))
} } else {
else {
configuration.put(JVMConfigurationKeys.OUTPUT_DIRECTORY, File(destination)) configuration.put(JVMConfigurationKeys.OUTPUT_DIRECTORY, File(destination))
} }
} }
@@ -220,8 +217,7 @@ class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
PerformanceCounter.report { s -> reportPerf(configuration, s) } PerformanceCounter.report { s -> reportPerf(configuration, s) }
} }
return OK return OK
} } catch (e: CompilationException) {
catch (e: CompilationException) {
messageCollector.report( messageCollector.report(
EXCEPTION, EXCEPTION,
OutputMessageUtil.renderException(e), OutputMessageUtil.renderException(e),
@@ -242,8 +238,10 @@ class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
super.setupPlatformSpecificLanguageFeatureSettings(extraLanguageFeatures, commandLineArguments) super.setupPlatformSpecificLanguageFeatureSettings(extraLanguageFeatures, commandLineArguments)
} }
private fun registerJavacIfNeeded(environment: KotlinCoreEnvironment, private fun registerJavacIfNeeded(
arguments: K2JVMCompilerArguments): Boolean { environment: KotlinCoreEnvironment,
arguments: K2JVMCompilerArguments
): Boolean {
if (arguments.useJavac) { if (arguments.useJavac) {
environment.configuration.put(JVMConfigurationKeys.USE_JAVAC, true) environment.configuration.put(JVMConfigurationKeys.USE_JAVAC, true)
if (arguments.compileJava) { if (arguments.compileJava) {
@@ -255,8 +253,10 @@ class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
return true return true
} }
private fun compileJavaFilesIfNeeded(environment: KotlinCoreEnvironment, private fun compileJavaFilesIfNeeded(
arguments: K2JVMCompilerArguments): Boolean { environment: KotlinCoreEnvironment,
arguments: K2JVMCompilerArguments
): Boolean {
if (arguments.compileJava) { if (arguments.compileJava) {
return JavacWrapper.getInstance(environment.project).use { it.compile() } return JavacWrapper.getInstance(environment.project).use { it.compile() }
} }
@@ -335,7 +335,8 @@ class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
} }
} }
@JvmStatic fun main(args: Array<String>) { @JvmStatic
fun main(args: Array<String>) {
CLITool.doMain(K2JVMCompiler(), args) CLITool.doMain(K2JVMCompiler(), args)
} }
@@ -366,17 +367,25 @@ class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
configuration.put(JVMConfigurationKeys.DISABLE_CALL_ASSERTIONS, arguments.noCallAssertions) configuration.put(JVMConfigurationKeys.DISABLE_CALL_ASSERTIONS, arguments.noCallAssertions)
configuration.put(JVMConfigurationKeys.DISABLE_RECEIVER_ASSERTIONS, arguments.noReceiverAssertions) configuration.put(JVMConfigurationKeys.DISABLE_RECEIVER_ASSERTIONS, arguments.noReceiverAssertions)
configuration.put(JVMConfigurationKeys.DISABLE_PARAM_ASSERTIONS, arguments.noParamAssertions) configuration.put(JVMConfigurationKeys.DISABLE_PARAM_ASSERTIONS, arguments.noParamAssertions)
configuration.put(JVMConfigurationKeys.NO_EXCEPTION_ON_EXPLICIT_EQUALS_FOR_BOXED_NULL, arguments.noExceptionOnExplicitEqualsForBoxedNull) configuration.put(
JVMConfigurationKeys.NO_EXCEPTION_ON_EXPLICIT_EQUALS_FOR_BOXED_NULL,
arguments.noExceptionOnExplicitEqualsForBoxedNull
)
configuration.put(JVMConfigurationKeys.DISABLE_OPTIMIZATION, arguments.noOptimize) configuration.put(JVMConfigurationKeys.DISABLE_OPTIMIZATION, arguments.noOptimize)
val constructorCallNormalizationMode = JVMConstructorCallNormalizationMode.fromStringOrNull(arguments.constructorCallNormalizationMode) val constructorCallNormalizationMode =
JVMConstructorCallNormalizationMode.fromStringOrNull(arguments.constructorCallNormalizationMode)
if (constructorCallNormalizationMode == null) { if (constructorCallNormalizationMode == null) {
configuration.getNotNull(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY) configuration.getNotNull(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY).report(
.report(ERROR, "Unknown constructor call normalization mode: ${arguments.constructorCallNormalizationMode}, " + ERROR,
"supported modes: ${JVMConstructorCallNormalizationMode.values().map { it.description }}") "Unknown constructor call normalization mode: ${arguments.constructorCallNormalizationMode}, " +
"supported modes: ${JVMConstructorCallNormalizationMode.values().map { it.description }}"
)
} }
configuration.put(JVMConfigurationKeys.CONSTRUCTOR_CALL_NORMALIZATION_MODE, configuration.put(
constructorCallNormalizationMode ?: JVMConstructorCallNormalizationMode.DEFAULT) JVMConfigurationKeys.CONSTRUCTOR_CALL_NORMALIZATION_MODE,
constructorCallNormalizationMode ?: JVMConstructorCallNormalizationMode.DEFAULT
)
configuration.put(JVMConfigurationKeys.INHERIT_MULTIFILE_PARTS, arguments.inheritMultifileParts) configuration.put(JVMConfigurationKeys.INHERIT_MULTIFILE_PARTS, arguments.inheritMultifileParts)
configuration.put(JVMConfigurationKeys.SKIP_RUNTIME_VERSION_CHECK, arguments.skipRuntimeVersionCheck) configuration.put(JVMConfigurationKeys.SKIP_RUNTIME_VERSION_CHECK, arguments.skipRuntimeVersionCheck)
@@ -412,8 +421,7 @@ class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
if (isModularJava) { if (isModularJava) {
configuration.add(JVMConfigurationKeys.CONTENT_ROOTS, JvmModulePathRoot(file)) configuration.add(JVMConfigurationKeys.CONTENT_ROOTS, JvmModulePathRoot(file))
configuration.add(JVMConfigurationKeys.ADDITIONAL_JAVA_MODULES, moduleName) configuration.add(JVMConfigurationKeys.ADDITIONAL_JAVA_MODULES, moduleName)
} } else {
else {
configuration.add(JVMConfigurationKeys.CONTENT_ROOTS, JvmClasspathRoot(file)) configuration.add(JVMConfigurationKeys.CONTENT_ROOTS, JvmClasspathRoot(file))
} }
} }
@@ -429,7 +437,11 @@ class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
} }
} }
private fun configureJdkHome(arguments: K2JVMCompilerArguments, configuration: CompilerConfiguration, messageCollector: MessageCollector): ExitCode { private fun configureJdkHome(
arguments: K2JVMCompilerArguments,
configuration: CompilerConfiguration,
messageCollector: MessageCollector
): ExitCode {
if (arguments.noJdk) { if (arguments.noJdk) {
configuration.put(JVMConfigurationKeys.NO_JDK, true) configuration.put(JVMConfigurationKeys.NO_JDK, true)
@@ -454,14 +466,17 @@ class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
return OK return OK
} }
fun configureScriptDefinitions(scriptTemplates: Array<String>?, fun configureScriptDefinitions(
scriptTemplates: Array<String>?,
configuration: CompilerConfiguration, configuration: CompilerConfiguration,
messageCollector: MessageCollector, messageCollector: MessageCollector,
scriptResolverEnv: HashMap<String, Any?>) { scriptResolverEnv: HashMap<String, Any?>
) {
val classpath = configuration.jvmClasspathRoots val classpath = configuration.jvmClasspathRoots
// TODO: consider using escaping to allow kotlin escaped names in class names // TODO: consider using escaping to allow kotlin escaped names in class names
if (scriptTemplates != null && scriptTemplates.isNotEmpty()) { if (scriptTemplates != null && scriptTemplates.isNotEmpty()) {
val classloader = URLClassLoader(classpath.map { it.toURI().toURL() }.toTypedArray(), Thread.currentThread().contextClassLoader) val classloader =
URLClassLoader(classpath.map { it.toURI().toURL() }.toTypedArray(), Thread.currentThread().contextClassLoader)
var hasErrors = false var hasErrors = false
for (template in scriptTemplates) { for (template in scriptTemplates) {
try { try {
@@ -473,12 +488,10 @@ class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
"Added script definition $template to configuration: files pattern = \"${def.scriptFilePattern}\", " + "Added script definition $template to configuration: files pattern = \"${def.scriptFilePattern}\", " +
"resolver = ${def.dependencyResolver.javaClass.name}" "resolver = ${def.dependencyResolver.javaClass.name}"
) )
} } catch (ex: ClassNotFoundException) {
catch (ex: ClassNotFoundException) {
messageCollector.report(ERROR, "Cannot find script definition template class $template") messageCollector.report(ERROR, "Cannot find script definition template class $template")
hasErrors = true hasErrors = true
} } catch (ex: Exception) {
catch (ex: Exception) {
messageCollector.report(ERROR, "Error processing script definition template $template: ${ex.message}") messageCollector.report(ERROR, "Error processing script definition template $template: ${ex.message}")
hasErrors = true hasErrors = true
break break
@@ -507,7 +520,9 @@ class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
messageCollector.report(ERROR, "Unable to parse script-resolver-environment argument $envParam") messageCollector.report(ERROR, "Unable to parse script-resolver-environment argument $envParam")
return null return null
} }
scriptResolverEnv.put(match.groupValues[1], match.groupValues.drop(2).firstOrNull { it.isNotEmpty() }?.let { unescapeRe.replace(it, "\$1") }) scriptResolverEnv.put(
match.groupValues[1],
match.groupValues.drop(2).firstOrNull { it.isNotEmpty() }?.let { unescapeRe.replace(it, "\$1") })
} }
} }
return scriptResolverEnv return scriptResolverEnv
@@ -77,7 +77,11 @@ enum class LanguageFeature(
// Experimental features // Experimental features
Coroutines(KOTLIN_1_1, ApiVersion.KOTLIN_1_1, "https://kotlinlang.org/docs/diagnostics/experimental-coroutines", State.ENABLED_WITH_WARNING), Coroutines(
KOTLIN_1_1, ApiVersion.KOTLIN_1_1,
"https://kotlinlang.org/docs/diagnostics/experimental-coroutines",
State.ENABLED_WITH_WARNING
),
MultiPlatformProjects(sinceVersion = null, defaultState = State.DISABLED), MultiPlatformProjects(sinceVersion = null, defaultState = State.DISABLED),
@@ -126,7 +130,8 @@ enum class LanguageVersion(val major: Int, val minor: Int) : DescriptionAware {
fun fromVersionString(str: String?) = values().find { it.versionString == str } fun fromVersionString(str: String?) = values().find { it.versionString == str }
@JvmStatic @JvmStatic
fun fromFullVersionString(str: String) = str.split(".", "-").let { if (it.size >= 2) fromVersionString("${it[0]}.${it[1]}") else null } fun fromFullVersionString(str: String) =
str.split(".", "-").let { if (it.size >= 2) fromVersionString("${it[0]}.${it[1]}") else null }
@JvmField @JvmField
val LATEST_STABLE = KOTLIN_1_2 val LATEST_STABLE = KOTLIN_1_2