com.faendir.kotlin.autodsl.ksp.KspSourceInfoResolver.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of processor Show documentation
Show all versions of processor Show documentation
Auto-generates DSL for your Kotlin projects using annotations.
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
}