Skip to content

Commit

Permalink
Change 'jvmTarget' backing property visibility to internal.
Browse files Browse the repository at this point in the history
Allows to check if this property was updated by the user or not.

^KT-45611 In Progress
^KT-43095 In Progress
  • Loading branch information
Tapchicoma authored and Space committed Jun 25, 2021
1 parent 48d170a commit 430306b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
package org.jetbrains.kotlin.cli.common.arguments

import kotlin.reflect.KClass
import kotlin.reflect.KVisibility

@Retention(AnnotationRetention.RUNTIME)
annotation class GradleOption(val value: KClass<out DefaultValues> = DefaultValues::class)
annotation class GradleOption(
val value: KClass<out DefaultValues> = DefaultValues::class,
val backingFieldVisibility: KVisibility = KVisibility.PRIVATE
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package org.jetbrains.kotlin.cli.common.arguments
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import org.jetbrains.kotlin.config.*
import kotlin.reflect.KVisibility

class K2JVMCompilerArguments : CommonCompilerArguments() {
companion object {
Expand Down Expand Up @@ -75,7 +76,10 @@ class K2JVMCompilerArguments : CommonCompilerArguments() {
@Argument(value = "-module-name", valueDescription = "<name>", description = "Name of the generated .kotlin_module file")
var moduleName: String? by NullableStringFreezableVar(null)

@GradleOption(DefaultValues.JvmTargetVersions::class)
@GradleOption(
value = DefaultValues.JvmTargetVersions::class,
backingFieldVisibility = KVisibility.INTERNAL
)
@Argument(
value = "-jvm-target",
valueDescription = "<version>",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.utils.Printer
import java.io.File
import java.io.PrintStream
import java.util.*
import kotlin.reflect.KAnnotatedElement
import kotlin.reflect.KProperty1
import kotlin.reflect.KVisibility
import kotlin.reflect.full.declaredMemberProperties
import kotlin.reflect.full.withNullability

Expand Down Expand Up @@ -203,7 +205,8 @@ private fun Printer.generateImpl(
generatePropertyDeclaration(property, modifiers = "override", value = "null")
} else {
val backingField = property.backingField()
println("private var $backingField: $propertyType? = null")
val visibilityModified = property.gradleBackingFieldVisibility.name.lowercase(Locale.US)
println("$visibilityModified var $backingField: $propertyType? = null")
generatePropertyDeclaration(property, modifiers = "override")
withIndent {
println("get() = $backingField ?: ${property.gradleDefaultValue}")
Expand Down Expand Up @@ -314,6 +317,15 @@ private val KProperty1<*, *>.gradleValues: DefaultValues
private val KProperty1<*, *>.gradleDefaultValue: String
get() = gradleValues.defaultValue

private val KProperty1<*, *>.gradleBackingFieldVisibility: KVisibility
get() {
val fieldVisibility = findAnnotation<GradleOption>()!!.backingFieldVisibility
require(fieldVisibility != KVisibility.PUBLIC) {
"Backing field should not have public visibility!"
}
return fieldVisibility
}

private val KProperty1<*, *>.gradleReturnType: String
get() {
// Set nullability based on Gradle default value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ internal abstract class KotlinJvmOptionsBase : org.jetbrains.kotlin.gradle.dsl.K

override var jdkHome: kotlin.String? = null

private var jvmTargetField: kotlin.String? = null
internal var jvmTargetField: kotlin.String? = null
override var jvmTarget: kotlin.String
get() = jvmTargetField ?: "1.8"
set(value) {
Expand Down

0 comments on commit 430306b

Please sign in to comment.