Generating Kotlin code with KotlinPoet

Jake Wharton
May 16, 2017 · 2 min read

Heads up, we’ve moved! If you’d like to continue keeping up with the latest technical content from Square please visit us at our new home https://developer.squareup.com/blog

Java code generation has become a popular solution to simplifying library code. Dagger generates interface implementations, Butter Knife generates Android UI boilerplate, and Wire generates implementations of value classes for binary encoding of data.

Despite Kotlin’s strong interop with Java, the generated Java code for these libraries can feel foreign and convention-violating as they’re targeted at Java consumers and lack Kotlin features.

Today we’re happy to announce KotlinPoet, a library for generating Kotlin code!

Square’s history of code generation started with JavaWriter–a linear code generator that required emitting from top to bottom. We collaborated with Google’s Dagger team on its successor, JavaPoet, which took the concept further in providing builders and an immutable model of the generated code. KotlinPoet builds on the success of JavaPoet by providing similar models for creating Kotlin:

val greeterClass = ClassName.get("", "Greeter")
val kotlinFile = KotlinFile.builder("", "HelloWorld")
.addType(TypeSpec.classBuilder("Greeter")
.primaryConstructor(FunSpec.constructorBuilder()
.addParameter(String::class, "name")
.build())
.addProperty(PropertySpec.builder(String::class, "name")
.initializer("name")
.build())
.addFun(FunSpec.builder("greet")
.addStatement("println(%S)", "Hello, \$name")
.build())
.build())
.addFun(FunSpec.builder("main")
.addParameter(ArrayTypeName.of(String::class), "args")
.addStatement("%T(args[0]).greet()", greeterClass)
.build())
.build()

The above code produces the following Kotlin:

class Greeter(name: String) {
val name: String = name
fun greet() {
println("Hello, $name")
}
}
fun main(args: Array<String>) {
Greeter(args[0]).greet()
}

Generating Kotlin also comes with an advantage over generating Java: JavaScript and native are available as first-party compilation targets. This allows you to use the same tool and the same generated code for multiple platforms.

KotlinPoet is currently an early-access release. Not every language feature and syntax is covered yet, but it’s enough to get started and make public. We’re looking forward to seeing what you build with this library!

This post concludes Square’s “Square Open Source ♥s Kotlin” series.

Square Corner Blog

Buying and selling sound like simple things - and they…

Thanks to Jesse Wilson

Jake Wharton

Written by

Stop stopping.

Square Corner Blog

Buying and selling sound like simple things - and they should be. Somewhere along the way, they got complicated. At Square, we're working hard to make commerce easy for everyone.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store