This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface KSVisitor<D, R> { | |
fun visitNode(node: KSNode, data: D): R | |
fun visitClassDeclaration(classDeclaration: KSClassDeclaration, data: D): R | |
fun visitPropertyDeclaration(property: KSPropertyDeclaration, data: D): R | |
// ~20 more functions | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FunctionProcessor(...) : SymbolProcessor { | |
... | |
inner class Visitor(private val file: OutputStream) : KSVisitorVoid() { | |
override fun visitClassDeclaration(classDeclaration: KSClassDeclaration, data: Unit) { | |
} | |
override fun visitPropertyDeclaration(property: KSPropertyDeclaration, data: Unit) { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.morfly | |
@Function(name = "myAmazingFunction") | |
interface MyAmazingFunction | |
fun main() { | |
println("Hello World!") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface KSNode { | |
... | |
fun <D, R> accept(visitor: KSVisitor<D, R>, data: D): R | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class KSPropertyDeclarationImpl(...) : KSPropertyDeclaration, ... { | |
... | |
override fun <D, R> accept(visitor: KSVisitor<D, R>, data: D): R { | |
return visitor.visitPropertyDeclaration(this, data) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
kotlin { | |
sourceSets { | |
main { | |
kotlin.srcDirs(file("$buildDir/generated/ksp/main/kotlin")) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override fun visitClassDeclaration(classDeclaration: KSClassDeclaration, data: Unit) { | |
... | |
// Getting the list of member properties of the annotated interface. | |
val properties: Sequence<KSPropertyDeclaration> = classDeclaration.getAllProperties() | |
.filter { it.validate() } | |
// Generating function signature. | |
file += "\n" | |
if (properties.iterator().hasNext()) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override fun visitPropertyDeclaration(property: KSPropertyDeclaration, data: Unit) { | |
// Generating argument name. | |
val argumentName = property.simpleName.asString() | |
file += " $argumentName: " | |
// Generating argument type. | |
val resolvedType: KSType = property.type.resolve() | |
file += resolvedType.declaration.qualifiedName?.asString() ?: run { | |
logger.error("Invalid property type", property) | |
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 += ">" |