Merge pull request #385 from square/eric.20171105.transient_constructor_error

Clarify error message for transient parameters.
This commit is contained in:
Jesse Wilson
2017-11-05 11:16:54 -05:00
committed by GitHub
2 changed files with 24 additions and 3 deletions

View File

@@ -164,9 +164,16 @@ class KotlinJsonAdapterFactory : JsonAdapter.Factory {
val bindingsByName = LinkedHashMap<String, KotlinJsonAdapter.Binding<Any, Any?>>()
for (property in rawType.kotlin.memberProperties) {
if (Modifier.isTransient(property.javaField?.modifiers ?: 0)) continue
val parameter = parametersByName[property.name]
if (Modifier.isTransient(property.javaField?.modifiers ?: 0)) {
if (parameter != null && !parameter.isOptional) {
throw IllegalArgumentException(
"No default value for transient constructor $parameter")
}
continue
}
if (property !is KMutableProperty1 && parameter == null) continue
property.isAccessible = true

View File

@@ -277,6 +277,20 @@ class KotlinJsonAdapterTest {
class TransientConstructorParameter(@Transient var a: Int = -1, var b: Int = -1)
@Test fun requiredTransientConstructorParameterFails() {
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
try {
moshi.adapter(RequiredTransientConstructorParameter::class.java)
fail()
} catch (expected: IllegalArgumentException) {
assertThat(expected).hasMessage("No default value for transient constructor parameter #0 " +
"a of fun <init>(kotlin.Int): " +
"com.squareup.moshi.KotlinJsonAdapterTest.RequiredTransientConstructorParameter")
}
}
class RequiredTransientConstructorParameter(@Transient var a: Int)
@Test fun transientProperty() {
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
val jsonAdapter = moshi.adapter(TransientProperty::class.java)
@@ -516,7 +530,7 @@ class KotlinJsonAdapterTest {
fail()
} catch(expected: IllegalArgumentException) {
assertThat(expected).hasMessage(
"No property for required constructor parameter #0 a of " + "fun <init>(" +
"No property for required constructor parameter #0 a of fun <init>(" +
"kotlin.Int, kotlin.Int): ${NonPropertyConstructorParameter::class.qualifiedName}")
}
}