Crash earlier for property type conflicts. (#377)

This commit is contained in:
Eric Cochran 2018-02-17 19:51:02 -08:00 committed by Jesse Wilson
parent a00860ee1d
commit 834a401122
2 changed files with 20 additions and 0 deletions

View file

@ -206,6 +206,11 @@ class KotlinJsonAdapterFactory : JsonAdapter.Factory {
continue
}
if (parameter != null && parameter.type != property.returnType) {
throw IllegalArgumentException("'${property.name}' has a constructor parameter of type " +
"${parameter.type} but a property of type ${property.returnType}.")
}
if (property !is KMutableProperty1 && parameter == null) continue
property.isAccessible = true

View file

@ -346,6 +346,21 @@ class KotlinJsonAdapterTest {
var b: Int = -1
}
@Test fun constructorParametersAndPropertiesWithSameNamesMustHaveSameTypes() {
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
try {
moshi.adapter(ConstructorParameterWithSameNameAsPropertyButDifferentType::class.java)
fail()
} catch (expected: IllegalArgumentException) {
assertThat(expected).hasMessage("'a' has a constructor parameter of type " +
"kotlin.Int but a property of type kotlin.String.")
}
}
class ConstructorParameterWithSameNameAsPropertyButDifferentType(a: Int) {
var a = "boo"
}
@Test fun supertypeConstructorParameters() {
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
val jsonAdapter = moshi.adapter(SubtypeConstructorParameters::class.java)