Created
May 5, 2022 13:52
-
-
Save adibfara/79d45fce94ea98f06f94b8c3cac0a126 to your computer and use it in GitHub Desktop.
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