kotlinpoet-dsl

Replace KotlinPoet builders with Kotlin DSL

View the Project on GitHub hendraanggrian/kotlinpoet-dsl

Travis CI Codecov Maven Central Nexus Snapshot OpenJDK

KotlinPoet DSL

Lightweight Kotlin extension of KotlinPoet, providing Kotlin DSL functionality and other convenient solutions.

buildFileSpec("com.example.helloworld", "HelloWorld") {
    addClass("HelloWorld") {
        addModifiers(KModifier.PUBLIC, KModifier.FINAL)
        methods {
            "main" {
                addModifiers(KModifier.PUBLIC, KModifier.STATIC)
                returns = UNIT
                parameters.add<Array<String>>("args")
                appendLine("%T.out.println(%S)", System::class, "Hello, KotlinPoet!")
            }
        }
    }
}.writeTo(System.out)

Download

repositories {
    mavenCentral()
}
dependencies {
    implementation "com.hendraanggrian:kotlinpoet-dsl:$version"
}

Usage

Use T::class as parameters

KClass<*> can now be used as format arguments. There is also inline reified type function whenever possible.

buildMethodSpec("sortList") {
    returns = int
    parameters.add(classNameOf("java.util", "List").parameterizedBy(hoverboard), "list")
    appendLine("%T.sort(list)", Collections::class)
    appendLine("return list")
}

buildFieldSpec<Int>("count") {
    initializer("%L", 0)
}

Optional DSL

Some elements (field, method, parameter, etc.) are wrapped in container class. These containers have ability to add components with/without invoking DSL.

For example, 2 examples below will produce the same result.

types.addClass("Car") {
    methods {
        "getWheels" {
            returns = int
            statements {
                add("return wheels")
            }
        }
        "setWheels" {
            parameters {
                add(int, "wheels")
            }
            statements {
                add("this.wheels = wheels")
            }
        }
    }
}

types.addClass("Car") {
    methods.add("getWheels") {
        returns = int
        statements.add("return wheels")
    }
    methods.add("setWheels") {
        parameters["wheels"] = int
        statements.add("this.wheels = wheels")
    }
}

Property delegation

In spirit of Gradle Kotlin DSL, creating a spec can be done by delegating to a property.

val title by buildingParameterSpec(String::class) {
    annotations.add<NotNull>
}

val message by parameters.adding(String::class) {
    annotations.add<Nullable>
}

Fluent TypeName API

Write TypeName and all its subtypes fluently.

val myClass: ClassName = classOf("com.example", "MyClass")
val listener: LambdaTypeName = null.lambdaBy(returnType = "kotlin".classOf("Unit"))
val memberOfString: MemberTypeName = myClass.memberOf("myField")
val pairOfInteger: ParameterizedTypeName = "kotlin".classOf("Pair").parameterizedBy(Int::class, Int::class)
val tVariable: TypeVariableName = "T".typeVarOf()
val producerOfCharSequence: WildcardTypeName = "kotlin".classOf("CharSequence").producerOf()

If you have access to those types, they can also be strongly-typed.

val myClass = com.example.MyClass.asClassName()
val listener = null.lambdaBy(returnType = Unit::class)
val pairOfInteger = parameterizedTypeNameOf<android.util.Pair>(Int::class, Int::class)
val subtypeOfCharSequence = wildcardTypeNameProducerOf<kotlin.CharSequence>()