Merge pull request #651 from square/jwilson.0909.enclosed_parameterized

Call Types.newParameterizedTypeWithOwner when necessary.
This commit is contained in:
Jesse Wilson
2018-09-09 23:16:38 -04:00
committed by GitHub
2 changed files with 65 additions and 6 deletions

View File

@@ -66,12 +66,21 @@ abstract class TypeRenderer {
Types::class, Types::class,
render(typeName.typeArguments[0].objectType())) render(typeName.typeArguments[0].objectType()))
} else { } else {
val placeholders = typeName.typeArguments.joinToString(", ") { "%L" } val builder = CodeBlock.builder().apply {
CodeBlock.of( add("%T.", Types::class)
"%T.newParameterizedType(%T::class.java, $placeholders)", val enclosingClassName = typeName.rawType.enclosingClassName()
Types::class, if (enclosingClassName != null) {
typeName.rawType.objectType(), add("newParameterizedTypeWithOwner(%L, ", render(enclosingClassName))
*(typeName.typeArguments.map { render(it.objectType()) }.toTypedArray())) } else {
add("newParameterizedType(")
}
add("%T::class.java", typeName.rawType.objectType())
for (typeArgument in typeName.typeArguments) {
add(", %L", render(typeArgument.objectType()))
}
add(")")
}
builder.build()
} }
} }

View File

@@ -28,6 +28,7 @@ import com.squareup.moshi.ToJson
import com.squareup.moshi.Types import com.squareup.moshi.Types
import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThat
import org.intellij.lang.annotations.Language import org.intellij.lang.annotations.Language
import org.junit.Assert.assertNull
import org.junit.Assert.fail import org.junit.Assert.fail
import org.junit.Ignore import org.junit.Ignore
import org.junit.Test import org.junit.Test
@@ -924,6 +925,55 @@ class GeneratedAdaptersTest {
} }
} }
@Test fun propertyIsNothing() {
val moshi = Moshi.Builder()
.add(NothingAdapter())
.build()
val jsonAdapter = moshi.adapter(HasNothingProperty::class.java).serializeNulls()
val toJson = HasNothingProperty()
toJson.a = "1"
assertThat(jsonAdapter.toJson(toJson)).isEqualTo("""{"a":"1","b":null}""")
val fromJson = jsonAdapter.fromJson("""{"a":"3","b":null}""")!!
assertThat(fromJson.a).isEqualTo("3")
assertNull(fromJson.b)
}
class NothingAdapter {
@ToJson fun toJson(jsonWriter: JsonWriter, unused: Nothing?) {
jsonWriter.nullValue()
}
@FromJson fun fromJson(jsonReader: JsonReader) : Nothing? {
jsonReader.skipValue()
return null
}
}
@JsonClass(generateAdapter = true)
class HasNothingProperty {
var a: String? = null
var b: Nothing? = null
}
@Test fun enclosedParameterizedType() {
val jsonAdapter = moshi.adapter(HasParameterizedProperty::class.java)
assertThat(jsonAdapter.toJson(HasParameterizedProperty(Twins("1", "2"))))
.isEqualTo("""{"twins":{"a":"1","b":"2"}}""")
val hasParameterizedProperty = jsonAdapter.fromJson("""{"twins":{"a":"3","b":"4"}}""")!!
assertThat(hasParameterizedProperty.twins.a).isEqualTo("3")
assertThat(hasParameterizedProperty.twins.b).isEqualTo("4")
}
@JsonClass(generateAdapter = true)
class Twins<T>(var a: T, var b: T)
@JsonClass(generateAdapter = true)
class HasParameterizedProperty(val twins: Twins<String>)
@JsonQualifier @JsonQualifier
annotation class Uppercase(val inFrench: Boolean, val onSundays: Boolean = false) annotation class Uppercase(val inFrench: Boolean, val onSundays: Boolean = false)