Simple Code Generator
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
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