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

com.bennyhuo.kotlin.processor.module.ksp.KspIndexLoader.kt Maven / Gradle / Ivy

The newest version!
package com.bennyhuo.kotlin.processor.module.ksp

import com.bennyhuo.kotlin.processor.module.LibraryIndex
import com.bennyhuo.kotlin.processor.module.common.IndexLoader
import com.bennyhuo.kotlin.processor.module.common.UniElement
import com.bennyhuo.kotlin.processor.module.common.UniTypeElement
import com.bennyhuo.kotlin.processor.module.utils.PACKAGE_NAME
import com.google.devtools.ksp.getAnnotationsByType
import com.google.devtools.ksp.getClassDeclarationByName
import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.symbol.KSAnnotated
import com.google.devtools.ksp.symbol.KSClassDeclaration

/**
 * Created by benny.
 */
internal class KspIndexLoader(
    private val resolver: Resolver,
    override val annotations: Set
) : IndexLoader {

    override fun findAnnotatedElementsByTypeName(enclosingTypeName: String): Collection> {
        val declarationName = DeclarationName.parse(enclosingTypeName)
        return resolver.getDeclarations(declarationName).flatMap {
            findAnnotatedElements(it.toUniElement())
        }
    }

    override fun getTypeElement(typeName: String): UniTypeElement? {
        return resolver.getClassDeclarationByName(typeName)?.toUniElement()
    }

    override fun getIndexes(): List {
        return resolver.getDeclarationsFromPackage(PACKAGE_NAME)
            .filterIsInstance()
            .mapNotNull {
                it.getAnnotationsByType(LibraryIndex::class).firstOrNull()
            }.toList()
    }

    fun loadUnwrapped() = load().mapValues {
        it.value.map { it.unwrap() }.toSet()
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy