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

com.zeoflow.depot.compiler.processing.ksp.KspReflectiveAnnotationBox.kt Maven / Gradle / Ivy

/*
 * Copyright (C) 2021 ZeoFlow SRL
 *
 * 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
 *
 *      http://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.zeoflow.depot.compiler.processing.ksp

import androidx.annotation.VisibleForTesting
import com.zeoflow.depot.compiler.processing.XAnnotationBox
import com.zeoflow.depot.compiler.processing.XType

/**
 * KSP sometimes cannot read default values in annotations. This reflective implementation
 * handles those cases.
 * see: https://github.com/google/ksp/issues/53
 */
internal class KspReflectiveAnnotationBox @VisibleForTesting constructor(
    private val env: KspProcessingEnv,
    private val annotationClass: Class,
    private val annotation: T
) : XAnnotationBox {
    override val value: T = annotation

    override fun getAsType(methodName: String): XType? {
        val value = getFieldValue>(methodName) ?: return null
        return env.findType(value.kotlin)
    }

    override fun getAsTypeList(methodName: String): List {
        val values = getFieldValue>(methodName)
        return values?.filterIsInstance>()?.mapNotNull {
            env.findType(it.kotlin)
        } ?: emptyList()
    }

    override fun  getAsAnnotationBox(methodName: String): XAnnotationBox {
        return createFromDefaultValue(
            env = env,
            annotationClass = annotationClass,
            methodName = methodName
        )
    }

    @Suppress("UNCHECKED_CAST", "BanUncheckedReflection")
    override fun  getAsAnnotationBoxArray(
        methodName: String
    ): Array> {
        val method = annotationClass.methods.firstOrNull {
            it.name == methodName
        } ?: error("$annotationClass does not contain $methodName")
        val values = method.invoke(annotation) as? Array ?: return emptyArray()
        return values.map {
            KspReflectiveAnnotationBox(
                env = env,
                annotationClass = method.returnType.componentType as Class,
                annotation = it
            )
        }.toTypedArray()
    }

    @Suppress("UNCHECKED_CAST", "BanUncheckedReflection")
    private fun  getFieldValue(methodName: String): R? {
        val value = annotationClass.methods.firstOrNull {
            it.name == methodName
        }?.invoke(annotation) ?: return null
        return value as R?
    }

    companion object {
        @Suppress("UNCHECKED_CAST")
        fun  createFromDefaultValue(
            env: KspProcessingEnv,
            annotationClass: Class<*>,
            methodName: String
        ): KspReflectiveAnnotationBox {
            val method = annotationClass.methods.firstOrNull {
                it.name == methodName
            } ?: error("$annotationClass does not contain $methodName")
            val defaultValue = method.defaultValue
                ?: error("$annotationClass.$method does not have a default value and is not set")
            return KspReflectiveAnnotationBox(
                env = env,
                annotationClass = method.returnType as Class,
                annotation = defaultValue as R
            )
        }

        @Suppress("UNCHECKED_CAST")
        fun  createFromDefaultValues(
            env: KspProcessingEnv,
            annotationClass: Class<*>,
            methodName: String
        ): Array> {
            val method = annotationClass.methods.firstOrNull {
                it.name == methodName
            } ?: error("$annotationClass does not contain $methodName")
            check(method.returnType.isArray) {
                "expected ${method.returnType} to be an array. $method"
            }
            val defaultValue = method.defaultValue
                ?: error("$annotationClass.$method does not have a default value and is not set")
            val values: Array = defaultValue as Array
            return values.map {
                KspReflectiveAnnotationBox(
                    env = env,
                    annotationClass = method.returnType.componentType as Class,
                    annotation = it
                )
            }.toTypedArray()
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy