KSP 示例程序
得到所有成员函数
fun KSClassDeclaration.getDeclaredFunctions(): Sequence<KSFunctionDeclaration> =
declarations.filterIsInstance<KSFunctionDeclaration>()
检查一个类或函数是否为 local
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
}
}
在源代码文件的注解中查找被压制(Suppressed)的名称
// @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()
}
}
最终更新: 2024/10/17