mirror of
https://github.com/fankes/moshi.git
synced 2025-10-19 16:09:21 +08:00
Clean up warnings in KotlinJsonAdapter (#899)
* Use isNotEmpty() * Use Metadata import directly * Tighten visibility of IndexedParameterMap * Use idiomatic require() checks Didn't use it on a couple other places where readability would suffer * Remove unnecessary type arg * Remove unnecessary curly braces * Use mutable list rather than allocate new lists IntelliJ thinks this is a better approach * Tighten KotlinJsonAdapter property visibility * Suppress existing extraneous warnings
This commit is contained in:
@@ -41,7 +41,7 @@ import kotlin.reflect.jvm.javaField
|
|||||||
import kotlin.reflect.jvm.javaType
|
import kotlin.reflect.jvm.javaType
|
||||||
|
|
||||||
/** Classes annotated with this are eligible for this adapter. */
|
/** Classes annotated with this are eligible for this adapter. */
|
||||||
private val KOTLIN_METADATA = Class.forName("kotlin.Metadata") as Class<out Annotation>
|
private val KOTLIN_METADATA = Metadata::class.java
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Placeholder value used when a field is absent from the JSON. Note that this code
|
* Placeholder value used when a field is absent from the JSON. Note that this code
|
||||||
@@ -54,9 +54,10 @@ private val ABSENT_VALUE = Any()
|
|||||||
* 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>(
|
internal class KotlinJsonAdapter<T>(
|
||||||
val constructor: KFunction<T>,
|
private val constructor: KFunction<T>,
|
||||||
val bindings: List<Binding<T, Any?>?>,
|
private val bindings: List<Binding<T, Any?>?>,
|
||||||
val options: JsonReader.Options) : JsonAdapter<T>() {
|
private val options: JsonReader.Options
|
||||||
|
) : JsonAdapter<T>() {
|
||||||
|
|
||||||
override fun fromJson(reader: JsonReader): T {
|
override fun fromJson(reader: JsonReader): T {
|
||||||
val constructorSize = constructor.parameters.size
|
val constructorSize = constructor.parameters.size
|
||||||
@@ -142,8 +143,10 @@ internal class KotlinJsonAdapter<T>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** A simple [Map] that uses parameter indexes instead of sorting or hashing. */
|
/** A simple [Map] that uses parameter indexes instead of sorting or hashing. */
|
||||||
class IndexedParameterMap(val parameterKeys: List<KParameter>, val parameterValues: Array<Any?>)
|
class IndexedParameterMap(
|
||||||
: AbstractMap<KParameter, Any?>() {
|
private val parameterKeys: List<KParameter>,
|
||||||
|
private val parameterValues: Array<Any?>
|
||||||
|
) : AbstractMap<KParameter, Any?>() {
|
||||||
|
|
||||||
override val entries: Set<Entry<KParameter, Any?>>
|
override val entries: Set<Entry<KParameter, Any?>>
|
||||||
get() {
|
get() {
|
||||||
@@ -167,7 +170,7 @@ internal class KotlinJsonAdapter<T>(
|
|||||||
class KotlinJsonAdapterFactory : JsonAdapter.Factory {
|
class KotlinJsonAdapterFactory : JsonAdapter.Factory {
|
||||||
override fun create(type: Type, annotations: MutableSet<out Annotation>, moshi: Moshi)
|
override fun create(type: Type, annotations: MutableSet<out Annotation>, moshi: Moshi)
|
||||||
: JsonAdapter<*>? {
|
: JsonAdapter<*>? {
|
||||||
if (!annotations.isEmpty()) return null
|
if (annotations.isNotEmpty()) return null
|
||||||
|
|
||||||
val rawType = Types.getRawType(type)
|
val rawType = Types.getRawType(type)
|
||||||
if (rawType.isInterface) return null
|
if (rawType.isInterface) return null
|
||||||
@@ -186,18 +189,18 @@ class KotlinJsonAdapterFactory : JsonAdapter.Factory {
|
|||||||
// Fall back to a reflective adapter when the generated adapter is not found.
|
// Fall back to a reflective adapter when the generated adapter is not found.
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rawType.isLocalClass) {
|
require(!rawType.isLocalClass) {
|
||||||
throw IllegalArgumentException("Cannot serialize local class or object expression ${rawType.name}")
|
"Cannot serialize local class or object expression ${rawType.name}"
|
||||||
}
|
}
|
||||||
val rawTypeKotlin = rawType.kotlin
|
val rawTypeKotlin = rawType.kotlin
|
||||||
if (rawTypeKotlin.isAbstract) {
|
require(!rawTypeKotlin.isAbstract) {
|
||||||
throw IllegalArgumentException("Cannot serialize abstract class ${rawType.name}")
|
"Cannot serialize abstract class ${rawType.name}"
|
||||||
}
|
}
|
||||||
if (rawTypeKotlin.isInner) {
|
require(!rawTypeKotlin.isInner) {
|
||||||
throw IllegalArgumentException("Cannot serialize inner class ${rawType.name}")
|
"Cannot serialize inner class ${rawType.name}"
|
||||||
}
|
}
|
||||||
if (rawTypeKotlin.objectInstance != null) {
|
require(rawTypeKotlin.objectInstance == null) {
|
||||||
throw IllegalArgumentException("Cannot serialize object declaration ${rawType.name}")
|
"Cannot serialize object declaration ${rawType.name}"
|
||||||
}
|
}
|
||||||
|
|
||||||
val constructor = rawTypeKotlin.primaryConstructor ?: return null
|
val constructor = rawTypeKotlin.primaryConstructor ?: return null
|
||||||
@@ -210,6 +213,7 @@ class KotlinJsonAdapterFactory : JsonAdapter.Factory {
|
|||||||
val parameter = parametersByName[property.name]
|
val parameter = parametersByName[property.name]
|
||||||
|
|
||||||
if (Modifier.isTransient(property.javaField?.modifiers ?: 0)) {
|
if (Modifier.isTransient(property.javaField?.modifiers ?: 0)) {
|
||||||
|
@Suppress("ReplaceGuardClauseWithFunctionCall") // More readable this way
|
||||||
if (parameter != null && !parameter.isOptional) {
|
if (parameter != null && !parameter.isOptional) {
|
||||||
throw IllegalArgumentException(
|
throw IllegalArgumentException(
|
||||||
"No default value for transient constructor $parameter")
|
"No default value for transient constructor $parameter")
|
||||||
@@ -217,6 +221,7 @@ class KotlinJsonAdapterFactory : JsonAdapter.Factory {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Suppress("ReplaceGuardClauseWithFunctionCall") // More readable this way
|
||||||
if (parameter != null && parameter.type != property.returnType) {
|
if (parameter != null && parameter.type != property.returnType) {
|
||||||
throw IllegalArgumentException("'${property.name}' has a constructor parameter of type " +
|
throw IllegalArgumentException("'${property.name}' has a constructor parameter of type " +
|
||||||
"${parameter.type} but a property of type ${property.returnType}.")
|
"${parameter.type} but a property of type ${property.returnType}.")
|
||||||
@@ -225,13 +230,13 @@ class KotlinJsonAdapterFactory : JsonAdapter.Factory {
|
|||||||
if (property !is KMutableProperty1 && parameter == null) continue
|
if (property !is KMutableProperty1 && parameter == null) continue
|
||||||
|
|
||||||
property.isAccessible = true
|
property.isAccessible = true
|
||||||
var allAnnotations = property.annotations
|
val allAnnotations = property.annotations.toMutableList()
|
||||||
var jsonAnnotation = property.findAnnotation<Json>()
|
var jsonAnnotation = property.findAnnotation<Json>()
|
||||||
|
|
||||||
if (parameter != null) {
|
if (parameter != null) {
|
||||||
allAnnotations += parameter.annotations
|
allAnnotations += parameter.annotations
|
||||||
if (jsonAnnotation == null) {
|
if (jsonAnnotation == null) {
|
||||||
jsonAnnotation = parameter.findAnnotation<Json>()
|
jsonAnnotation = parameter.findAnnotation()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -240,6 +245,7 @@ class KotlinJsonAdapterFactory : JsonAdapter.Factory {
|
|||||||
val adapter = moshi.adapter<Any>(
|
val adapter = moshi.adapter<Any>(
|
||||||
resolvedPropertyType, Util.jsonAnnotations(allAnnotations.toTypedArray()), property.name)
|
resolvedPropertyType, Util.jsonAnnotations(allAnnotations.toTypedArray()), property.name)
|
||||||
|
|
||||||
|
@Suppress("UNCHECKED_CAST")
|
||||||
bindingsByName[property.name] = KotlinJsonAdapter.Binding(name, adapter,
|
bindingsByName[property.name] = KotlinJsonAdapter.Binding(name, adapter,
|
||||||
property as KProperty1<Any, Any?>, parameter)
|
property as KProperty1<Any, Any?>, parameter)
|
||||||
}
|
}
|
||||||
@@ -248,9 +254,7 @@ class KotlinJsonAdapterFactory : JsonAdapter.Factory {
|
|||||||
|
|
||||||
for (parameter in constructor.parameters) {
|
for (parameter in constructor.parameters) {
|
||||||
val binding = bindingsByName.remove(parameter.name)
|
val binding = bindingsByName.remove(parameter.name)
|
||||||
if (binding == null && !parameter.isOptional) {
|
require(!(binding == null && !parameter.isOptional)) { "No property for required constructor $parameter" }
|
||||||
throw IllegalArgumentException("No property for required constructor ${parameter}")
|
|
||||||
}
|
|
||||||
bindings += binding
|
bindings += binding
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user