Skip to content

ktool-dev/kotlin-gen

Repository files navigation

KotlinGen

A Kotlin Multiplatform code generation library for programmatically creating Kotlin source files.

Overview

For simple code generation cases, multi-line strings work really well. However, if you want to build more complex code, or have the ability to progressively and conditionally build the code, or make testing the code output easier, then KotlinGen can help. Kotlin Gen provides a type-safe API for generating Kotlin code. Instead of string concatenation or templates, you can build Kotlin declarations using a fluent API that ensures proper formatting, indentation, and syntax.

Features

  • Type-safe code generation: Build Kotlin code using classes and builders
  • Multiplatform support: Works on JVM, Android, iOS, Linux, Windows, and macOS
  • Comprehensive: Supports classes, interfaces, functions, properties, and more
  • Automatic formatting: Handles indentation, line breaks, and keyword escaping
  • Import management: Automatically organize and sort imports

Installation

dependencies {
    implementation("dev.ktool:kotlin-gen:1.0.0")
}

Quick Start

Creating a Simple Class

val file = KotlinFile("com.example") {
    +Class("User") {
        +Property("id", type = LongType)
        +Property("name", type = StringType)
    }
}

println(file.render())

Output:

package com.example

class User {
    val id: Long
    val name: String
}

Creating Functions

val file = KotlinFile("com.example") {
    +Function("add", returnType = IntType) {
        +Parameter("a", IntType)
        +Parameter("b", IntType)
        body = ExpressionBody("a + b")
    }
}

println(file.render())
// Output: fun add(a: Int, b: Int): Int = a + b

Extension Functions

val file = KotlinFile("com.example") {
    +Function("isEven", returnType = BooleanType) {
        receiver = IntType
        body = ExpressionBody("this % 2 == 0")
    }
}

println(file.render())
// Output: fun Int.isEven(): Boolean = this % 2 == 0

Creating Interfaces

val file = KotlinFile("com.example") {
    +Interface("Repository") {
        +TypeParameter("T")

        +Function("save") {
            +Parameter("entity", Type("T"))
        }
    }
}

println(file.render())

Output:

interface Repository<T> {
    fun save(entity: T)
}

Complete File with Imports

val file = KotlinFile("com.example.domain") {
    +Import("kotlin.collections.List")
    +Import("kotlin.collections.Map")

    +Class("Product", modifiers = listOf(Modifer.Data)) {
        primaryConstructor = PrimaryConstructor {
            +Property("id", type = StringType)
            +Property("name", type = StringType)
            +Property("price", type = DoubleType)
        }
    }
}

println(file.render())

Output:

package com.example.domain

import kotlin.collections.List
import kotlin.collections.Map

data class Product(val id: String, val name: String, val price: Double)

Core Components

KotlinFile

Represents a complete Kotlin source file with package declaration, imports, and top-level declarations.

Declarations

  • Class: Standard and data classes with properties, functions, and init blocks
  • Interface: Interfaces with abstract functions and properties
  • Object: Singleton objects
  • Function: Top-level and member functions with various modifiers
  • Property: Top-level and member properties with getters/setters
  • TypeAlias: Type aliases

Type System

  • Type: Represents Kotlin types including generics
  • TypeParameter: Generic type parameters with variance
  • Parameter: Function parameters with default values
  • Modifier: Access modifiers (public, private, etc.) and other modifiers (override, suspend, etc.)

Function Bodies

  • ExpressionBody: Single-expression function bodies (= expression)
  • FunctionBlock: Multi-statement function bodies with braces

Features

  • Automatic keyword escaping: Names that conflict with Kotlin keywords are automatically wrapped in backticks
  • Import sorting: Imports are automatically sorted alphabetically
  • Proper indentation: All nested structures are properly indented
  • Init blocks: Support for class initialization blocks
  • Primary constructors: Constructor parameters with properties

Supported Platforms

  • JVM
  • Android (API 21+)
  • iOS (x64, ARM64, Simulator ARM64)
  • Linux (x64)
  • Windows (mingw x64)
  • macOS (x64, ARM64)

License

Apache License 2.0

Author

Aaron Freeman (aaron@ktool.dev)

About

Kotlin Code Generation Library

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages