Fix safe args directions class generation
Since navigaiton is now is Kotlin, the safe args kotlin plugin needs to
override the property instead of getter methods since the getters don't
exist in kotlin.
RelNote: "Safe Args no longer crashes when attempting to generate
direction functions in kotlin with Navigation 2.4.0-alpha01"
Test: ./gradlew --rerun-tasks navigation:navigation-safe-args-gradle-plugin:test
Bug: 188564435
Change-Id: Id2416d7d7c52888fb9ec9bdbb80afcc7d2a1bf3e
diff --git a/navigation/navigation-safe-args-generator/src/main/kotlin/androidx/navigation/safe/args/generator/kotlin/KotlinNavWriter.kt b/navigation/navigation-safe-args-generator/src/main/kotlin/androidx/navigation/safe/args/generator/kotlin/KotlinNavWriter.kt
index 96140c9..bf91bd6 100644
--- a/navigation/navigation-safe-args-generator/src/main/kotlin/androidx/navigation/safe/args/generator/kotlin/KotlinNavWriter.kt
+++ b/navigation/navigation-safe-args-generator/src/main/kotlin/androidx/navigation/safe/args/generator/kotlin/KotlinNavWriter.kt
@@ -127,25 +127,30 @@
internal fun generateDirectionTypeSpec(action: Action): TypeSpec {
val className = ClassName("", action.id.javaIdentifier.toCamelCase())
- val getActionIdFunSpec = FunSpec.builder("getActionId")
- .addModifiers(KModifier.OVERRIDE)
- .returns(Int::class)
- .addStatement("return %L", action.id.accessor())
- .build()
+ val actionIdPropSpec =
+ PropertySpec.builder("actionId", Int::class, KModifier.OVERRIDE)
+ .initializer("%L", action.id.accessor()).build()
- val getArgumentsFunSpec = FunSpec.builder("getArguments").apply {
- addModifiers(KModifier.OVERRIDE)
- if (action.args.any { it.type is ObjectType }) {
- addAnnotation(CAST_NEVER_SUCCEEDS)
- }
- returns(BUNDLE_CLASSNAME)
- val resultVal = "result"
- addStatement("val %L = %T()", resultVal, BUNDLE_CLASSNAME)
- action.args.forEach { arg ->
- arg.type.addBundlePutStatement(this, arg, resultVal, "this.${arg.sanitizedName}")
- }
- addStatement("return %L", resultVal)
- }.build()
+ val argumentsPropSpec =
+ PropertySpec.builder("arguments", BUNDLE_CLASSNAME, KModifier.OVERRIDE)
+ .getter(
+ FunSpec.getterBuilder().apply {
+ if (action.args.any { it.type is ObjectType }) {
+ addAnnotation(CAST_NEVER_SUCCEEDS)
+ }
+ val resultVal = "result"
+ addStatement("val %L = %T()", resultVal, BUNDLE_CLASSNAME)
+ action.args.forEach { arg ->
+ arg.type.addBundlePutStatement(
+ this,
+ arg,
+ resultVal,
+ "this.${arg.sanitizedName}"
+ )
+ }
+ addStatement("return %L", resultVal)
+ }.build()
+ ).build()
val constructorFunSpec = FunSpec.constructorBuilder()
.addParameters(
@@ -178,8 +183,8 @@
)
}.addSuperinterface(NAV_DIRECTION_CLASSNAME)
.addModifiers(KModifier.PRIVATE)
- .addFunction(getActionIdFunSpec)
- .addFunction(getArgumentsFunSpec)
+ .addProperty(actionIdPropSpec)
+ .addProperty(argumentsPropSpec)
.build()
}
diff --git a/navigation/navigation-safe-args-generator/src/test/test-data/expected/kotlin_nav_writer_test/MainFragmentDirections.kt b/navigation/navigation-safe-args-generator/src/test/test-data/expected/kotlin_nav_writer_test/MainFragmentDirections.kt
index 70eb55c..474e7bf 100644
--- a/navigation/navigation-safe-args-generator/src/test/test-data/expected/kotlin_nav_writer_test/MainFragmentDirections.kt
+++ b/navigation/navigation-safe-args-generator/src/test/test-data/expected/kotlin_nav_writer_test/MainFragmentDirections.kt
@@ -11,14 +11,15 @@
public val main: String,
public val optional: String = "bla"
) : NavDirections {
- public override fun getActionId(): Int = R.id.next
+ public override val actionId: Int = R.id.next
- public override fun getArguments(): Bundle {
- val result = Bundle()
- result.putString("main", this.main)
- result.putString("optional", this.optional)
- return result
- }
+ public override val arguments: Bundle
+ get() {
+ val result = Bundle()
+ result.putString("main", this.main)
+ result.putString("optional", this.optional)
+ return result
+ }
}
public companion object {
diff --git a/navigation/navigation-safe-args-generator/src/test/test-data/expected/kotlin_nav_writer_test/Next.kt b/navigation/navigation-safe-args-generator/src/test/test-data/expected/kotlin_nav_writer_test/Next.kt
index 0358452..10a9e3c 100644
--- a/navigation/navigation-safe-args-generator/src/test/test-data/expected/kotlin_nav_writer_test/Next.kt
+++ b/navigation/navigation-safe-args-generator/src/test/test-data/expected/kotlin_nav_writer_test/Next.kt
@@ -19,36 +19,37 @@
public val parcelable: ActivityInfo,
public val innerData: ActivityInfo.WindowLayout
) : NavDirections {
- public override fun getActionId(): Int = R.id.next
+ public override val actionId: Int = R.id.next
- @Suppress("CAST_NEVER_SUCCEEDS")
- public override fun getArguments(): Bundle {
- val result = Bundle()
- result.putString("main", this.main)
- result.putInt("mainInt", this.mainInt)
- result.putString("optional", this.optional)
- result.putInt("optionalInt", this.optionalInt)
- if (Parcelable::class.java.isAssignableFrom(ActivityInfo::class.java)) {
- result.putParcelable("optionalParcelable", this.optionalParcelable as Parcelable?)
- } else if (Serializable::class.java.isAssignableFrom(ActivityInfo::class.java)) {
- result.putSerializable("optionalParcelable", this.optionalParcelable as Serializable?)
+ public override val arguments: Bundle
+ @Suppress("CAST_NEVER_SUCCEEDS")
+ get() {
+ val result = Bundle()
+ result.putString("main", this.main)
+ result.putInt("mainInt", this.mainInt)
+ result.putString("optional", this.optional)
+ result.putInt("optionalInt", this.optionalInt)
+ if (Parcelable::class.java.isAssignableFrom(ActivityInfo::class.java)) {
+ result.putParcelable("optionalParcelable", this.optionalParcelable as Parcelable?)
+ } else if (Serializable::class.java.isAssignableFrom(ActivityInfo::class.java)) {
+ result.putSerializable("optionalParcelable", this.optionalParcelable as Serializable?)
+ }
+ if (Parcelable::class.java.isAssignableFrom(ActivityInfo::class.java)) {
+ result.putParcelable("parcelable", this.parcelable as Parcelable)
+ } else if (Serializable::class.java.isAssignableFrom(ActivityInfo::class.java)) {
+ result.putSerializable("parcelable", this.parcelable as Serializable)
+ } else {
+ throw UnsupportedOperationException(ActivityInfo::class.java.name +
+ " must implement Parcelable or Serializable or must be an Enum.")
+ }
+ if (Parcelable::class.java.isAssignableFrom(ActivityInfo.WindowLayout::class.java)) {
+ result.putParcelable("innerData", this.innerData as Parcelable)
+ } else if (Serializable::class.java.isAssignableFrom(ActivityInfo.WindowLayout::class.java)) {
+ result.putSerializable("innerData", this.innerData as Serializable)
+ } else {
+ throw UnsupportedOperationException(ActivityInfo.WindowLayout::class.java.name +
+ " must implement Parcelable or Serializable or must be an Enum.")
+ }
+ return result
}
- if (Parcelable::class.java.isAssignableFrom(ActivityInfo::class.java)) {
- result.putParcelable("parcelable", this.parcelable as Parcelable)
- } else if (Serializable::class.java.isAssignableFrom(ActivityInfo::class.java)) {
- result.putSerializable("parcelable", this.parcelable as Serializable)
- } else {
- throw UnsupportedOperationException(ActivityInfo::class.java.name +
- " must implement Parcelable or Serializable or must be an Enum.")
- }
- if (Parcelable::class.java.isAssignableFrom(ActivityInfo.WindowLayout::class.java)) {
- result.putParcelable("innerData", this.innerData as Parcelable)
- } else if (Serializable::class.java.isAssignableFrom(ActivityInfo.WindowLayout::class.java)) {
- result.putSerializable("innerData", this.innerData as Serializable)
- } else {
- throw UnsupportedOperationException(ActivityInfo.WindowLayout::class.java.name +
- " must implement Parcelable or Serializable or must be an Enum.")
- }
- return result
- }
}
diff --git a/navigation/navigation-safe-args-generator/src/test/test-data/expected/nav_generator_test/kotlin/nested_overridden_action/InnerSettingsDirections.kt b/navigation/navigation-safe-args-generator/src/test/test-data/expected/nav_generator_test/kotlin/nested_overridden_action/InnerSettingsDirections.kt
index 1790afe..67a10c3 100644
--- a/navigation/navigation-safe-args-generator/src/test/test-data/expected/nav_generator_test/kotlin/nested_overridden_action/InnerSettingsDirections.kt
+++ b/navigation/navigation-safe-args-generator/src/test/test-data/expected/nav_generator_test/kotlin/nested_overridden_action/InnerSettingsDirections.kt
@@ -9,13 +9,14 @@
private data class Exit(
public val exitReason: Int
) : NavDirections {
- public override fun getActionId(): Int = R.id.exit
+ public override val actionId: Int = R.id.exit
- public override fun getArguments(): Bundle {
- val result = Bundle()
- result.putInt("exitReason", this.exitReason)
- return result
- }
+ public override val arguments: Bundle
+ get() {
+ val result = Bundle()
+ result.putInt("exitReason", this.exitReason)
+ return result
+ }
}
public companion object {
diff --git a/navigation/navigation-safe-args-generator/src/test/test-data/expected/nav_generator_test/kotlin/nested_overridden_action/SettingsDirections.kt b/navigation/navigation-safe-args-generator/src/test/test-data/expected/nav_generator_test/kotlin/nested_overridden_action/SettingsDirections.kt
index 25f9192..ad3d07a 100644
--- a/navigation/navigation-safe-args-generator/src/test/test-data/expected/nav_generator_test/kotlin/nested_overridden_action/SettingsDirections.kt
+++ b/navigation/navigation-safe-args-generator/src/test/test-data/expected/nav_generator_test/kotlin/nested_overridden_action/SettingsDirections.kt
@@ -9,25 +9,27 @@
private data class Main(
public val enterReason: String = "DEFAULT"
) : NavDirections {
- public override fun getActionId(): Int = R.id.main
+ public override val actionId: Int = R.id.main
- public override fun getArguments(): Bundle {
- val result = Bundle()
- result.putString("enterReason", this.enterReason)
- return result
- }
+ public override val arguments: Bundle
+ get() {
+ val result = Bundle()
+ result.putString("enterReason", this.enterReason)
+ return result
+ }
}
private data class Exit(
public val exitReason: String = "DEFAULT"
) : NavDirections {
- public override fun getActionId(): Int = R.id.exit
+ public override val actionId: Int = R.id.exit
- public override fun getArguments(): Bundle {
- val result = Bundle()
- result.putString("exitReason", this.exitReason)
- return result
- }
+ public override val arguments: Bundle
+ get() {
+ val result = Bundle()
+ result.putString("exitReason", this.exitReason)
+ return result
+ }
}
public companion object {
diff --git a/navigation/navigation-safe-args-generator/src/test/test-data/expected/nav_generator_test/kotlin/nested_same_action/SettingsDirections.kt b/navigation/navigation-safe-args-generator/src/test/test-data/expected/nav_generator_test/kotlin/nested_same_action/SettingsDirections.kt
index e8c8d17..181a8df 100644
--- a/navigation/navigation-safe-args-generator/src/test/test-data/expected/nav_generator_test/kotlin/nested_same_action/SettingsDirections.kt
+++ b/navigation/navigation-safe-args-generator/src/test/test-data/expected/nav_generator_test/kotlin/nested_same_action/SettingsDirections.kt
@@ -9,13 +9,14 @@
private data class Exit(
public val exitReason: String = "DEFAULT"
) : NavDirections {
- public override fun getActionId(): Int = R.id.exit
+ public override val actionId: Int = R.id.exit
- public override fun getArguments(): Bundle {
- val result = Bundle()
- result.putString("exitReason", this.exitReason)
- return result
- }
+ public override val arguments: Bundle
+ get() {
+ val result = Bundle()
+ result.putString("exitReason", this.exitReason)
+ return result
+ }
}
public companion object {
diff --git a/navigation/navigation-safe-args-generator/src/test/test-data/expected/nav_generator_test/kotlin/nested_same_action/SettingsFragmentDirections.kt b/navigation/navigation-safe-args-generator/src/test/test-data/expected/nav_generator_test/kotlin/nested_same_action/SettingsFragmentDirections.kt
index dee9a87..f566d27 100644
--- a/navigation/navigation-safe-args-generator/src/test/test-data/expected/nav_generator_test/kotlin/nested_same_action/SettingsFragmentDirections.kt
+++ b/navigation/navigation-safe-args-generator/src/test/test-data/expected/nav_generator_test/kotlin/nested_same_action/SettingsFragmentDirections.kt
@@ -10,13 +10,14 @@
private data class Exit(
public val exitReason: String = "DIFFERENT"
) : NavDirections {
- public override fun getActionId(): Int = R.id.exit
+ public override val actionId: Int = R.id.exit
- public override fun getArguments(): Bundle {
- val result = Bundle()
- result.putString("exitReason", this.exitReason)
- return result
- }
+ public override val arguments: Bundle
+ get() {
+ val result = Bundle()
+ result.putString("exitReason", this.exitReason)
+ return result
+ }
}
public companion object {