All Downloads are FREE. Search and download functionalities are using the official Maven repository.

se.ansman.kotshi.ProguardConfig.kt Maven / Gradle / Ivy

Go to download

An annotations processor that generates Moshi adapters from Kotlin data classes

The newest version!
package se.ansman.kotshi

import com.squareup.kotlinpoet.ClassName

data class ProguardConfig(
    val targetClass: ClassName,
    val targetConstructorParams: List,
) {
    fun outputFilePathWithoutExtension(canonicalName: String): String = "META-INF/proguard/kotshi-$canonicalName"

    fun writeTo(out: Appendable): Unit = out.run {
        val targetName = targetClass.reflectionName()
        // If the target class has default parameter values, keep its synthetic constructor
        //
        // -keepnames class kotlin.jvm.internal.DefaultConstructorMarker
        // -keepclassmembers @com.squareup.moshi.JsonClass @kotlin.Metadata class * {
        //     synthetic (...);
        // }
        //
        appendLine("-if class $targetName")
        appendLine("-keepnames class kotlin.jvm.internal.DefaultConstructorMarker")
        appendLine("-if class $targetName")
        appendLine("-keepclassmembers class $targetName {")
        val allParams = targetConstructorParams.toMutableList()
        val maskCount = if (targetConstructorParams.isEmpty()) {
            0
        } else {
            (targetConstructorParams.size + 31) / 32
        }
        repeat(maskCount) {
            allParams += "int"
        }
        allParams += "kotlin.jvm.internal.DefaultConstructorMarker"
        val params = allParams.joinToString(",")
        appendLine("    public synthetic ($params);")
        appendLine("}")
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy