write custom code generators for Kotlin Kotlin Poet is a popular library that helps in generating Kotlin code programmatically. What is KSP? What is Kotlin Poet In this slide, we will learn how to set up KSP with Kotlin Poet for code generation in an Android project. Where’re we going? Introduction to KSP KSP #
"@" that associate metadata with other code elements. They help convey information and can be used for automatic source code generation through annotation processing. This can reduce boilerplate code, and enhances code functionality and readability. Annotation Annotation #
integrate into Kotlin Symbol Processing It contains current information about current compilation session and has communication with Kotlin Compiler Called by Kotlin Symbol Processing to create the processor
in KSP such as class , field, property and annotation symbol analyze and manipulate these symbols during compilation process provides information about the symbol being processed, such as its name, kind (class, function, property, etc.), annotations, modifiers, etc
in KSP such as class , field, property and annotation symbol analyze and manipulate these symbols during compilation process provides information about the symbol being processed, such as its name, kind (class, function, property, etc.), annotations, modifiers, etc
// Using Kotlin reflect to get class from GenerateEnum Annotation (User) val symbols = resolver.getSymbols(GenerateEnum::class) val validatedSymbols = symbols.filter { it.validate() }.toList() val visitor = EnumGenerateVisitor(logger, codeGenerator) validatedSymbols.forEach { symbol -> symbol.accept(visitor, Unit) } return emptyList() }
Analysis: better to find potential errors Code Generation : better code generation such as class or functions Code Refactoring : better refactoring and renaming code