Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple Code Generator
internal class ListedProcessor(
private val environment: SymbolProcessorEnvironment,
) : SymbolProcessor {
private fun Resolver.findAnnotations(
kClass: KClass<*>,
) = getSymbolsWithAnnotation(
kClass.qualifiedName.toString())
.filterIsInstance<KSFunctionDeclaration>().filter {
it.parameters.isEmpty()
}
override fun process(resolver: Resolver): List<KSAnnotated> {
val listedFunctions: Sequence<KSFunctionDeclaration> =
resolver.findAnnotations(Listed::class)
if(!listedFunctions.iterator().hasNext()) return emptyList()
val functionNames = listedFunctions.map{ it.simpleName.asString() }
val sourceFiles = listedFunctions.mapNotNull { it.containingFile }
val fileText = buildString {
append("// ")
append(functionNames.joinToString(", "))
}
val file = environment.codeGenerator.createNewFile(
Dependencies(
false,
*sourceFiles.toList().toTypedArray(),
),
"your.generated.file.package",
"GeneratedLists"
)
file.write(fileText.toByteArray())
return (listedFunctions).filterNot { it.validate() }.toList()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment