Примеры KSP
Получение всех функций-членов
fun KSClassDeclaration.getDeclaredFunctions(): Sequence<KSFunctionDeclaration> =
declarations.filterIsInstance<KSFunctionDeclaration>()
Проверка, является ли класс или функция локальными
fun KSDeclaration.isLocal(): Boolean =
parentDeclaration != null && parentDeclaration !is KSClassDeclaration
Поиск фактического объявления класса или интерфейса, на которое указывает псевдоним типа
fun KSTypeAlias.findActualType(): KSClassDeclaration {
val resolvedType = this.type.resolve().declaration
return if (resolvedType is KSTypeAlias) {
resolvedType.findActualType()
} else {
resolvedType as KSClassDeclaration
}
}
Сбор подавленных имён в аннотации файла
// @file:kotlin.Suppress("Example1", "Example2")
fun KSFile.suppressedNames(): Sequence<String> = annotations
.filter {
it.shortName.asString() == "Suppress" &&
it.annotationType.resolve().declaration.qualifiedName?.asString() == "kotlin.Suppress"
}.flatMap {
it.arguments.flatMap {
(it.value as Array<String>).toList()
}
}
15 апреля 2024 г.
© 2010–2026 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/docs/ksp-examples.html