From 253f1d07ba8e1f97bf2921b36bd00ca31e3e046e Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Tue, 5 Nov 2024 20:27:51 +0000 Subject: [PATCH 1/2] Update dependency com.pinterest.ktlint:ktlint-cli to v1.4.1 --- gradle/libs.versions.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index a0fc29302..f5bc69544 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -37,4 +37,4 @@ kotlinCompileTesting = { module = "dev.zacsweers.kctfork:core", version.ref = "k kotlinCompileTesting-ksp = { module = "dev.zacsweers.kctfork:ksp", version.ref ="kotlinCompileTesting" } truth = "com.google.truth:truth:1.4.4" googleJavaFormat = "com.google.googlejavaformat:google-java-format:1.24.0" -ktlint = "com.pinterest.ktlint:ktlint-cli:1.3.1" +ktlint = "com.pinterest.ktlint:ktlint-cli:1.4.1" From 4af3d033bf16e2294ae94dc931e0551a95dfc232 Mon Sep 17 00:00:00 2001 From: Jake Wharton Date: Wed, 6 Nov 2024 20:01:43 -0500 Subject: [PATCH 2/2] Format --- .editorconfig | 1 + .../com/squareup/moshi/recipes/JsonString.kt | 3 +- .../moshi/kotlin/codegen/api/kotlintypes.kt | 25 +++--- .../moshi/kotlin/codegen/ksp/shadedUtil.kt | 76 +++++++++---------- .../src/main/java/com/squareup/moshi/Moshi.kt | 9 +-- .../java/com/squareup/moshi/internal/Util.kt | 6 +- 6 files changed, 55 insertions(+), 65 deletions(-) diff --git a/.editorconfig b/.editorconfig index 6531d6787..2efd7d571 100644 --- a/.editorconfig +++ b/.editorconfig @@ -5,6 +5,7 @@ indent_size = 2 charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true +max_line_length=100 [*.{kt, kts}] kotlin_imports_layout = ascii diff --git a/examples/src/main/java/com/squareup/moshi/recipes/JsonString.kt b/examples/src/main/java/com/squareup/moshi/recipes/JsonString.kt index 94b4fee47..d1d41dc0a 100644 --- a/examples/src/main/java/com/squareup/moshi/recipes/JsonString.kt +++ b/examples/src/main/java/com/squareup/moshi/recipes/JsonString.kt @@ -45,8 +45,7 @@ class JsonStringJsonAdapterFactory : JsonAdapter.Factory { } private class JsonStringJsonAdapter : JsonAdapter() { - override fun fromJson(reader: JsonReader): String = - reader.nextSource().use(BufferedSource::readUtf8) + override fun fromJson(reader: JsonReader): String = reader.nextSource().use(BufferedSource::readUtf8) override fun toJson(writer: JsonWriter, value: String?) { writer.valueSink().use { sink -> sink.writeUtf8(checkNotNull(value)) } diff --git a/moshi-kotlin-codegen/src/main/java/com/squareup/moshi/kotlin/codegen/api/kotlintypes.kt b/moshi-kotlin-codegen/src/main/java/com/squareup/moshi/kotlin/codegen/api/kotlintypes.kt index a43d52d82..733db0c0d 100644 --- a/moshi-kotlin-codegen/src/main/java/com/squareup/moshi/kotlin/codegen/api/kotlintypes.kt +++ b/moshi-kotlin-codegen/src/main/java/com/squareup/moshi/kotlin/codegen/api/kotlintypes.kt @@ -69,19 +69,18 @@ internal fun TypeName.findRawType(): ClassName? { } } -internal fun TypeName.defaultPrimitiveValue(): CodeBlock = - when (this) { - BOOLEAN -> CodeBlock.of("false") - CHAR -> CodeBlock.of("0.toChar()") - BYTE -> CodeBlock.of("0.toByte()") - SHORT -> CodeBlock.of("0.toShort()") - INT -> CodeBlock.of("0") - FLOAT -> CodeBlock.of("0f") - LONG -> CodeBlock.of("0L") - DOUBLE -> CodeBlock.of("0.0") - UNIT, Void::class.asTypeName(), NOTHING -> throw IllegalStateException("Parameter with void, Unit, or Nothing type is illegal") - else -> CodeBlock.of("null") - } +internal fun TypeName.defaultPrimitiveValue(): CodeBlock = when (this) { + BOOLEAN -> CodeBlock.of("false") + CHAR -> CodeBlock.of("0.toChar()") + BYTE -> CodeBlock.of("0.toByte()") + SHORT -> CodeBlock.of("0.toShort()") + INT -> CodeBlock.of("0") + FLOAT -> CodeBlock.of("0f") + LONG -> CodeBlock.of("0L") + DOUBLE -> CodeBlock.of("0.0") + UNIT, Void::class.asTypeName(), NOTHING -> throw IllegalStateException("Parameter with void, Unit, or Nothing type is illegal") + else -> CodeBlock.of("null") +} @OptIn(DelicateKotlinPoetApi::class) internal fun TypeName.asTypeBlock(): CodeBlock { diff --git a/moshi-kotlin-codegen/src/main/java/com/squareup/moshi/kotlin/codegen/ksp/shadedUtil.kt b/moshi-kotlin-codegen/src/main/java/com/squareup/moshi/kotlin/codegen/ksp/shadedUtil.kt index de75902fc..a870ffd15 100644 --- a/moshi-kotlin-codegen/src/main/java/com/squareup/moshi/kotlin/codegen/ksp/shadedUtil.kt +++ b/moshi-kotlin-codegen/src/main/java/com/squareup/moshi/kotlin/codegen/ksp/shadedUtil.kt @@ -38,8 +38,7 @@ import kotlin.reflect.KClass * @param name fully qualified name of the class to be loaded; using '.' as separator. * @return a KSClassDeclaration, or null if not found. */ -internal fun Resolver.getClassDeclarationByName(name: String): KSClassDeclaration? = - getClassDeclarationByName(getKSNameFromString(name)) +internal fun Resolver.getClassDeclarationByName(name: String): KSClassDeclaration? = getClassDeclarationByName(getKSNameFromString(name)) internal fun KSAnnotated.getAnnotationsByType(annotationKClass: KClass): Sequence { return this.annotations.filter { @@ -49,8 +48,7 @@ internal fun KSAnnotated.getAnnotationsByType(annotationKClass: }.map { it.toAnnotation(annotationKClass.java) } } -internal fun KSAnnotated.isAnnotationPresent(annotationKClass: KClass): Boolean = - getAnnotationsByType(annotationKClass).firstOrNull() != null +internal fun KSAnnotated.isAnnotationPresent(annotationKClass: KClass): Boolean = getAnnotationsByType(annotationKClass).firstOrNull() != null @Suppress("UNCHECKED_CAST") private fun KSAnnotation.toAnnotation(annotationClass: Class): T { @@ -134,46 +132,45 @@ private fun KSAnnotation.asAnnotation( } @Suppress("UNCHECKED_CAST") -private fun List<*>.asArray(method: Method) = - when (method.returnType.componentType.name) { - "boolean" -> (this as List).toBooleanArray() +private fun List<*>.asArray(method: Method) = when (method.returnType.componentType.name) { + "boolean" -> (this as List).toBooleanArray() - "byte" -> (this as List).toByteArray() + "byte" -> (this as List).toByteArray() - "short" -> (this as List).toShortArray() + "short" -> (this as List).toShortArray() - "char" -> (this as List).toCharArray() + "char" -> (this as List).toCharArray() - "double" -> (this as List).toDoubleArray() + "double" -> (this as List).toDoubleArray() - "float" -> (this as List).toFloatArray() + "float" -> (this as List).toFloatArray() - "int" -> (this as List).toIntArray() + "int" -> (this as List).toIntArray() - "long" -> (this as List).toLongArray() + "long" -> (this as List).toLongArray() - "java.lang.Class" -> (this as List).map { - Class.forName(it.declaration.qualifiedName!!.asString()) - }.toTypedArray() + "java.lang.Class" -> (this as List).map { + Class.forName(it.declaration.qualifiedName!!.asString()) + }.toTypedArray() - "java.lang.String" -> (this as List).toTypedArray() + "java.lang.String" -> (this as List).toTypedArray() - else -> { // arrays of enums or annotations - when { - method.returnType.componentType.isEnum -> { - this.toArray(method) { result -> result.asEnum(method.returnType.componentType) } - } + else -> { // arrays of enums or annotations + when { + method.returnType.componentType.isEnum -> { + this.toArray(method) { result -> result.asEnum(method.returnType.componentType) } + } - method.returnType.componentType.isAnnotation -> { - this.toArray(method) { result -> - (result as KSAnnotation).asAnnotation(method.returnType.componentType) - } + method.returnType.componentType.isAnnotation -> { + this.toArray(method) { result -> + (result as KSAnnotation).asAnnotation(method.returnType.componentType) } - - else -> throw IllegalStateException("Unable to process type ${method.returnType.componentType.name}") } + + else -> throw IllegalStateException("Unable to process type ${method.returnType.componentType.name}") } } +} @Suppress("UNCHECKED_CAST") private fun List<*>.toArray(method: Method, valueProvider: (Any) -> Any): Array { @@ -188,17 +185,16 @@ private fun List<*>.toArray(method: Method, valueProvider: (Any) -> Any): Array< } @Suppress("UNCHECKED_CAST") -private fun Any.asEnum(returnType: Class): T = - returnType.getDeclaredMethod("valueOf", String::class.java) - .invoke( - null, - // Change from upstream KSP - https://github.com/google/ksp/pull/685 - if (this is KSType) { - this.declaration.simpleName.getShortName() - } else { - this.toString() - }, - ) as T +private fun Any.asEnum(returnType: Class): T = returnType.getDeclaredMethod("valueOf", String::class.java) + .invoke( + null, + // Change from upstream KSP - https://github.com/google/ksp/pull/685 + if (this is KSType) { + this.declaration.simpleName.getShortName() + } else { + this.toString() + }, + ) as T private fun Any.asByte(): Byte = if (this is Int) this.toByte() else this as Byte diff --git a/moshi/src/main/java/com/squareup/moshi/Moshi.kt b/moshi/src/main/java/com/squareup/moshi/Moshi.kt index d36856b25..59b3be357 100644 --- a/moshi/src/main/java/com/squareup/moshi/Moshi.kt +++ b/moshi/src/main/java/com/squareup/moshi/Moshi.kt @@ -53,8 +53,7 @@ public class Moshi internal constructor(builder: Builder) { public fun adapter(type: Class): JsonAdapter = adapter(type, NO_ANNOTATIONS) @CheckReturnValue - public fun adapter(type: Type, annotationType: Class): JsonAdapter = - adapter(type, setOf(createJsonQualifierImplementation(annotationType))) + public fun adapter(type: Type, annotationType: Class): JsonAdapter = adapter(type, setOf(createJsonQualifierImplementation(annotationType))) @CheckReturnValue public fun adapter(type: Type, vararg annotationTypes: Class): JsonAdapter { @@ -70,8 +69,7 @@ public class Moshi internal constructor(builder: Builder) { } @CheckReturnValue - public fun adapter(type: Type, annotations: Set): JsonAdapter = - adapter(type, annotations, fieldName = null) + public fun adapter(type: Type, annotations: Set): JsonAdapter = adapter(type, annotations, fieldName = null) /** * @return a [JsonAdapter] for [T], creating it if necessary. Note that while nullability of [T] @@ -354,8 +352,7 @@ public class Moshi internal constructor(builder: Builder) { override fun toJson(writer: JsonWriter, value: T?) = withAdapter { toJson(writer, value) } - private inline fun withAdapter(body: JsonAdapter.() -> R): R = - checkNotNull(adapter) { "JsonAdapter isn't ready" }.body() + private inline fun withAdapter(body: JsonAdapter.() -> R): R = checkNotNull(adapter) { "JsonAdapter isn't ready" }.body() override fun toString() = adapter?.toString() ?: super.toString() } diff --git a/moshi/src/main/java/com/squareup/moshi/internal/Util.kt b/moshi/src/main/java/com/squareup/moshi/internal/Util.kt index fdd19591c..26b2254db 100644 --- a/moshi/src/main/java/com/squareup/moshi/internal/Util.kt +++ b/moshi/src/main/java/com/squareup/moshi/internal/Util.kt @@ -533,8 +533,7 @@ internal class ParameterizedTypeImpl private constructor( override fun getOwnerType() = ownerType @Suppress("NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS") - override fun equals(other: Any?) = - other is ParameterizedType && Types.equals(this, other as ParameterizedType?) + override fun equals(other: Any?) = other is ParameterizedType && Types.equals(this, other as ParameterizedType?) override fun hashCode(): Int { return typeArguments.contentHashCode() xor rawType.hashCode() xor ownerType.hashCodeOrZero @@ -585,8 +584,7 @@ internal class GenericArrayTypeImpl private constructor(private val componentTyp override fun getGenericComponentType() = componentType @Suppress("NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS") - override fun equals(other: Any?) = - other is GenericArrayType && Types.equals(this, other as GenericArrayType?) + override fun equals(other: Any?) = other is GenericArrayType && Types.equals(this, other as GenericArrayType?) override fun hashCode() = componentType.hashCode()