Kotlin Help

KSP examples

Get all member functions

fun KSClassDeclaration.getDeclaredFunctions(): List<KSFunctionDeclaration> = declarations.filterIsInstance<KSFunctionDeclaration>()

Check whether a class or function is local

fun KSDeclaration.isLocal(): Boolean = parentDeclaration != null && parentDeclaration !is KSClassDeclaration

Find the actual class or interface declaration that the type alias points to

fun KSTypeAlias.findActualType(): KSClassDeclaration { val resolvedType = this.type.resolve().declaration return if (resolvedType is KSTypeAlias) { resolvedType.findActualType() } else { resolvedType as KSClassDeclaration } }

Collect suppressed names in a file annotation

// @file:kotlin.Suppress("Example1", "Example2") fun KSFile.suppressedNames(): List<String> { val ignoredNames = mutableListOf<String>() annotations.filter { it.shortName.asString() == "Suppress" && it.annotationType.resolve()?.declaration?.qualifiedName?.asString() == "kotlin.Suppress" }.forEach { val argValues: List<String> = it.arguments.flatMap { it.value } ignoredNames.addAll(argValues) } return ignoredNames }
Last modified: 24 February 2022