From 4666e069102a4a1e14cc5fc7bf0e9cd126e8708c Mon Sep 17 00:00:00 2001 From: Eric Cochran Date: Wed, 6 Jun 2018 17:12:29 -0700 Subject: [PATCH] Allow private transient Kotlin properties. --- .../moshi/kotlin/codegen/TargetProperty.kt | 10 +++++----- .../kotlin/codgen/GeneratedAdaptersTest.kt | 19 ++++++++++++++----- .../kotlin/reflect/KotlinJsonAdapterTest.kt | 19 ++++++++++++++----- 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/TargetProperty.kt b/kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/TargetProperty.kt index 1f2a700..a0decdc 100644 --- a/kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/TargetProperty.kt +++ b/kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/TargetProperty.kt @@ -67,11 +67,6 @@ internal data class TargetProperty( * cannot be used with code gen, or if no codegen is necessary for this property. */ fun generator(messager: Messager): PropertyGenerator? { - if (!isVisible) { - messager.printMessage(Diagnostic.Kind.ERROR, "property ${this} is not visible", element) - return null - } - if (isTransient) { if (!hasDefault) { messager.printMessage( @@ -81,6 +76,11 @@ internal data class TargetProperty( return null // This property is transient and has a default value. Ignore it. } + if (!isVisible) { + messager.printMessage(Diagnostic.Kind.ERROR, "property ${this} is not visible", element) + return null + } + if (!isSettable) { return null // This property is not settable. Ignore it. } diff --git a/kotlin/tests/src/test/kotlin/com/squareup/moshi/kotlin/codgen/GeneratedAdaptersTest.kt b/kotlin/tests/src/test/kotlin/com/squareup/moshi/kotlin/codgen/GeneratedAdaptersTest.kt index da44b24..f302c3e 100644 --- a/kotlin/tests/src/test/kotlin/com/squareup/moshi/kotlin/codgen/GeneratedAdaptersTest.kt +++ b/kotlin/tests/src/test/kotlin/com/squareup/moshi/kotlin/codgen/GeneratedAdaptersTest.kt @@ -535,18 +535,27 @@ class GeneratedAdaptersTest { val encoded = TransientProperty() encoded.a = 3 - encoded.b = 5 - assertThat(jsonAdapter.toJson(encoded)).isEqualTo("""{"b":5}""") + encoded.setB(4) + encoded.c = 5 + assertThat(jsonAdapter.toJson(encoded)).isEqualTo("""{"c":5}""") - val decoded = jsonAdapter.fromJson("""{"a":4,"b":6}""")!! + val decoded = jsonAdapter.fromJson("""{"a":4,"b":5,"c":6}""")!! assertThat(decoded.a).isEqualTo(-1) - assertThat(decoded.b).isEqualTo(6) + assertThat(decoded.getB()).isEqualTo(-1) + assertThat(decoded.c).isEqualTo(6) } @JsonClass(generateAdapter = true) class TransientProperty { @Transient var a: Int = -1 - var b: Int = -1 + @Transient private var b: Int = -1 + var c: Int = -1 + + fun getB() = b + + fun setB(b: Int) { + this.b = b + } } @Test fun nonNullPropertySetToNullFailsWithJsonDataException() { diff --git a/kotlin/tests/src/test/kotlin/com/squareup/moshi/kotlin/reflect/KotlinJsonAdapterTest.kt b/kotlin/tests/src/test/kotlin/com/squareup/moshi/kotlin/reflect/KotlinJsonAdapterTest.kt index 7ee003a..87499f6 100644 --- a/kotlin/tests/src/test/kotlin/com/squareup/moshi/kotlin/reflect/KotlinJsonAdapterTest.kt +++ b/kotlin/tests/src/test/kotlin/com/squareup/moshi/kotlin/reflect/KotlinJsonAdapterTest.kt @@ -363,17 +363,26 @@ class KotlinJsonAdapterTest { val encoded = TransientProperty() encoded.a = 3 - encoded.b = 5 - assertThat(jsonAdapter.toJson(encoded)).isEqualTo("""{"b":5}""") + encoded.setB(4) + encoded.c = 5 + assertThat(jsonAdapter.toJson(encoded)).isEqualTo("""{"c":5}""") - val decoded = jsonAdapter.fromJson("""{"a":4,"b":6}""")!! + val decoded = jsonAdapter.fromJson("""{"a":4,"b":5,"c":6}""")!! assertThat(decoded.a).isEqualTo(-1) - assertThat(decoded.b).isEqualTo(6) + assertThat(decoded.getB()).isEqualTo(-1) + assertThat(decoded.c).isEqualTo(6) } class TransientProperty { @Transient var a: Int = -1 - var b: Int = -1 + @Transient private var b: Int = -1 + var c: Int = -1 + + fun getB() = b + + fun setB(b: Int) { + this.b = b + } } @Test fun constructorParametersAndPropertiesWithSameNamesMustHaveSameTypes() {