KSP 示例

Get all member functions

  1. fun KSClassDeclaration.getDeclaredFunctions(): Sequence<KSFunctionDeclaration> =
  2. declarations.filterIsInstance<KSFunctionDeclaration>()

Check whether a class or function is local

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

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

  1. fun KSTypeAlias.findActualType(): KSClassDeclaration {
  2. val resolvedType = this.type.resolve().declaration
  3. return if (resolvedType is KSTypeAlias) {
  4. resolvedType.findActualType()
  5. } else {
  6. resolvedType as KSClassDeclaration
  7. }
  8. }

Collect suppressed names in a file annotation

  1. // @file:kotlin.Suppress("Example1", "Example2")
  2. fun KSFile.suppressedNames(): Sequence<String> = annotations
  3. .filter {
  4. it.shortName.asString() == "Suppress" &&
  5. it.annotationType.resolve().declaration.qualifiedName?.asString() == "kotlin.Suppress"
  6. }.flatMap {
  7. it.arguments.flatMap {
  8. (it.value as Array<String>).toList()
  9. }
  10. }