Convert Collection adapter to Kotlin (#1485)

* Consolidate contract utils

* Rename .java to .kt

* Convert CollectionJsonAdapter to Kotlin

* Lift factory into companion object

* Update moshi/src/main/java/com/squareup/moshi/internal/Util.kt
This commit is contained in:
Zac Sweers
2022-01-10 11:07:57 -05:00
committed by GitHub
parent 32932cfb40
commit 2daf78337d
7 changed files with 94 additions and 126 deletions

View File

@@ -30,6 +30,9 @@ val japicmp = tasks.register<JapicmpTask>("japicmp") {
"com.squareup.moshi.internal.NullSafeJsonAdapter", // Internal.
"com.squareup.moshi.internal.Util" // Internal.
)
fieldExcludes = listOf(
"com.squareup.moshi.CollectionJsonAdapter#FACTORY" // False-positive, class is not public anyway
)
}
tasks.named("check").configure {

View File

@@ -1,100 +0,0 @@
/*
* Copyright (C) 2014 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.squareup.moshi;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import javax.annotation.Nullable;
/** Converts collection types to JSON arrays containing their converted contents. */
abstract class CollectionJsonAdapter<C extends Collection<T>, T> extends JsonAdapter<C> {
public static final JsonAdapter.Factory FACTORY =
new JsonAdapter.Factory() {
@Override
public @Nullable JsonAdapter<?> create(
Type type, Set<? extends Annotation> annotations, Moshi moshi) {
Class<?> rawType = Types.getRawType(type);
if (!annotations.isEmpty()) return null;
if (rawType == List.class || rawType == Collection.class) {
return newArrayListAdapter(type, moshi).nullSafe();
} else if (rawType == Set.class) {
return newLinkedHashSetAdapter(type, moshi).nullSafe();
}
return null;
}
};
private final JsonAdapter<T> elementAdapter;
private CollectionJsonAdapter(JsonAdapter<T> elementAdapter) {
this.elementAdapter = elementAdapter;
}
static <T> JsonAdapter<Collection<T>> newArrayListAdapter(Type type, Moshi moshi) {
Type elementType = Types.collectionElementType(type, Collection.class);
JsonAdapter<T> elementAdapter = moshi.adapter(elementType);
return new CollectionJsonAdapter<Collection<T>, T>(elementAdapter) {
@Override
Collection<T> newCollection() {
return new ArrayList<>();
}
};
}
static <T> JsonAdapter<Set<T>> newLinkedHashSetAdapter(Type type, Moshi moshi) {
Type elementType = Types.collectionElementType(type, Collection.class);
JsonAdapter<T> elementAdapter = moshi.adapter(elementType);
return new CollectionJsonAdapter<Set<T>, T>(elementAdapter) {
@Override
Set<T> newCollection() {
return new LinkedHashSet<>();
}
};
}
abstract C newCollection();
@Override
public C fromJson(JsonReader reader) throws IOException {
C result = newCollection();
reader.beginArray();
while (reader.hasNext()) {
result.add(elementAdapter.fromJson(reader));
}
reader.endArray();
return result;
}
@Override
public void toJson(JsonWriter writer, C value) throws IOException {
writer.beginArray();
for (T element : value) {
elementAdapter.toJson(writer, element);
}
writer.endArray();
}
@Override
public String toString() {
return elementAdapter + ".collection()";
}
}

View File

@@ -0,0 +1,79 @@
/*
* Copyright (C) 2014 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.squareup.moshi
import com.squareup.moshi.internal.markNotNull
import java.lang.reflect.Type
/** Converts collection types to JSON arrays containing their converted contents. */
internal abstract class CollectionJsonAdapter<C : MutableCollection<T?>, T> private constructor(
private val elementAdapter: JsonAdapter<T>
) : JsonAdapter<C>() {
abstract fun newCollection(): C
override fun fromJson(reader: JsonReader): C {
val result = newCollection()
reader.beginArray()
while (reader.hasNext()) {
result.add(elementAdapter.fromJson(reader))
}
reader.endArray()
return result
}
override fun toJson(writer: JsonWriter, value: C?) {
markNotNull(value) // Always wrapped in nullSafe()
writer.beginArray()
for (element in value) {
elementAdapter.toJson(writer, element)
}
writer.endArray()
}
override fun toString() = "$elementAdapter.collection()"
companion object Factory : JsonAdapter.Factory {
override fun create(type: Type, annotations: Set<Annotation>, moshi: Moshi): JsonAdapter<*>? {
if (annotations.isNotEmpty()) return null
return when (type.rawType) {
List::class.java, Collection::class.java -> {
newArrayListAdapter<Any>(type, moshi).nullSafe()
}
Set::class.java -> {
newLinkedHashSetAdapter<Any>(type, moshi).nullSafe()
}
else -> null
}
}
private fun <T> newArrayListAdapter(type: Type, moshi: Moshi): JsonAdapter<MutableCollection<T?>> {
val elementType = Types.collectionElementType(type, Collection::class.java)
val elementAdapter = moshi.adapter<T>(elementType)
return object : CollectionJsonAdapter<MutableCollection<T?>, T>(elementAdapter) {
override fun newCollection(): MutableCollection<T?> = ArrayList()
}
}
private fun <T> newLinkedHashSetAdapter(type: Type, moshi: Moshi): JsonAdapter<MutableSet<T?>> {
val elementType = Types.collectionElementType(type, Collection::class.java)
val elementAdapter = moshi.adapter<T>(elementType)
return object : CollectionJsonAdapter<MutableSet<T?>, T>(elementAdapter) {
override fun newCollection(): MutableSet<T?> = LinkedHashSet()
}
}
}
}

View File

@@ -15,6 +15,7 @@
*/
package com.squareup.moshi
import com.squareup.moshi.internal.knownNotNull
import okio.Buffer
import okio.BufferedSource
import okio.ByteString
@@ -23,7 +24,6 @@ import okio.EOFException
import okio.IOException
import okio.buffer
import java.math.BigDecimal
import kotlin.contracts.contract
internal class JsonUtf8Reader : JsonReader {
/** The input JSON. */
@@ -1098,19 +1098,3 @@ internal class JsonUtf8Reader : JsonReader {
@Suppress("NOTHING_TO_INLINE")
private inline fun Byte.asChar(): Char = toInt().toChar()
// Sneaky backdoor way of marking a value as non-null to the compiler and skip the null-check intrinsic.
// Safe to use (unstable) contracts since they're gone in the final bytecode
// TODO move this to Util.kt after it's migrated to kotlin
@Suppress("NOTHING_TO_INLINE")
private inline fun <T> markNotNull(value: T?) {
contract {
returns() implies (value != null)
}
}
@Suppress("NOTHING_TO_INLINE")
private inline fun <T> knownNotNull(value: T?): T {
markNotNull(value)
return value
}

View File

@@ -330,7 +330,7 @@ public class Moshi internal constructor(builder: Builder) {
@JvmField
val BUILT_IN_FACTORIES: List<JsonAdapter.Factory> = buildList(6) {
add(StandardJsonAdapters.FACTORY)
add(CollectionJsonAdapter.FACTORY)
add(CollectionJsonAdapter.Factory)
add(MapJsonAdapter.FACTORY)
add(ArrayJsonAdapter.FACTORY)
add(RecordJsonAdapter.FACTORY)

View File

@@ -25,9 +25,7 @@ public class NonNullJsonAdapter<T>(public val delegate: JsonAdapter<T>) : JsonAd
return if (reader.peek() == JsonReader.Token.NULL) {
throw JsonDataException("Unexpected null at " + reader.path)
} else {
val result = delegate.fromJson(reader)
knownNotNull(result)
result
knownNotNull(delegate.fromJson(reader))
}
}

View File

@@ -46,7 +46,6 @@ import java.lang.reflect.TypeVariable
import java.lang.reflect.WildcardType
import java.util.Collections
import java.util.LinkedHashSet
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract
@JvmField public val NO_ANNOTATIONS: Set<Annotation> = emptySet()
@@ -477,16 +476,21 @@ public fun unexpectedNull(
return JsonDataException(message)
}
// A sneaky way to mark value as known to be not null, allowing smart casts and skipping the null-check intrinsic
// Safe to use here because it's already compiled and not being used externally
// Sneaky backdoor way of marking a value as non-null to the compiler and skip the null-check intrinsic.
// Safe to use (unstable) contracts since they're gone in the final bytecode
@Suppress("NOTHING_TO_INLINE")
@OptIn(ExperimentalContracts::class)
public inline fun <T : Any> knownNotNull(value: T?) {
internal inline fun <T> markNotNull(value: T?) {
contract {
returns() implies (value != null)
}
}
@Suppress("NOTHING_TO_INLINE")
internal inline fun <T> knownNotNull(value: T?): T {
markNotNull(value)
return value
}
// Public due to inline access in MoshiKotlinTypesExtensions
public fun <T> Class<T>.boxIfPrimitive(): Class<T> {
// cast is safe: long.class and Long.class are both of type Class<Long>