-
-
Notifications
You must be signed in to change notification settings - Fork 422
Register Moshi adapters for serialization and deserialization for KotlinModuleMetadata
#1732
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Goooler
wants to merge
3
commits into
g/20250723/relocate-kotlin-metadata
from
g/20250912/moshi-adapter
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
247 changes: 247 additions & 0 deletions
247
src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/Moshi.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,247 @@ | ||
| @file:OptIn(UnstableMetadataApi::class) | ||
|
|
||
| package com.github.jengelman.gradle.plugins.shadow.internal | ||
|
|
||
| import com.squareup.moshi.JsonAdapter | ||
| import com.squareup.moshi.JsonDataException | ||
| import com.squareup.moshi.JsonReader | ||
| import com.squareup.moshi.JsonWriter | ||
| import com.squareup.moshi.Moshi | ||
| import com.squareup.moshi.Types | ||
| import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory | ||
| import java.lang.reflect.Type | ||
| import kotlin.metadata.ClassName | ||
| import kotlin.metadata.KmClass | ||
| import kotlin.metadata.jvm.JvmMetadataVersion | ||
| import kotlin.metadata.jvm.KmModule | ||
| import kotlin.metadata.jvm.KmPackageParts | ||
| import kotlin.metadata.jvm.KotlinModuleMetadata | ||
| import kotlin.metadata.jvm.UnstableMetadataApi | ||
|
|
||
| internal val moshi: Moshi = Moshi.Builder() | ||
| .add(KotlinJsonAdapterFactory()) | ||
| .add(KotlinModuleMetadataAdapterFactory()) | ||
| .build() | ||
|
|
||
| private class KotlinModuleMetadataAdapterFactory : JsonAdapter.Factory { | ||
| override fun create(type: Type, annotations: Set<Annotation>, moshi: Moshi): JsonAdapter<*>? { | ||
| return when (type) { | ||
| KotlinModuleMetadata::class.java -> { | ||
| val kmModuleAdapter = moshi.adapter(KmModule::class.java) | ||
| val versionAdapter = moshi.adapter(JvmMetadataVersion::class.java) | ||
| KotlinModuleMetadataAdapter(kmModuleAdapter, versionAdapter) | ||
| } | ||
| JvmMetadataVersion::class.java -> JvmMetadataVersionAdapter() | ||
| KmModule::class.java -> { | ||
| val mapAdapter = moshi.adapter<Map<String, KmPackageParts>>( | ||
| Types.newParameterizedType( | ||
| Map::class.java, | ||
| String::class.java, | ||
| KmPackageParts::class.java, | ||
| ), | ||
| ) | ||
| val listKmClassAdapter = moshi.adapter<List<KmClass>>( | ||
| Types.newParameterizedType(List::class.java, KmClass::class.java), | ||
| ) | ||
| KmModuleAdapter(mapAdapter, listKmClassAdapter) | ||
| } | ||
| KmPackageParts::class.java -> { | ||
| val listStringAdapter = moshi.adapter<List<String>>( | ||
| Types.newParameterizedType(List::class.java, String::class.java), | ||
| ) | ||
| val mapStringStringAdapter = moshi.adapter<Map<String, String>>( | ||
| Types.newParameterizedType( | ||
| Map::class.java, | ||
| String::class.java, | ||
| String::class.java, | ||
| ), | ||
| ) | ||
| KmPackagePartsAdapter(listStringAdapter, mapStringStringAdapter) | ||
| } | ||
| KmClass::class.java -> { | ||
| // This adapter is simplified. A full adapter would be very large. | ||
| val classNameAdapter = moshi.adapter(ClassName::class.java) | ||
| KmClassJsonAdapter(classNameAdapter) | ||
| } | ||
| else -> null | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private class KotlinModuleMetadataAdapter( | ||
| private val kmModuleAdapter: JsonAdapter<KmModule>, | ||
| private val versionAdapter: JsonAdapter<JvmMetadataVersion>, | ||
| ) : JsonAdapter<KotlinModuleMetadata>() { | ||
| override fun fromJson(reader: JsonReader): KotlinModuleMetadata { | ||
| reader.beginObject() | ||
| var kmModule: KmModule? = null | ||
| var version: JvmMetadataVersion? = null | ||
| while (reader.hasNext()) { | ||
| when (reader.nextName()) { | ||
| "kmModule" -> kmModule = kmModuleAdapter.fromJson(reader) | ||
| "version" -> version = versionAdapter.fromJson(reader) | ||
| else -> reader.skipValue() | ||
| } | ||
| } | ||
| reader.endObject() | ||
| return KotlinModuleMetadata( | ||
| kmModule ?: throw JsonDataException("Required property 'kmModule' is missing"), | ||
| version ?: throw JsonDataException("Required property 'version' is missing"), | ||
| ) | ||
| } | ||
|
|
||
| override fun toJson(writer: JsonWriter, value: KotlinModuleMetadata?) { | ||
| if (value == null) { | ||
| writer.nullValue() | ||
| return | ||
| } | ||
| writer.beginObject() | ||
| writer.name("kmModule") | ||
| kmModuleAdapter.toJson(writer, value.kmModule) | ||
| writer.name("version") | ||
| versionAdapter.toJson(writer, value.version) | ||
| writer.endObject() | ||
| } | ||
| } | ||
|
|
||
| private class JvmMetadataVersionAdapter : JsonAdapter<JvmMetadataVersion>() { | ||
| override fun fromJson(reader: JsonReader): JvmMetadataVersion { | ||
| reader.beginObject() | ||
| var major = -1 | ||
| var minor = -1 | ||
| var patch = -1 | ||
| while (reader.hasNext()) { | ||
| when (reader.nextName()) { | ||
| "major" -> major = reader.nextInt() | ||
| "minor" -> minor = reader.nextInt() | ||
| "patch" -> patch = reader.nextInt() | ||
| else -> reader.skipValue() | ||
| } | ||
| } | ||
| reader.endObject() | ||
| return JvmMetadataVersion(major, minor, patch) | ||
| } | ||
|
|
||
| override fun toJson(writer: JsonWriter, value: JvmMetadataVersion?) { | ||
| if (value == null) { | ||
| writer.nullValue() | ||
| return | ||
| } | ||
| writer.beginObject() | ||
| writer.name("major").value(value.major) | ||
| writer.name("minor").value(value.minor) | ||
| writer.name("patch").value(value.patch) | ||
| writer.endObject() | ||
| } | ||
| } | ||
|
|
||
| private class KmModuleAdapter( | ||
| private val packagePartsAdapter: JsonAdapter<Map<String, KmPackageParts>>, | ||
| private val optionalClassesAdapter: JsonAdapter<List<KmClass>>, | ||
| ) : JsonAdapter<KmModule>() { | ||
| override fun fromJson(reader: JsonReader): KmModule { | ||
| val kmModule = KmModule() | ||
| reader.beginObject() | ||
| while (reader.hasNext()) { | ||
| when (reader.nextName()) { | ||
| "packageParts" -> { | ||
| packagePartsAdapter.fromJson(reader)?.let { | ||
| kmModule.packageParts.putAll(it) | ||
| } | ||
| } | ||
| "optionalAnnotationClasses" -> { | ||
| optionalClassesAdapter.fromJson(reader)?.let { | ||
| kmModule.optionalAnnotationClasses.addAll(it) | ||
| } | ||
| } | ||
| else -> reader.skipValue() | ||
| } | ||
| } | ||
| reader.endObject() | ||
| return kmModule | ||
| } | ||
|
|
||
| override fun toJson(writer: JsonWriter, value: KmModule?) { | ||
| if (value == null) { | ||
| writer.nullValue() | ||
| return | ||
| } | ||
| writer.beginObject() | ||
| writer.name("packageParts") | ||
| packagePartsAdapter.toJson(writer, value.packageParts) | ||
| writer.name("optionalAnnotationClasses") | ||
| optionalClassesAdapter.toJson(writer, value.optionalAnnotationClasses) | ||
| writer.endObject() | ||
| } | ||
| } | ||
|
|
||
| private class KmPackagePartsAdapter( | ||
| private val listStringAdapter: JsonAdapter<List<String>>, | ||
| private val mapStringStringAdapter: JsonAdapter<Map<String, String>>, | ||
| ) : JsonAdapter<KmPackageParts>() { | ||
| override fun fromJson(reader: JsonReader): KmPackageParts { | ||
| var fileFacades: MutableList<String>? = null | ||
| var multiFileClassParts: MutableMap<String, String>? = null | ||
| reader.beginObject() | ||
| while (reader.hasNext()) { | ||
| when (reader.nextName()) { | ||
| "fileFacades" -> fileFacades = listStringAdapter.fromJson(reader)?.toMutableList() | ||
| "multiFileClassParts" -> | ||
| multiFileClassParts = | ||
| mapStringStringAdapter.fromJson(reader)?.toMutableMap() | ||
| else -> reader.skipValue() | ||
| } | ||
| } | ||
| reader.endObject() | ||
| return KmPackageParts( | ||
| fileFacades ?: mutableListOf(), | ||
| multiFileClassParts ?: mutableMapOf(), | ||
| ) | ||
| } | ||
|
|
||
| override fun toJson(writer: JsonWriter, value: KmPackageParts?) { | ||
| if (value == null) { | ||
| writer.nullValue() | ||
| return | ||
| } | ||
| writer.beginObject() | ||
| writer.name("fileFacades") | ||
| listStringAdapter.toJson(writer, value.fileFacades) | ||
| writer.name("multiFileClassParts") | ||
| mapStringStringAdapter.toJson(writer, value.multiFileClassParts) | ||
| writer.endObject() | ||
| } | ||
| } | ||
|
|
||
| // Simplified adapter for KmClass | ||
| private class KmClassJsonAdapter( | ||
| private val classNameAdapter: JsonAdapter<ClassName>, | ||
| ) : JsonAdapter<KmClass>() { | ||
| override fun fromJson(reader: JsonReader): KmClass { | ||
| val kmClass = KmClass() | ||
| reader.beginObject() | ||
| while (reader.hasNext()) { | ||
| when (reader.nextName()) { | ||
| "name" -> kmClass.name = classNameAdapter.fromJson(reader) | ||
| ?: throw JsonDataException("Required property 'name' is missing for KmClass") | ||
| // Other properties would be deserialized here | ||
| else -> reader.skipValue() | ||
| } | ||
| } | ||
| reader.endObject() | ||
| return kmClass | ||
| } | ||
|
|
||
| override fun toJson(writer: JsonWriter, value: KmClass?) { | ||
| if (value == null) { | ||
| writer.nullValue() | ||
| return | ||
| } | ||
| writer.beginObject() | ||
| writer.name("name") | ||
| classNameAdapter.toJson(writer, value.name) | ||
| // Other properties would be serialized here. | ||
| // For brevity, we are only serializing the name. | ||
| writer.endObject() | ||
| } | ||
| } |
21 changes: 21 additions & 0 deletions
21
...in/com/github/jengelman/gradle/plugins/shadow/internal/KotlinModuleMetadataAdapterTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package com.github.jengelman.gradle.plugins.shadow.internal | ||
|
|
||
| import assertk.assertThat | ||
| import assertk.assertions.isEqualTo | ||
| import kotlin.io.path.readBytes | ||
| import kotlin.metadata.jvm.KotlinModuleMetadata | ||
| import kotlin.metadata.jvm.UnstableMetadataApi | ||
| import org.junit.jupiter.api.Test | ||
|
|
||
| @OptIn(UnstableMetadataApi::class) | ||
| class KotlinModuleMetadataAdapterTest { | ||
|
|
||
| @Test | ||
| fun moduleMetadataFileNotChanged() { | ||
| val expected = requireResourceAsPath("META-INF/kotlin-stdlib.kotlin_module").readBytes() | ||
| val adapter = moshi.adapter(KotlinModuleMetadata::class.java) | ||
| val json = adapter.toJson(KotlinModuleMetadata.read(expected)) | ||
| val actual = requireNotNull(adapter.fromJson(json)).write() | ||
| assertThat(actual).isEqualTo(expected) | ||
| } | ||
| } | ||
Binary file not shown.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't and shouldn't use Gson instead; it works for
toJsonbut usingfromJsonwith errors like: