Skip to content

Instantly share code, notes, and snippets.

View Morfly's full-sized avatar

Pavlo Stavytskyi Morfly

View GitHub Profile
override fun visitClassDeclaration(classDeclaration: KSClassDeclaration, data: Unit) {
if (classDeclaration.classKind != ClassKind.INTERFACE) {
logger.error("Only interface can be annotated with @Function", classDeclaration)
return
}
// Getting the @Function annotation object.
val annotation: KSAnnotation = classDeclaration.annotations.first {
it.shortName.asString() == "Function"
}
fun visitPropertyDeclaration(...) { ... }
private fun visitTypeArguments(typeArguments: List<KSTypeArgument>) {
if (typeArguments.isNotEmpty()) {
file += "<"
typeArguments.forEachIndexed { i, arg ->
visitTypeArgument(arg, data = Unit)
if (i < typeArguments.lastIndex) file += ", "
}
file += ">"
package com.morfly
@Function(name = "functionWithoutArgs")
interface FunctionWithoutArgs
@Function(name = "myAmazingFunction")
interface MyAmazingFunction {
val arg1: String?
val arg2: List<Int?>
val arg3: List<Map<String, *>>
// main-project/build.gradle.kts
...
kotlin.sourceSets.main {
kotlin.srcDirs(
file("$buildDir/generated/ksp/main/kotlin"),
)
}
package com.morfly
fun functionWithoutArgs() {
println("Hello from functionWithoutArgs")
}
fun myAmazingFunction(
arg1: kotlin.String?,
arg2: kotlin.collections.List<kotlin.Int?>,
arg3: kotlin.collections.List<kotlin.collections.Map<kotlin.String, *>>,
@Morfly
Morfly / GeneratedExample.kt
Last active July 18, 2021 14:44
KSP example of a generated function
fun myAmazingFunction(
arg1: String,
arg2: Map<String, List<*>>
) {
println("Hello from myAmazingFunction")
}
@Function(name = "myAmazingFunction")
interface MyAmazingFunction {
val arg1: List<Int>,
val arg2: Map<String, List<String>>
}
fun myAmazingFunction(
arg1: List<*>,
arg2: Map<*, *>,
) {
println("Hello from myAmazingFunction")
}
override fun visitTypeArgument(typeArgument: KSTypeArgument, data: Unit) {
if (options["ignoreGenericArgs"] == "true") {
file += "*"
return
}
// the rest of the code
...
}
// main-project/build.gradle.kts
...
ksp {
arg("ignoreGenericArgs", "true")
}