Skip to content

Instantly share code, notes, and snippets.

@adibfara
Created May 5, 2022 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adibfara/79d45fce94ea98f06f94b8c3cac0a126 to your computer and use it in GitHub Desktop.
Save adibfara/79d45fce94ea98f06f94b8c3cac0a126 to your computer and use it in GitHub Desktop.
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