The codegentle file generation modules provide APIs for creating complete Java and Kotlin source files.
Generate Java source files with the JavaFile API.
Generate Kotlin source files with the KotlinFile API.
Both JavaFile and KotlinFile provide these APIs on all platforms (JVM, JS, Native, Wasm):
writeTo(Appendable, Strategy)- Write to anyAppendablewrite*String()extensions - Generate string outputtoRelativePath()- Get file path relative to source root
The following methods are only available on JVM targets:
JavaFile (JVM):
writeTo(path: Path)- Write tojava.nio.file.PathwriteTo(directory: File)- Write tojava.io.FilewriteTo(filer: Filer, vararg originatingElements: Element)- APT integration
KotlinFile (JVM):
writeTo(path: Path)- Write tojava.nio.file.PathwriteTo(directory: File)- Write tojava.io.FilewriteTo(filer: Filer, vararg originatingElements: Element)- APT integration
Important: When targeting non-JVM platforms, use
writeTo(Appendable, Strategy)with appropriate file I/O for your target platform.
val javaFile = JavaFile("com.example".parseToPackageName(), myClassSpec)
javaFile.writeToJavaString() // Platform-independent string generationval kotlinFile = KotlinFile("com.example".parseToPackageName(), myClassSpec)
kotlinFile.writeToKotlinString() // Platform-independent string generation