CodeGentle is a Kotlin multiplatform library for generating Java and Kotlin source code programmatically.
Learn about CodeGentle's code generation system built on CodeValue and CodePart:
- CodeValue - Container for code fragments with placeholder support
- CodePart - Building blocks for code construction
- Control flow, indentation, and formatting
Understand how to represent types, classes, and members:
- Common Naming - TypeName, ClassName, PackageName, MemberName
- Generic Types - ParameterizedTypeName, TypeVariableName, WildcardTypeName, ArrayTypeName
- Java Naming - Java-specific utilities and constants
- Kotlin Naming - KotlinLambdaTypeName, context receivers, value classes
Generate complete source files with imports and package structure:
- JavaFile - Java source file generation with secondary types
- KotlinFile - Kotlin source files with top-level functions/properties
Build type, method, and property specifications:
- Common Specs - Base interfaces and patterns
- Java Specs - Classes, interfaces, enums, records, sealed types
- Kotlin Specs - Classes, functions, properties, value classes
Seamlessly integrate with Kotlin Symbol Processing:
- Type Conversion - Convert KSP types to TypeName (15+ functions)
- Context Receivers - Handle Kotlin 2.0+ context receivers
- Class and member name conversion
- Spec conversion from KSP symbols
import love.forte.codegentle.java.*
val classSpec = JavaSimpleTypeSpec(JavaTypeSpec.Kind.CLASS, "HelloWorld") {
addModifier(JavaModifier.PUBLIC)
addMethod(JavaMethodSpec("main") {
addModifier(JavaModifier.PUBLIC, JavaModifier.STATIC)
returns(JavaClassNames.VOID.ref())
addParameter(JavaParameterSpec("args", JavaClassNames.STRING.ref().array()))
addCode("System.out.println(\"Hello, World!\");")
})
}
val javaFile = JavaFile("com.example".parseToPackageName(), classSpec)
println(javaFile.writeToJavaString())import love.forte.codegentle.kotlin.*
val classSpec = KotlinSimpleTypeSpec(KotlinTypeSpec.Kind.CLASS, "HelloWorld") {
addFunction(KotlinFunctionSpec("main") {
returns(KotlinClassNames.UNIT.ref())
addCode("println(\"Hello, World!\")")
})
}
val kotlinFile = KotlinFile("com.example".parseToPackageName(), classSpec)
println(kotlinFile.writeToKotlinString())import love.forte.codegentle.kotlin.ksp.*
class MyProcessor : SymbolProcessor {
override fun process(resolver: Resolver): List<KSAnnotated> {
resolver.getSymbolsWithAnnotation("MyAnnotation")
.filterIsInstance<KSClassDeclaration>()
.forEach { classDecl ->
// Convert KSP types directly
val className = classDecl.toClassName()
val functionSpecs = classDecl.getAllFunctions()
.map { it.toKotlinFunctionSpec() }
// Generate code...
}
return emptyList()
}
}| Module | Description |
|---|---|
codegentle-common |
Core APIs: CodeValue, TypeName, common specs |
codegentle-java |
Java code generation: JavaFile, Java specs |
codegentle-kotlin |
Kotlin code generation: KotlinFile, Kotlin specs |
codegentle-common-ksp |
KSP common utilities for type conversion |
codegentle-kotlin-ksp |
KSP Kotlin integration for spec conversion |
- JVM, JavaScript, Native, Wasm targets
- Platform-independent APIs with JVM-specific extensions
Java:
- Records (Java 16+)
- Sealed classes/interfaces (Java 17+)
- Non-sealed types
Kotlin:
- Value classes (inline classes)
- Context receivers (Kotlin 2.0+)
- Suspend functions
- Extension functions and properties
- Top-level declarations
- Direct conversion from KSP symbols
- Context receiver detection
- Full type system support
- ERROR TYPE handling
- Placeholder system (
%V) for dynamic content - Control flow extensions (if/else, try/catch, loops)
- Smart line wrapping (100 column limit)
- Builder DSL patterns
- GitHub: ForteScarlet/codegentle
- API Reference: See module-specific documentation
- Examples: Check
/tests/directory in the repository
- English Documentation: You are here (
docs/) - 中文文档: See
docs_zh/for Chinese documentation