From 711de52ae145ac3273a0c5e0c924578bb8ca3dbb Mon Sep 17 00:00:00 2001 From: Zac Sweers Date: Wed, 4 Sep 2019 21:39:35 -0400 Subject: [PATCH] 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 --- .../moshi/kotlin/reflect/KotlinJsonAdapter.kt | 44 ++++++++++--------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/kotlin/reflect/src/main/java/com/squareup/moshi/kotlin/reflect/KotlinJsonAdapter.kt b/kotlin/reflect/src/main/java/com/squareup/moshi/kotlin/reflect/KotlinJsonAdapter.kt index d187d12..4fb8050 100644 --- a/kotlin/reflect/src/main/java/com/squareup/moshi/kotlin/reflect/KotlinJsonAdapter.kt +++ b/kotlin/reflect/src/main/java/com/squareup/moshi/kotlin/reflect/KotlinJsonAdapter.kt @@ -41,7 +41,7 @@ import kotlin.reflect.jvm.javaField import kotlin.reflect.jvm.javaType /** Classes annotated with this are eligible for this adapter. */ -private val KOTLIN_METADATA = Class.forName("kotlin.Metadata") as Class +private val KOTLIN_METADATA = Metadata::class.java /** * 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. */ internal class KotlinJsonAdapter( - val constructor: KFunction, - val bindings: List?>, - val options: JsonReader.Options) : JsonAdapter() { + private val constructor: KFunction, + private val bindings: List?>, + private val options: JsonReader.Options +) : JsonAdapter() { override fun fromJson(reader: JsonReader): T { val constructorSize = constructor.parameters.size @@ -142,8 +143,10 @@ internal class KotlinJsonAdapter( } /** A simple [Map] that uses parameter indexes instead of sorting or hashing. */ - class IndexedParameterMap(val parameterKeys: List, val parameterValues: Array) - : AbstractMap() { + class IndexedParameterMap( + private val parameterKeys: List, + private val parameterValues: Array + ) : AbstractMap() { override val entries: Set> get() { @@ -167,7 +170,7 @@ internal class KotlinJsonAdapter( class KotlinJsonAdapterFactory : JsonAdapter.Factory { override fun create(type: Type, annotations: MutableSet, moshi: Moshi) : JsonAdapter<*>? { - if (!annotations.isEmpty()) return null + if (annotations.isNotEmpty()) return null val rawType = Types.getRawType(type) 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. } - if (rawType.isLocalClass) { - throw IllegalArgumentException("Cannot serialize local class or object expression ${rawType.name}") + require(!rawType.isLocalClass) { + "Cannot serialize local class or object expression ${rawType.name}" } val rawTypeKotlin = rawType.kotlin - if (rawTypeKotlin.isAbstract) { - throw IllegalArgumentException("Cannot serialize abstract class ${rawType.name}") + require(!rawTypeKotlin.isAbstract) { + "Cannot serialize abstract class ${rawType.name}" } - if (rawTypeKotlin.isInner) { - throw IllegalArgumentException("Cannot serialize inner class ${rawType.name}") + require(!rawTypeKotlin.isInner) { + "Cannot serialize inner class ${rawType.name}" } - if (rawTypeKotlin.objectInstance != null) { - throw IllegalArgumentException("Cannot serialize object declaration ${rawType.name}") + require(rawTypeKotlin.objectInstance == null) { + "Cannot serialize object declaration ${rawType.name}" } val constructor = rawTypeKotlin.primaryConstructor ?: return null @@ -210,6 +213,7 @@ class KotlinJsonAdapterFactory : JsonAdapter.Factory { val parameter = parametersByName[property.name] if (Modifier.isTransient(property.javaField?.modifiers ?: 0)) { + @Suppress("ReplaceGuardClauseWithFunctionCall") // More readable this way if (parameter != null && !parameter.isOptional) { throw IllegalArgumentException( "No default value for transient constructor $parameter") @@ -217,6 +221,7 @@ class KotlinJsonAdapterFactory : JsonAdapter.Factory { continue } + @Suppress("ReplaceGuardClauseWithFunctionCall") // More readable this way 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}.") @@ -225,13 +230,13 @@ class KotlinJsonAdapterFactory : JsonAdapter.Factory { if (property !is KMutableProperty1 && parameter == null) continue property.isAccessible = true - var allAnnotations = property.annotations + val allAnnotations = property.annotations.toMutableList() var jsonAnnotation = property.findAnnotation() if (parameter != null) { allAnnotations += parameter.annotations if (jsonAnnotation == null) { - jsonAnnotation = parameter.findAnnotation() + jsonAnnotation = parameter.findAnnotation() } } @@ -240,6 +245,7 @@ class KotlinJsonAdapterFactory : JsonAdapter.Factory { val adapter = moshi.adapter( resolvedPropertyType, Util.jsonAnnotations(allAnnotations.toTypedArray()), property.name) + @Suppress("UNCHECKED_CAST") bindingsByName[property.name] = KotlinJsonAdapter.Binding(name, adapter, property as KProperty1, parameter) } @@ -248,9 +254,7 @@ class KotlinJsonAdapterFactory : JsonAdapter.Factory { for (parameter in constructor.parameters) { val binding = bindingsByName.remove(parameter.name) - if (binding == null && !parameter.isOptional) { - throw IllegalArgumentException("No property for required constructor ${parameter}") - } + require(!(binding == null && !parameter.isOptional)) { "No property for required constructor $parameter" } bindings += binding }