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

com.faendir.kotlin.autodsl.ksp.KspSourceInfoResolver.kt Maven / Gradle / Ivy

There is a newer version: 2.4.0
Show newest version
package com.faendir.kotlin.autodsl.ksp

import com.faendir.kotlin.autodsl.*
import com.google.devtools.ksp.isInternal
import com.google.devtools.ksp.isPublic
import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.symbol.*
import com.google.devtools.ksp.validate
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.TypeName
import kotlin.reflect.KClass
import kotlin.reflect.KProperty1
import com.google.devtools.ksp.getConstructors as superGetConstructors
import com.faendir.kotlin.autodsl.ksp.asClassName as asClassNameUtil

class KspSourceInfoResolver(private val resolver: Resolver) : SourceInfoResolver {
    private fun getClassesWithAnnotation(annotation: String): List =
        resolver.getSymbolsWithAnnotation(annotation).filterIsInstance().toList()

    override fun getClassesWithAnnotation(annotation: KClass) = getClassesWithAnnotation(annotation.qualifiedName!!)

    override fun getClassesWithAnnotation(annotation: KSClassDeclaration) = getClassesWithAnnotation(annotation.qualifiedName!!.asString())

    override fun KSClassDeclaration.getClassKind(): ClassKind = classKind

    override fun KSAnnotated.hasAnnotation(annotation: KClass): Boolean =
        annotations.filter { it.couldBe(annotation) }.any { it.isEqualTo(annotation) }

    override fun  KSAnnotated.getAnnotationTypeProperty(annotation: KClass, property: KProperty1>): ClassName? =
        (findAnnotation(annotation)?.arguments?.firstOrNull { it.name?.asString() == property.name }?.value as? KSType?)?.asClassNameUtil()

    override fun  KSAnnotated.getAnnotationProperty(annotation: KClass, property: KProperty1): V? =
        findAnnotation(annotation)?.arguments?.firstOrNull { it.name?.asString() == property.name }?.value as? V?

    override fun KSClassDeclaration.isAbstract(): Boolean = modifiers.contains(Modifier.ABSTRACT)

    override fun KSClassDeclaration.getConstructors(): List = superGetConstructors().toList()

    override fun KSFunctionDeclaration.isAccessible(): Boolean = isPublic() || isInternal()

    override fun KSClassDeclaration.getPrimaryConstructor(): KSFunctionDeclaration? = primaryConstructor

    override fun KSFunctionDeclaration.isValid(): Boolean = validate()

    override fun KSFunctionDeclaration.getParameters(): List = parameters

    override fun KSClassDeclaration.asClassName(): ClassName = asClassNameUtil()

    override fun KSValueParameter.getTypeDeclaration(): KSClassDeclaration? = type.resolve().declaration as? KSClassDeclaration

    override fun KSValueParameter.getTypeArguments(): List =
        type.resolve().arguments.mapNotNull { it.type?.resolve()?.declaration as? KSClassDeclaration }

    override fun KSValueParameter.getTypeName(): TypeName = type.asTypeName()

    override fun KSValueParameter.getName(): String = name!!.asString()

    override fun KSValueParameter.hasDefault(): Boolean = hasDefault

    override fun KSValueParameter.getDoc(): String? = null
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy