mirror of
https://github.com/fankes/moshi.git
synced 2025-10-20 00:19:21 +08:00
Fix Factory visibility (#282)
* Fix Factory visibility * Remove redundant constructor keyword
This commit is contained in:
committed by
Jesse Wilson
parent
448a2d3298
commit
e76110b4b1
@@ -16,6 +16,7 @@
|
|||||||
package com.squareup.moshi
|
package com.squareup.moshi
|
||||||
|
|
||||||
import java.lang.reflect.Modifier
|
import java.lang.reflect.Modifier
|
||||||
|
import java.lang.reflect.Type
|
||||||
import java.util.AbstractMap.SimpleEntry
|
import java.util.AbstractMap.SimpleEntry
|
||||||
import kotlin.collections.Map.Entry
|
import kotlin.collections.Map.Entry
|
||||||
import kotlin.reflect.KFunction
|
import kotlin.reflect.KFunction
|
||||||
@@ -42,7 +43,7 @@ private object ABSENT_VALUE
|
|||||||
* This class encodes Kotlin classes using their properties. It decodes them by first invoking the
|
* This class encodes Kotlin classes using their properties. It decodes them by first invoking the
|
||||||
* constructor, and then by setting any additional properties that exist, if any.
|
* constructor, and then by setting any additional properties that exist, if any.
|
||||||
*/
|
*/
|
||||||
internal class KotlinJsonAdapter<T> private constructor(
|
internal class KotlinJsonAdapter<T>(
|
||||||
val constructor: KFunction<T>,
|
val constructor: KFunction<T>,
|
||||||
val bindings: List<Binding<T, Any?>?>,
|
val bindings: List<Binding<T, Any?>?>,
|
||||||
val options: JsonReader.Options) : JsonAdapter<T>() {
|
val options: JsonReader.Options) : JsonAdapter<T>() {
|
||||||
@@ -148,22 +149,23 @@ internal class KotlinJsonAdapter<T> private constructor(
|
|||||||
return if (value !== ABSENT_VALUE) value else null
|
return if (value !== ABSENT_VALUE) value else null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
object KotlinJsonAdapterFactory : JsonAdapter.Factory {
|
||||||
@JvmField val FACTORY = Factory { type, annotations, moshi ->
|
override fun create(type: Type?, annotations: MutableSet<out Annotation>, moshi: Moshi): JsonAdapter<*>? {
|
||||||
if (!annotations.isEmpty()) return@Factory null
|
if (!annotations.isEmpty()) return null
|
||||||
|
|
||||||
val rawType = Types.getRawType(type)
|
val rawType = Types.getRawType(type)
|
||||||
val platformType = ClassJsonAdapter.isPlatformType(rawType)
|
val platformType = ClassJsonAdapter.isPlatformType(rawType)
|
||||||
if (platformType) return@Factory null
|
if (platformType) return null
|
||||||
|
|
||||||
if (!rawType.isAnnotationPresent(KOTLIN_METADATA)) return@Factory null
|
if (!rawType.isAnnotationPresent(KOTLIN_METADATA)) return null
|
||||||
|
|
||||||
val constructor = rawType.kotlin.primaryConstructor ?: return@Factory null
|
val constructor = rawType.kotlin.primaryConstructor ?: return null
|
||||||
val parametersByName = constructor.parameters.associateBy { it.name }
|
val parametersByName = constructor.parameters.associateBy { it.name }
|
||||||
constructor.isAccessible = true
|
constructor.isAccessible = true
|
||||||
|
|
||||||
val bindingsByName = LinkedHashMap<String, Binding<Any, Any?>>()
|
val bindingsByName = LinkedHashMap<String, KotlinJsonAdapter.Binding<Any, Any?>>()
|
||||||
|
|
||||||
for (property in rawType.kotlin.memberProperties) {
|
for (property in rawType.kotlin.memberProperties) {
|
||||||
if (Modifier.isTransient(property.javaField?.modifiers ?: 0)) continue
|
if (Modifier.isTransient(property.javaField?.modifiers ?: 0)) continue
|
||||||
@@ -185,10 +187,10 @@ internal class KotlinJsonAdapter<T> private constructor(
|
|||||||
property.returnType.javaType, Util.jsonAnnotations(allAnnotations.toTypedArray()))
|
property.returnType.javaType, Util.jsonAnnotations(allAnnotations.toTypedArray()))
|
||||||
|
|
||||||
bindingsByName[property.name] =
|
bindingsByName[property.name] =
|
||||||
Binding(name, adapter, property as KProperty1<Any, Any?>, parameter)
|
KotlinJsonAdapter.Binding(name, adapter, property as KProperty1<Any, Any?>, parameter)
|
||||||
}
|
}
|
||||||
|
|
||||||
val bindings = ArrayList<Binding<Any, Any?>?>()
|
val bindings = ArrayList<KotlinJsonAdapter.Binding<Any, Any?>?>()
|
||||||
|
|
||||||
for (parameter in constructor.parameters) {
|
for (parameter in constructor.parameters) {
|
||||||
val binding = bindingsByName.remove(parameter.name)
|
val binding = bindingsByName.remove(parameter.name)
|
||||||
@@ -201,7 +203,6 @@ internal class KotlinJsonAdapter<T> private constructor(
|
|||||||
bindings += bindingsByName.values
|
bindings += bindingsByName.values
|
||||||
|
|
||||||
val options = JsonReader.Options.of(*bindings.map { it?.name ?: "\u0000" }.toTypedArray())
|
val options = JsonReader.Options.of(*bindings.map { it?.name ?: "\u0000" }.toTypedArray())
|
||||||
KotlinJsonAdapter(constructor, bindings, options)
|
return KotlinJsonAdapter(constructor, bindings, options)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -25,7 +25,7 @@ import kotlin.annotation.AnnotationRetention.RUNTIME
|
|||||||
|
|
||||||
class KotlinJsonAdapterTest {
|
class KotlinJsonAdapterTest {
|
||||||
@Test fun constructorParameters() {
|
@Test fun constructorParameters() {
|
||||||
val moshi = Moshi.Builder().add(KotlinJsonAdapter.FACTORY).build()
|
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory).build()
|
||||||
val jsonAdapter = moshi.adapter(ConstructorParameters::class.java)
|
val jsonAdapter = moshi.adapter(ConstructorParameters::class.java)
|
||||||
|
|
||||||
val encoded = ConstructorParameters(3, 5)
|
val encoded = ConstructorParameters(3, 5)
|
||||||
@@ -39,7 +39,7 @@ class KotlinJsonAdapterTest {
|
|||||||
class ConstructorParameters(var a: Int, var b: Int)
|
class ConstructorParameters(var a: Int, var b: Int)
|
||||||
|
|
||||||
@Test fun properties() {
|
@Test fun properties() {
|
||||||
val moshi = Moshi.Builder().add(KotlinJsonAdapter.FACTORY).build()
|
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory).build()
|
||||||
val jsonAdapter = moshi.adapter(Properties::class.java)
|
val jsonAdapter = moshi.adapter(Properties::class.java)
|
||||||
|
|
||||||
val encoded = Properties()
|
val encoded = Properties()
|
||||||
@@ -58,7 +58,7 @@ class KotlinJsonAdapterTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test fun constructorParametersAndProperties() {
|
@Test fun constructorParametersAndProperties() {
|
||||||
val moshi = Moshi.Builder().add(KotlinJsonAdapter.FACTORY).build()
|
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory).build()
|
||||||
val jsonAdapter = moshi.adapter(ConstructorParametersAndProperties::class.java)
|
val jsonAdapter = moshi.adapter(ConstructorParametersAndProperties::class.java)
|
||||||
|
|
||||||
val encoded = ConstructorParametersAndProperties(3)
|
val encoded = ConstructorParametersAndProperties(3)
|
||||||
@@ -75,7 +75,7 @@ class KotlinJsonAdapterTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test fun immutableConstructorParameters() {
|
@Test fun immutableConstructorParameters() {
|
||||||
val moshi = Moshi.Builder().add(KotlinJsonAdapter.FACTORY).build()
|
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory).build()
|
||||||
val jsonAdapter = moshi.adapter(ImmutableConstructorParameters::class.java)
|
val jsonAdapter = moshi.adapter(ImmutableConstructorParameters::class.java)
|
||||||
|
|
||||||
val encoded = ImmutableConstructorParameters(3, 5)
|
val encoded = ImmutableConstructorParameters(3, 5)
|
||||||
@@ -89,7 +89,7 @@ class KotlinJsonAdapterTest {
|
|||||||
class ImmutableConstructorParameters(val a: Int, val b: Int)
|
class ImmutableConstructorParameters(val a: Int, val b: Int)
|
||||||
|
|
||||||
@Test fun immutableProperties() {
|
@Test fun immutableProperties() {
|
||||||
val moshi = Moshi.Builder().add(KotlinJsonAdapter.FACTORY).build()
|
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory).build()
|
||||||
val jsonAdapter = moshi.adapter(ImmutableProperties::class.java)
|
val jsonAdapter = moshi.adapter(ImmutableProperties::class.java)
|
||||||
|
|
||||||
val encoded = ImmutableProperties(3, 5)
|
val encoded = ImmutableProperties(3, 5)
|
||||||
@@ -106,7 +106,7 @@ class KotlinJsonAdapterTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test fun constructorDefaults() {
|
@Test fun constructorDefaults() {
|
||||||
val moshi = Moshi.Builder().add(KotlinJsonAdapter.FACTORY).build()
|
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory).build()
|
||||||
val jsonAdapter = moshi.adapter(ConstructorDefaultValues::class.java)
|
val jsonAdapter = moshi.adapter(ConstructorDefaultValues::class.java)
|
||||||
|
|
||||||
val encoded = ConstructorDefaultValues(3, 5)
|
val encoded = ConstructorDefaultValues(3, 5)
|
||||||
@@ -120,7 +120,7 @@ class KotlinJsonAdapterTest {
|
|||||||
class ConstructorDefaultValues(var a: Int = -1, var b: Int = -2)
|
class ConstructorDefaultValues(var a: Int = -1, var b: Int = -2)
|
||||||
|
|
||||||
@Test fun requiredValueAbsent() {
|
@Test fun requiredValueAbsent() {
|
||||||
val moshi = Moshi.Builder().add(KotlinJsonAdapter.FACTORY).build()
|
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory).build()
|
||||||
val jsonAdapter = moshi.adapter(RequiredValueAbsent::class.java)
|
val jsonAdapter = moshi.adapter(RequiredValueAbsent::class.java)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -134,7 +134,7 @@ class KotlinJsonAdapterTest {
|
|||||||
class RequiredValueAbsent(var a: Int = 3, var b: Int)
|
class RequiredValueAbsent(var a: Int = 3, var b: Int)
|
||||||
|
|
||||||
@Test fun duplicatedValue() {
|
@Test fun duplicatedValue() {
|
||||||
val moshi = Moshi.Builder().add(KotlinJsonAdapter.FACTORY).build()
|
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory).build()
|
||||||
val jsonAdapter = moshi.adapter(DuplicateValue::class.java)
|
val jsonAdapter = moshi.adapter(DuplicateValue::class.java)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -148,7 +148,7 @@ class KotlinJsonAdapterTest {
|
|||||||
class DuplicateValue(var a: Int = -1, var b: Int = -2)
|
class DuplicateValue(var a: Int = -1, var b: Int = -2)
|
||||||
|
|
||||||
@Test fun explicitNull() {
|
@Test fun explicitNull() {
|
||||||
val moshi = Moshi.Builder().add(KotlinJsonAdapter.FACTORY).build()
|
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory).build()
|
||||||
val jsonAdapter = moshi.adapter(ExplicitNull::class.java)
|
val jsonAdapter = moshi.adapter(ExplicitNull::class.java)
|
||||||
|
|
||||||
val encoded = ExplicitNull(null, 5)
|
val encoded = ExplicitNull(null, 5)
|
||||||
@@ -163,7 +163,7 @@ class KotlinJsonAdapterTest {
|
|||||||
class ExplicitNull(var a: Int?, var b: Int?)
|
class ExplicitNull(var a: Int?, var b: Int?)
|
||||||
|
|
||||||
@Test fun absentNull() {
|
@Test fun absentNull() {
|
||||||
val moshi = Moshi.Builder().add(KotlinJsonAdapter.FACTORY).build()
|
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory).build()
|
||||||
val jsonAdapter = moshi.adapter(AbsentNull::class.java)
|
val jsonAdapter = moshi.adapter(AbsentNull::class.java)
|
||||||
|
|
||||||
val encoded = AbsentNull(null, 5)
|
val encoded = AbsentNull(null, 5)
|
||||||
@@ -193,7 +193,7 @@ class KotlinJsonAdapterTest {
|
|||||||
|
|
||||||
@Test fun constructorParameterWithQualifier() {
|
@Test fun constructorParameterWithQualifier() {
|
||||||
val moshi = Moshi.Builder()
|
val moshi = Moshi.Builder()
|
||||||
.add(KotlinJsonAdapter.FACTORY)
|
.add(KotlinJsonAdapterFactory)
|
||||||
.add(UppercaseJsonAdapter())
|
.add(UppercaseJsonAdapter())
|
||||||
.build()
|
.build()
|
||||||
val jsonAdapter = moshi.adapter(ConstructorParameterWithQualifier::class.java)
|
val jsonAdapter = moshi.adapter(ConstructorParameterWithQualifier::class.java)
|
||||||
@@ -210,7 +210,7 @@ class KotlinJsonAdapterTest {
|
|||||||
|
|
||||||
@Test fun propertyWithQualifier() {
|
@Test fun propertyWithQualifier() {
|
||||||
val moshi = Moshi.Builder()
|
val moshi = Moshi.Builder()
|
||||||
.add(KotlinJsonAdapter.FACTORY)
|
.add(KotlinJsonAdapterFactory)
|
||||||
.add(UppercaseJsonAdapter())
|
.add(UppercaseJsonAdapter())
|
||||||
.build()
|
.build()
|
||||||
val jsonAdapter = moshi.adapter(PropertyWithQualifier::class.java)
|
val jsonAdapter = moshi.adapter(PropertyWithQualifier::class.java)
|
||||||
@@ -231,7 +231,7 @@ class KotlinJsonAdapterTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test fun constructorParameterWithJsonName() {
|
@Test fun constructorParameterWithJsonName() {
|
||||||
val moshi = Moshi.Builder().add(KotlinJsonAdapter.FACTORY).build()
|
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory).build()
|
||||||
val jsonAdapter = moshi.adapter(ConstructorParameterWithJsonName::class.java)
|
val jsonAdapter = moshi.adapter(ConstructorParameterWithJsonName::class.java)
|
||||||
|
|
||||||
val encoded = ConstructorParameterWithJsonName(3, 5)
|
val encoded = ConstructorParameterWithJsonName(3, 5)
|
||||||
@@ -245,7 +245,7 @@ class KotlinJsonAdapterTest {
|
|||||||
class ConstructorParameterWithJsonName(@Json(name = "key a") var a: Int, var b: Int)
|
class ConstructorParameterWithJsonName(@Json(name = "key a") var a: Int, var b: Int)
|
||||||
|
|
||||||
@Test fun propertyWithJsonName() {
|
@Test fun propertyWithJsonName() {
|
||||||
val moshi = Moshi.Builder().add(KotlinJsonAdapter.FACTORY).build()
|
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory).build()
|
||||||
val jsonAdapter = moshi.adapter(PropertyWithJsonName::class.java)
|
val jsonAdapter = moshi.adapter(PropertyWithJsonName::class.java)
|
||||||
|
|
||||||
val encoded = PropertyWithJsonName()
|
val encoded = PropertyWithJsonName()
|
||||||
@@ -264,7 +264,7 @@ class KotlinJsonAdapterTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test fun transientConstructorParameter() {
|
@Test fun transientConstructorParameter() {
|
||||||
val moshi = Moshi.Builder().add(KotlinJsonAdapter.FACTORY).build()
|
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory).build()
|
||||||
val jsonAdapter = moshi.adapter(TransientConstructorParameter::class.java)
|
val jsonAdapter = moshi.adapter(TransientConstructorParameter::class.java)
|
||||||
|
|
||||||
val encoded = TransientConstructorParameter(3, 5)
|
val encoded = TransientConstructorParameter(3, 5)
|
||||||
@@ -278,7 +278,7 @@ class KotlinJsonAdapterTest {
|
|||||||
class TransientConstructorParameter(@Transient var a: Int = -1, var b: Int = -1)
|
class TransientConstructorParameter(@Transient var a: Int = -1, var b: Int = -1)
|
||||||
|
|
||||||
@Test fun transientProperty() {
|
@Test fun transientProperty() {
|
||||||
val moshi = Moshi.Builder().add(KotlinJsonAdapter.FACTORY).build()
|
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory).build()
|
||||||
val jsonAdapter = moshi.adapter(TransientProperty::class.java)
|
val jsonAdapter = moshi.adapter(TransientProperty::class.java)
|
||||||
|
|
||||||
val encoded = TransientProperty()
|
val encoded = TransientProperty()
|
||||||
@@ -297,7 +297,7 @@ class KotlinJsonAdapterTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test fun supertypeConstructorParameters() {
|
@Test fun supertypeConstructorParameters() {
|
||||||
val moshi = Moshi.Builder().add(KotlinJsonAdapter.FACTORY).build()
|
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory).build()
|
||||||
val jsonAdapter = moshi.adapter(SubtypeConstructorParameters::class.java)
|
val jsonAdapter = moshi.adapter(SubtypeConstructorParameters::class.java)
|
||||||
|
|
||||||
val encoded = SubtypeConstructorParameters(3, 5)
|
val encoded = SubtypeConstructorParameters(3, 5)
|
||||||
@@ -313,7 +313,7 @@ class KotlinJsonAdapterTest {
|
|||||||
class SubtypeConstructorParameters(a: Int, var b: Int) : SupertypeConstructorParameters(a)
|
class SubtypeConstructorParameters(a: Int, var b: Int) : SupertypeConstructorParameters(a)
|
||||||
|
|
||||||
@Test fun supertypeProperties() {
|
@Test fun supertypeProperties() {
|
||||||
val moshi = Moshi.Builder().add(KotlinJsonAdapter.FACTORY).build()
|
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory).build()
|
||||||
val jsonAdapter = moshi.adapter(SubtypeProperties::class.java)
|
val jsonAdapter = moshi.adapter(SubtypeProperties::class.java)
|
||||||
|
|
||||||
val encoded = SubtypeProperties()
|
val encoded = SubtypeProperties()
|
||||||
@@ -335,7 +335,7 @@ class KotlinJsonAdapterTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test fun extendsPlatformClassWithPrivateField() {
|
@Test fun extendsPlatformClassWithPrivateField() {
|
||||||
val moshi = Moshi.Builder().add(KotlinJsonAdapter.FACTORY).build()
|
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory).build()
|
||||||
val jsonAdapter = moshi.adapter(ExtendsPlatformClassWithPrivateField::class.java)
|
val jsonAdapter = moshi.adapter(ExtendsPlatformClassWithPrivateField::class.java)
|
||||||
|
|
||||||
val encoded = ExtendsPlatformClassWithPrivateField(3)
|
val encoded = ExtendsPlatformClassWithPrivateField(3)
|
||||||
@@ -349,7 +349,7 @@ class KotlinJsonAdapterTest {
|
|||||||
internal class ExtendsPlatformClassWithPrivateField(var a: Int) : SimpleTimeZone(0, "C")
|
internal class ExtendsPlatformClassWithPrivateField(var a: Int) : SimpleTimeZone(0, "C")
|
||||||
|
|
||||||
@Test fun extendsPlatformClassWithProtectedField() {
|
@Test fun extendsPlatformClassWithProtectedField() {
|
||||||
val moshi = Moshi.Builder().add(KotlinJsonAdapter.FACTORY).build()
|
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory).build()
|
||||||
val jsonAdapter = moshi.adapter(ExtendsPlatformClassWithProtectedField::class.java)
|
val jsonAdapter = moshi.adapter(ExtendsPlatformClassWithProtectedField::class.java)
|
||||||
|
|
||||||
val encoded = ExtendsPlatformClassWithProtectedField(3)
|
val encoded = ExtendsPlatformClassWithProtectedField(3)
|
||||||
@@ -367,7 +367,7 @@ class KotlinJsonAdapterTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test fun platformTypeThrows() {
|
@Test fun platformTypeThrows() {
|
||||||
val moshi = Moshi.Builder().add(KotlinJsonAdapter.FACTORY).build()
|
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory).build()
|
||||||
try {
|
try {
|
||||||
moshi.adapter(Triple::class.java)
|
moshi.adapter(Triple::class.java)
|
||||||
fail()
|
fail()
|
||||||
@@ -378,7 +378,7 @@ class KotlinJsonAdapterTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test fun privateConstructorParameters() {
|
@Test fun privateConstructorParameters() {
|
||||||
val moshi = Moshi.Builder().add(KotlinJsonAdapter.FACTORY).build()
|
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory).build()
|
||||||
val jsonAdapter = moshi.adapter(PrivateConstructorParameters::class.java)
|
val jsonAdapter = moshi.adapter(PrivateConstructorParameters::class.java)
|
||||||
|
|
||||||
val encoded = PrivateConstructorParameters(3, 5)
|
val encoded = PrivateConstructorParameters(3, 5)
|
||||||
@@ -415,7 +415,7 @@ class KotlinJsonAdapterTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test fun privateProperties() {
|
@Test fun privateProperties() {
|
||||||
val moshi = Moshi.Builder().add(KotlinJsonAdapter.FACTORY).build()
|
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory).build()
|
||||||
val jsonAdapter = moshi.adapter(PrivateProperties::class.java)
|
val jsonAdapter = moshi.adapter(PrivateProperties::class.java)
|
||||||
|
|
||||||
val encoded = PrivateProperties()
|
val encoded = PrivateProperties()
|
||||||
|
Reference in New Issue
Block a user