Kotlin Code Generation Alec Strong & Jake Wharton

public final class User {

private final String firstName ; private final String lastName ; private final int age ; public User (String firstName , String lastName , int age) {

this . firstName = firstName ; this . lastName = lastName ; this . age = age ;

}

public String getFirstName () {

return firstName ;

}

public String getLastName () {

return lastName ;

}

public int getAge () {

return age ;

}

@Override public boolean equals (Object o) {

if ( this == o) return true; if (o == null || getClass() != o.getClass()) return false;

User user = (User) o ; return age == user. age

&& Objects. equals ( firstName , user. firstName ) && Objects. equals ( lastName , user. lastName ) ;

}

@Override public int hashCode () {

return Objects. hash ( firstName , lastName , age ) ;

}

@Override public String toString () {

return "User{"

"firstName='"

firstName

' ' '

", lastName='"

lastName

' ' '

", age="

age

'}' ;

} } data class User(

val firstName : String , val lastName : String , val age : Int )

Bytecode

Protocol Buffers

Protocol Buffers SQL

Protocol Buffers SQL Swagger

Protocol Buffers SQL Swagger Android XML

Protocol Buffers SQL Swagger Android XML YAML

Protocol Buffers SQL Swagger Android XML YAML Mirrors/Elements

SQL Swagger Android XML YAML Mirrors/Elements PSI

Swagger Android XML YAML Mirrors/Elements PSI UAST

Android XML YAML Mirrors/Elements PSI UAST

???

Protocol Buffers SQL Swagger Android XML YAML Mirrors/Elements PSI UAST
???

Protocol Buffers

syntax = "proto2" ; package com.sample; message Ogre {

required string name = 1 ;

repeated int32 layers = 2 ;

optional string swamp = 3 ; }

public final String name ; public final List<Integer> layers ;

public final String swamp ; public Ogre (String name , List<Integer> layers , String swamp) {

this (name , layers , swamp , ByteString. EMPTY ) ; } public Ogre (String name , List<Integer> layers , String swamp ,

ByteString unknownFields) {

super ( ADAPTER , unknownFields) ; this . name = name ; this . layers = Internal. immutableCopyOf ( "layers" , layers) ; this . swamp = swamp ; }

public final String name ; public final List<Integer> layers ;

public final String swamp ; public Ogre (String name , List<Integer> layers , String swamp) {

this (name , layers , swamp , ByteString. EMPTY ) ; } public Ogre (String name , List<Integer> layers , String swamp ,

ByteString unknownFields) {

super ( ADAPTER , unknownFields) ; this . name = name ; this . layers = Internal. immutableCopyOf ( "layers" , layers) ; this . swamp = swamp ; } public final String name ; public final List<Integer> layers ;

public final String swamp ; this . name = name ; this . layers = Internal. immutableCopyOf ( "layers" , layers) ; this . swamp = swamp ;

public final String name ; public final List<Integer> layers ;

public final String swamp ; public Ogre (String name , List<Integer> layers , String swamp) {

this (name , layers , swamp , ByteString. EMPTY ) ; } public Ogre (String name , List<Integer> layers , String swamp ,

ByteString unknownFields) {

super ( ADAPTER , unknownFields) ; this . name = name ; this . layers = Internal. immutableCopyOf ( "layers" , layers) ; this . swamp = swamp ; }

ByteString. EMPTY

ByteString unknownFields

public final String name ; public final List<Integer> layers ;

public final String swamp ; public Ogre (String name , List<Integer> layers , String swamp) {

this (name , layers , swamp , ByteString. EMPTY ) ; } public Ogre (String name , List<Integer> layers , String swamp ,

ByteString unknownFields) {

super ( ADAPTER , unknownFields) ; this . name = name ; this . layers = Internal. immutableCopyOf ( "layers" , layers) ; this . swamp = swamp ; }

Internal. immutableCopyOf ( "layers" , layers)

public final String name ; public final List<Integer> layers ;

public final String swamp ; public Ogre (String name , List<Integer> layers , String swamp) {

this (name , layers , swamp , ByteString. EMPTY ) ; } public Ogre (String name , List<Integer> layers , String swamp ,

ByteString unknownFields) {

super ( ADAPTER , unknownFields) ; this . name = name ; this . layers = Internal. immutableCopyOf ( "layers" , layers) ; this . swamp = swamp ; }

String name

String swamp

String name

String swamp

String name

String swamp

public final String name ; public final List<Integer> layers ;

public final String swamp ; public Ogre (String name , List<Integer> layers , String swamp) {

this (name , layers , swamp , ByteString. EMPTY ) ; } public Ogre (String name , List<Integer> layers , String swamp ,

ByteString unknownFields) {

super ( ADAPTER , unknownFields) ; this . name = name ; this . layers = Internal. immutableCopyOf ( "layers" , layers) ; this . swamp = swamp ; }

class Ogre @JvmOverloads constructor (

val name : String , val layers : List<Int> , val swamp : String? ,

unknownFields: ByteString = ByteString. EMPTY ) : Message<Ogre , Ogre.Builder>( ADAPTER , unknownFields)

class Ogre @JvmOverloads constructor (

val name : String , val layers : List<Int> , val swamp : String? ,

unknownFields: ByteString = ByteString. EMPTY ) : Message<Ogre , Ogre.Builder>( ADAPTER , unknownFields)

val name : String , val layers : List<Int> , val swamp : String? , Ogre shrek = new Ogre( "Shrek" , Arrays. asList ( 1 , 2 ) , null ) ;

System. out .println(shrek.getSwamp()) ;

               s

class Ogre @JvmOverloads constructor (

@JvmField val name : String , @JvmField val layers : List<Int> , @JvmField val swamp : String? ,

unknownFields: ByteString = ByteString. EMPTY ) : Message<Ogre , Ogre.Builder>( ADAPTER , unknownFields)

@JvmField val name : String , @JvmField val layers : List<Int> , @JvmField val swamp : String? , Ogre shrek = new Ogre( "Shrek" , Arrays. asList ( 1 , 2 ) , null ) ;

System. out .println(shrek. swamp ) ;

S e

override fun equals (other: Any?): Boolean {

if (other === this ) return true if (other !is OgreKt) return false val o = other as OgreKt?

return (unknownFields() == o!!.unknownFields() && name == o. name

&& layers == o. layers

&& Internal.equals( swamp , o. swamp )) } override fun hashCode (): Int {

var result = super . hashCode

if (result == 0 ) { result = unknownFields().hashCode() result = result * 37 + name .hashCode() result = result * 37 + layers .hashCode() result = result * 37

  • ( swamp ?.hashCode() ?: 0 )

super . hashCode = result }

return result } override fun toString (): String {

val builder = StringBuilder() builder.append( ", name=" ).append( name )

if (! layers .isEmpty()) builder.append( ", layers=" ).append( layers )

if ( swamp != null ) builder.append( ", swamp=" ).append( swamp )

return builder.replace( 0 , 2 , "Ogre{" ).append( '}' ).toString() }

class Ogre @JvmOverloads constructor (

@JvmField val name : String , @JvmField val layers : List<Int> , @JvmField val swamp : String? ,

unknownFields: ByteString = ByteString. EMPTY ) : Message<Ogre , Ogre.Builder>( ADAPTER , unknownFields)

data class Ogre @JvmOverloads constructor (

@JvmField val name : String , @JvmField val layers : List<Int> , @JvmField val swamp : String? ,

unknownFields: ByteString = ByteString. EMPTY ) : Message<Ogre , Ogre.Builder>( ADAPTER , unknownFields)

data class Ogre @JvmOverloads constructor (

@JvmField val name : String , @JvmField val layers : List<Int> , @JvmField val swamp : String? ,

unknownFields: ByteString = ByteString. EMPTY ) : Message<Ogre , Ogre.Builder>( ADAPTER , unknownFields) data class Ogre

unknownFields: ByteString = ByteString.EMPTY

data class Ogre @JvmOverloads constructor (

@JvmField val name : String , @JvmField val layers : List<Int> , @JvmField val swamp : String? , private val unknownFields: ByteString = ByteString. EMPTY ) : Message<Ogre , Ogre.Builder>( ADAPTER , unknownFields) data class Ogre

private val 

unknownFields : ByteString = ByteString. EMPTY

class Ogre @JvmOverloads constructor (

@JvmField val name : String , @JvmField val layers : List<Int> , @JvmField val swamp : String? ,

unknownFields: ByteString = ByteString. EMPTY ) : Message<Ogre , Ogre.Builder>( ADAPTER , unknownFields)

public static final class Builder extends Message.Builder<Ogre , Builder> {

public String name ; public List<Integer> layers ; public String swamp ; public Builder () {

layers = Internal. newMutableList () ;

}

public Builder name (String name) {

this . name = name ; return this;

}

public Builder layers (List<Integer> layers) { Internal. checkElementsNotNull (layers) ; this . layers = layers ; return this;

}

public Builder swamp (String swamp) {

this . swamp = swamp ; return this;

}

@Override public

Ogre build () {

if ( name

null ) {

throw Internal. missingRequiredFields ( name , "name" ) ;

}

return new Ogre( name , layers , swamp , super .buildUnknownFields()) ;

} }

public static final class Builder extends Message.Builder<Ogre , Builder> {

public String name ; public List<Integer> layers ; public String swamp ; public Builder () {

layers = Internal. newMutableList () ;

}

public Builder name (String name) {

this . name = name ; return this;

}

public Builder layers (List<Integer> layers) { Internal. checkElementsNotNull (layers) ; this . layers = layers ; return this;

}

public Builder swamp (String swamp) {

this . swamp = swamp ; return this;

}

@Override public

Ogre build () {

if ( name

null ) {

throw Internal. missingRequiredFields ( name , "name" ) ;

}

return new Ogre( name , layers , swamp , super .buildUnknownFields()) ;

} } Internal. checkElementsNotNull (layers) ;

if ( name

null ) {

throw Internal. missingRequiredFields ( name , "name" ) ;

}

public static final class Builder extends Message.Builder<Ogre , Builder> {

public String name ; public List<Integer> layers ; public String swamp ; public Builder () {

layers = Internal. newMutableList () ;

}

public Builder name (String name) {

this . name = name ; return this;

}

public Builder layers (List<Integer> layers) { Internal. checkElementsNotNull (layers) ; this . layers = layers ; return this;

}

public Builder swamp (String swamp) {

this . swamp = swamp ; return this;

}

@Override public

Ogre build () {

if ( name

null ) {

throw Internal. missingRequiredFields ( name , "name" ) ;

}

return new Ogre( name , layers , swamp , super .buildUnknownFields()) ;

} }

class Ogre @JvmOverloads constructor (

@JvmField val name : String , @JvmField val layers : List<Int>

@JvmField val 

swamp : String?

unknownFields: ByteString = ByteString. EMPTY ) : Message<Ogre , Ogre.Builder>( ADAPTER , unknownFields)

@JvmField val name : String , @JvmField val layers : List<Int> = emptyList () , @JvmField val swamp : String? = null ,

val shrek = Ogre(

name = "shrek" ,

layers = listOf ( 1 , 2 ) ,

swamp = null )

Ogre shrek = new Ogre() ;

Ogre shrek = new Ogre( "Shrek" , Arrays. asList ( 1 , 2 ) , "Shrek" ) ;

Java Interop

class

Ogre(

@JvmField val name : String , @JvmField val layers : List<Int> = emptyList () , @JvmField val swamp : String? = null

) interface

@JvmField val name : String , @JvmField val layers : List<Int> = emptyList () , @JvmField val swamp : String? = null

interface Ogre {

val name : String

val layers : List<Int>

val swamp : String? }

interface Ogre {

val name : String

val layers : List<Int>

val swamp : String? }

shrek.getLayers() ;

inline fun < reified T

Ogre. findFriendOfType (): T ? {

for (friend in friends ()) {

if (friend is T ) return friend }

return null }

inline fun < reified T

Ogre. findFriendOfType (): T ? {

for (friend in friends ()) {

if (friend is T ) return friend }

return null } 
 
 shrek.findFriendOfType<Donkey>()

inline fun < reified T

Ogre. findFriendOfType (): T ? {

for (friend in friends ()) {

if (friend is T ) return friend }

return null } 
 
 shrek.findFriendOfType<Donkey>()

shrek.<Donkey>findFriendOfType() ;

inline fun < reified T

Ogre. findFriendOfType (): T ? {

for (friend a in friends ()) {

if (friend is T ) return friend } Y

return b null } Z

fun < T

Ogre. findFriendOfType (type: Class< T ): T ? {

for (friend in friends ()) {

if (type.isInstance(friend)) return friend as T ? } B

return null } A

inline fun < reified T

Ogre. findFriendOfType (): T ? {

for (friend a in friends ()) {

if (friend is T ) return friend } Y

return b null } Z

findFriendOfType ( ::class. java )

fun < T

Ogre. findFriendOfType (type: Class< T ): T ? {

for (friend in friends ()) {

if (type.isInstance(friend)) return friend as T ? } B

return null } A

inline fun < reified T

Ogre. findFriendOfType (): T ? {

return b findFriendOfType ( T :: class . java ) } Z

interface SwampChangedListener {

fun swampChanged (newSwamp: String) } fun addSwampChangedListener (listener: SwampChangedListener) = …

interface SwampChangedListener {

fun swampChanged (newSwamp: String) } A

fun addSwampChangedListener (listener: SwampChangedListener) = …

shrek.addSwampChangedListener( new SwampChangedListener() {

@Override public void swampChanged ( @NotNull String newSwamp) { P

System.

out .println( "What are you doing in "

  • newSwamp) ;

} }) ;

interface SwampChangedListener {

fun swampChanged (newSwamp: String) } A

fun addSwampChangedListener (listener: SwampChangedListener) = …

shrek.addSwampChangedListener(newSwamp -> { P

System. out .println( "What are you doing in "

  • newSwamp) ; }) ;

interface SwampChangedListener {

fun swampChanged (newSwamp: String) } fun addSwampChangedListener (listener: SwampChangedListener) = …

interface SwampChangedListener {

fun swampChanged (newSwamp: ( String) : Unit

} fun addSwampChangedListener (listener: SwampChangedListener) = … shrek.addSwampChangedListener( object : SwampChangedListener {

override fun swampChanged (newSwamp: String) { System. out .println( "What are you doing in $ newSwamp " ) } }) KT-7770

fun addSwampChangedListener (listener: (String) -> Unit) = …

fun addSwampChangedListener (listener: (String) -> Unit) = …

shrek.addSwampChangedListener { newSwamp ->

System. out .println( "What are you doing in $ newSwamp " ) }

fun addSwampChangedListener (listener: (String) -> Unit) = …

shrek.addSwampChangedListener { newSwamp ->

System. out .println( "What are you doing in $ newSwamp " ) }

shrek.addSwampChangedListener(newSwamp -> { System. out .println( "What are you doing in "

  • newSwamp) ; return Unit. INSTANCE ; }) ; KT-21018

fun addSwampChangedListener (listener: SwampChangedListener) = …

public interface SwampChangedListener {

void swampChanged (String newSwamp) ; } Z

fun addSwampChangedListener (listener: SwampChangedListener) = …

shrek.addSwampChangedListener(SwampChangedListener { newSwamp ->

System. out .println( "What are you doing in $ newSwamp " ) } )

public interface SwampChangedListener {

void swampChanged (String newSwamp) ; } Z

fun addSwampChangedListener (listener: SwampChangedListener) = … public interface SwampChangedListener {

void swampChanged (String newSwamp) ; } Z

shrek.addSwampChangedListener(newSwamp -> { System. out .println( "What are you doing in "

  • newSwamp) ; }) ; shrek.addSwampChangedListener(SwampChangedListener { newSwamp ->

System. out .println("What are you doing in $newSwamp") } )

sealed class Optional< T : Any>

data class Some< T : Any>( val value : T ): Optional< T

() object None : Optional<Nothing>()

sealed class Optional< T : Any>

data class Some< T : Any>( val value : T ): Optional< T

() object None : Optional<Nothing>()

fun < T : Any> T ?. asOptional () = if ( this

null ) None else Some( this )

@file:JvmName ( "Optionals" )

sealed class Optional< T : Any>

data class Some< T : Any>( val value : T ): Optional< T

() object None : Optional<Nothing>()

@JvmName ( "ofNullable" )

fun < T : Any> T ?. asOptional () = if ( this

null ) None else Some( this )

sealed class Optional< T : Any> {

companion object {

@JvmName ( "ofNullable" ) @JvmStatic fun < T

:

Any> T ?. asOptional ()

=

if ( this

null ) None else Some( this )

} Y

} Z

data class Some< T : Any>( val value : T ): Optional< T

() object None : Optional<Nothing>() @file:JvmName("Optionals ") KT-15286

sealed class Optional< T : Any> {

companion object {

@JvmStatic fun < T

:

Any> ofNullable (value: T ?) = value. asOptional () } Y

} Z

data class Some< T : Any>( val value : T ): Optional< T

() object None : Optional<Nothing>() fun < T

:

Any> T ?. asOptional () = if ( this

null ) None else Some( this ) @JvmName KT-15286

@file:JvmName( "-Optionals" ) sealed class Optional< T : Any> {

companion object {

@JvmStatic fun < T

:

Any> ofNullable (value: T ?) = value. asOptional () } Y

} Z

data class Some< T : Any>( val value : T ): Optional< T

() object None : Optional<Nothing>() fun < T

:

Any> T ?. asOptional () = if ( this

null ) None else Some( this ) KT-15286

class Foo (

val bar : Nothing , val listBar : List<Nothing> )

class Foo (

val bar : Nothing , val listBar : List<Nothing> ) Void bar = foo.getBar() ; List listBar = foo.getListBar() ;

KotlinPoet

Type

Type Class

Type Class Parameterized Type

Type String Map<String,Int>

val stringClass: KClass<String> = String:: class

val stringClass: KClass<String> = String:: class val stringName: ClassName = stringClass. asClassName ()

val stringClass: KClass<String> = String:: class val stringName: ClassName = stringClass. asClassName ()

import kotlin.String val foo : String = ""

val stringClass: KClass<String> = String:: class val stringName: ClassName = stringClass. asClassName ()

import kotlin.String val foo : String = ""

class String

val stringClass: KClass<String> = String:: class val stringName: ClassName = stringClass. asClassName ()

class String {

val foo : kotlin.String = "" }

val stringClass: KClass<String> = String:: class val stringName: ClassName = stringClass. asClassName ()

val stringClass: KClass<String> = String:: class val stringName: ClassName = stringClass. asClassName ()

val mapStringToInt

= ParameterizedTypeName.get( Map:: class . asTypeName () , stringName , Int:: class . asTypeName ())

val stringClass: KClass<String> = String:: class val stringName: ClassName = stringClass. asClassName ()

val mapStringToInt

= ParameterizedTypeName.get( Map:: class . asTypeName () , stringName , Int:: class . asTypeName ())

val nullableMap = mapStringToInt.asNullable()

val foo = PropertySpec.builder( "foo" , String:: class ) .initializer( "%S" , "foo" ) .build()

val foo = PropertySpec.builder( "foo" , String:: class ) .initializer( "%S" , "foo" ) .build()

String:: class

val foo = PropertySpec.builder( "foo" , String:: class ) .initializer( "%S" , "foo" ) .build()

"%S" ,

"foo"

val foo = PropertySpec.builder( "foo" , String:: class ) .initializer( "%S" , "foo" ) .build()

val foo = PropertySpec.builder( "foo" , String:: class ) .initializer( "%S" , "foo" ) .build()

println (foo) val

foo : kotlin.String = "foo" X

val foo = PropertySpec.builder( "foo" , String:: class ) .initializer( "%S" , "foo

bar" ) .build()

println (foo) val

foo : kotlin.String = "foo

bar" X

val foo = PropertySpec.builder( "foo" , String:: class ) .initializer( "%S" , "foo

bar" ) .build()

println (foo) val

foo : kotlin.String = "foo

bar" X

kotlin.String

val foo = PropertySpec.builder( "foo" , String:: class ) .initializer( "%S" , "foo

bar" ) .build()

val file = FileSpec.builder( "com.example" , "KotlinConf.kt" ) .addProperty(foo) .build()

println (file)

package com.example import kotlin.String val

foo : String = "foo

bar" X

foo

PropertySpec.builder( "foo" , String:: class ) .initializer( "%S" , "foo

bar" ) .build() package com.example import kotlin.String val

foo : String = "foo

bar" X

FunSpec.builder( "main" ) .addParameter( "args" , String:: class, VARARG ) .build() package com.example import kotlin.String fun main ( vararg args: String)

{ }

TypeSpec.classBuilder( "User" ) .addModifiers( DATA ) .build() package com.example data class User

TypeSpec.classBuilder( "User" ) .addModifiers( DATA ) .build() package com.example data class User

TypeSpec.classBuilder( "User" ) .addModifiers( DATA ) .addProperty(PropertySpec.builder( "name" , String:: class ).build()) .build() Z

package com.example data class User { Y

val

name : String } X

TypeSpec.classBuilder( "User" ) .addModifiers( DATA ) .addProperty(PropertySpec.builder( "name" , String:: class ).build())

.primaryConstructor(FunSpec.constructorBuilder() .addParameter( "name" , String:: class ) .build()) A

.build()

Z

package com.example data class User(name: String) G { Y

val

name : String } X

                name: String

TypeSpec.classBuilder( "User" ) .addModifiers( DATA ) .addProperty(PropertySpec.builder( "name" , String:: class )

    .initializer(

"name" ) .build()) B

.primaryConstructor(FunSpec.constructorBuilder() .addParameter( "name" , String:: class ) .build()) A

.build() 

package com.example data class User( val

name : String) G

TypeSpec.classBuilder( "User" ) .addModifiers( DATA ) .addProperty(PropertySpec.builder( "name" , String:: class )

    .initializer(

"name.toLowerCase()" ) .build()) B

.primaryConstructor(FunSpec.constructorBuilder() .addParameter( "name" , String:: class ) .build()) A

.build() 

package com.example data class User(name: String) G {

val

name : String = name. toLowerCase () } X

FunSpec.builder( "seconds" ) .receiver(Long:: class ) .returns(Duration:: class ) .addStatement( "return %T.ofSeconds(this)" , Duration:: class ) .build() package com.example import java.time.Duration

import kotlin.Long

fun Long. seconds (): Duration = Duration.ofSeconds( this )

FunSpec.builder( "seconds" ) .receiver(Long:: class ) .returns(Duration:: class )

.addStatement(

"require(this >= 0L)" )

.addStatement(

"return %T.ofSeconds(this)" , Duration:: class ) .build() package com.example import java.time.Duration

import kotlin.Long

fun Long. seconds (): Duration {

require ( this

= 0L )

return Duration.ofSeconds( this )

}

FunSpec.builder( "seconds" ) .receiver(Long:: class ) .returns(Duration:: class )

.addStatement(

"require(this >= 0L)" )

.addStatement(

"return %T.ofSeconds(this)" , Duration:: class ) .build()

val longSeconds = FunSpec.builder( "seconds" ) .receiver(Long:: class ) .returns(Duration:: class )

.addStatement(

"require(this >= 0L)" )

.addStatement(

"return %T.ofSeconds(this)" , Duration:: class ) .build()

val longSeconds = FunSpec.builder( "seconds" ) .receiver(Long:: class ) .returns(Duration:: class )

.addStatement(

"require(this >= 0L)" )

.addStatement(

"return %T.ofSeconds(this)" , Duration:: class ) .build()

FunSpec.builder( "main" ) .addParameter( "args" , String:: class,

VARARG ) .addStatement( "println(%L.%N())" ,

2L , longSeconds) .build()

val longSeconds = FunSpec.builder( "seconds" ) .receiver(Long:: class ) .returns(Duration:: class )

.addStatement(

"require(this >= 0L)" )

.addStatement(

"return %T.ofSeconds(this)" , Duration:: class ) .build()

FunSpec.builder( "main" ) .addParameter( "args" , String:: class,

VARARG ) .addStatement( "println(%L.%N())" ,

2L , longSeconds) .build()

%N

longSeconds

val longSeconds = FunSpec.builder( "seconds" ) .receiver(Long:: class ) .returns(Duration:: class )

.addStatement(

"require(this >= 0L)" )

.addStatement(

"return %T.ofSeconds(this)" , Duration:: class ) .build()

FunSpec.builder( "main" ) .addParameter( "args" , String:: class,

VARARG ) .addStatement( "println(%L.%N())" ,

2L , longSeconds) .build() fun

main ( vararg args: String) {

println ( 2L . seconds ()) }

val code = CodeBlock.builder()

val code = CodeBlock.builder() code.addStatement( "val foo = %T.MIN_VALUE" , Int:: class )

val code = CodeBlock.builder() code.addStatement( "val foo = %T.MIN_VALUE" , Int:: class )

code.add( "val bar = " ) when (answer) {

YES -> code.add( "0L" )

NO -> code.add( "%T.MIN_VALUE" , Int:: class ) } code.add( ".toString()

" )

val code = CodeBlock.builder() code.addStatement( "val foo = %T.MIN_VALUE" , Int:: class )

code.add( "val bar = " ) when (answer) {

YES -> code.add( "0L" )

NO -> code.add( "%T.MIN_VALUE" , Int:: class ) } code.add( ".toString()

" )

code.beginControlFlow( "if (bar.isEmpty())" ) .addStatement( "println(%S)" , "Empty!" ) .nextControlFlow( "else" ) .addStatement( "println(bar)" ) .endControlFlow()

val intMin = CodeBlock.of( "%T.MIN_VALUE" , Int:: class ) val intMax = CodeBlock.of( "%T.MAX_VALUE" , Int:: class ) val longMin = CodeBlock.of( "%T.MIN_VALUE" , Long:: class ) val longMax = CodeBlock.of( "%T.MAX_VALUE" , Long:: class ) val values = listOf (intMin , intMax , longMin , longMax)

val intMin = CodeBlock.of( "%T.MIN_VALUE" , Int:: class ) val intMax = CodeBlock.of( "%T.MAX_VALUE" , Int:: class ) val longMin = CodeBlock.of( "%T.MIN_VALUE" , Long:: class ) val longMax = CodeBlock.of( "%T.MAX_VALUE" , Long:: class ) val values = listOf (intMin , intMax , longMin , longMax) // elsewhere val list = values. joinToCode ( prefix = "listOf(" , suffix = ")" )

val intMin = CodeBlock.of( "%T.MIN_VALUE" , Int:: class ) val intMax = CodeBlock.of( "%T.MAX_VALUE" , Int:: class ) val longMin = CodeBlock.of( "%T.MIN_VALUE" , Long:: class ) val longMax = CodeBlock.of( "%T.MAX_VALUE" , Long:: class ) val values = listOf (intMin , intMax , longMin , longMax) // elsewhere val list = values. joinToCode ( prefix = "listOf(" , suffix = ")" )

listOf (Int. MIN_VALUE , Int. MAX_VALUE , Long. MIN_VALUE , Long. MAX_VALUE )

inline fun < reified T , R : CharSequence>

(String.() -> 

R ). crazy ( noinline foo: T .( R ?) -> Unit)

where T : Runnable , T : Closeable

= 

42L

val typeT = TypeVariableName( "T" , Runnable:: class, Closeable:: class ) val typeR = TypeVariableName( "R" , CharSequence:: class ) val stringExtToR = LambdaTypeName.get(String:: class . asClassName () , returnType = typeR) val tRtoUnit = LambdaTypeName.get(typeT ,

parameters = typeR.asNullable() , returnType = UNIT ) val crazy = FunSpec.builder( "crazy" ) .addModifiers( INLINE ) .receiver(stringExtToR) .addTypeVariable(typeT.reified()) .addTypeVariable(typeR) .addParameter( "foo" , tRtoUnit , NOINLINE ) .addStatement( "return 42L" ) .build()

inline fun < reified T , R : CharSequence>

(String.() -> 

R ). crazy ( noinline foo: T .( R ?) -> Unit)

where T : Runnable , T : Closeable

= 

42L

Kotlin Compatible Code

@Nullable public String getSwamp () {

return swamp ; }

@Nullable public String getSwamp () {

return swamp ; }

val swamp: String? = shrek. swamp

Bytecode Build tool

Bytecode Build tool

interface Foo {

val bar : String }

public abstract interface com/sample/Foo {

// access flags 0x401

public abstract getBar()Ljava/lang/String ;

@Lorg/jetbrains/annotations/NotNull ; () // invisible

LOCALVARIABLE 

this Lcom/sample/Foo ; L0 L1 0

@Lkotlin/Metadata ; ( 
 mv={ 1 ,

1 ,

7 } , 
 bv={ 1 ,

0 ,

2 } , 
 k= 1 , 
 d1={ “…” } , 
 d2={ "Lcom/sample/Foo;" ,

"" ,

"bar" ,

"" ,

“getBar" } 


)

// compiled from: Foo.kt

}

public abstract interface com/sample/Foo {

// access flags 0x401

public abstract getBar()Ljava/lang/String ;

@Lorg/jetbrains/annotations/NotNull ; () // invisible

LOCALVARIABLE 

this Lcom/sample/Foo ; L0 L1 0

@Lkotlin/Metadata ; ( 
 mv={ 1 ,

1 ,

7 } , 
 bv={ 1 ,

0 ,

2 } , 
 k= 1 , 
 d1={ “…” } , 
 d2={ "Lcom/sample/Foo;" ,

"" ,

"bar" ,

"" ,

“getBar" } 


)

// compiled from: Foo.kt

} goo.gl/GcT9gz

github.com/Takhion/kotlin-metadata

org.jetbrains.kotlin.serialization .ProtoBuf.Class

github.com/Takhion/kotlin-metadata

org.jetbrains.kotlin.serialization .ProtoBuf.Class

github.com/Takhion/kotlin-metadata

org.jetbrains.kotlin.serialization .ProtoBuf.Class

github.com/Takhion/kotlin-metadata

org.jetbrains.kotlin.serialization .ProtoBuf.Class

github.com/Takhion/kotlin-metadata

org.jetbrains.kotlin.serialization .ProtoBuf.Class

github.com/Takhion/kotlin-metadata

org.jetbrains.kotlin.serialization .ProtoBuf.Class

public abstract interface com/sample/Foo {

// access flags 0x401

public abstract

getBar ()Ljava/lang/String ;

@Lorg/jetbrains/annotations/NotNull ; () // invisible

LOCALVARIABLE 

this Lcom/sample/Foo ; L0 L1 0

@Lkotlin/Metadata ; ( 
 mv={ 1 ,

1 ,

7 } , 
 bv={ 1 ,

0 ,

2 } , 
 k= 1 , 
 d1={ “…” } , 
 d2={ "Lcom/sample/Foo;" ,

"" ,

"bar" ,

"" ,

“ getBar " } 


)

// compiled from: Foo.kt

}

Bytecode

Bytecode

public abstract interface com/sample/Foo {

// access flags 0x401

public abstract getBar()Ljava/lang/String ;

@Lorg/jetbrains/annotations/NotNull ; () // invisible

LOCALVARIABLE 

this Lcom/sample/Foo ; L0 L1 0

@Lkotlin/Metadata ; ( 
 mv={ 1 ,

1 ,

7 } , 
 bv={ 1 ,

0 ,

2 } , 
 k= 1 , 
 d1={ “…” } , 
 d2={ "Lcom/sample/Foo;" ,

"" ,

"bar" ,

"" ,

“getBar" } 
 ) g

// compiled from: Foo.kt

} A

public abstract interface com/sample/Foo {

// access flags 0x401

public abstract getBar()Ljava/lang/String ;

@Lorg/jetbrains/annotations/NotNull ; () // invisible

LOCALVARIABLE 

this Lcom/sample/Foo ; L0 L1 0

@Lkotlin/Metadata ; ( 
 mv={ 1 ,

1 ,

7 } , 
 bv={ 1 ,

0 ,

2 } , 
 k= 1 , 
 d1={ “…” } , 
 d2={ "Lcom/sample/Foo;" ,

"" ,

"bar" ,

"" ,

“getBar" } 
 ) g

// compiled from: Foo.kt

} A

public abstract interface com/sample/Foo {

// access flags 0x401

public abstract getBar()Ljava/lang/String ;

@Lorg/jetbrains/annotations/NotNull ; () // invisible

LOCALVARIABLE 

this Lcom/sample/Foo ; L0 L1 0

@Lkotlin/Metadata ; ( 
 mv={ 1 ,

1 ,

7 } , 
 bv={ 1 ,

0 ,

2 } , 
 k= 1 , 
 d1={ “…” } , 
 d2={ "Lcom/sample/Foo;" ,

"" ,

"bar" ,

"" ,

“getBar" } 


)

// compiled from: Foo.kt

} { A

AnnotationVisitor av1 = av0.visitArray( "d2" ) ;

av1.visit( null, "Lcom/sample/Foo;" ) ;

av1.visit( null, "" ) ;

av1.visit( null, "bar" ) ;

av1.visit( null, "" ) ;

av1.visit( null, "getBar" ) ;

av1.visit( null, "()I" ) ;

av1.visitEnd() ; } B

{ C

mv = cw.visitMethod( ACC_PUBLIC + ACC_ABSTRACT ,

"getBar" , "()I" , null, null ) ;

mv.visitEnd() ; } D

{ A

AnnotationVisitor av1 = av0.visitArray( "d2" ) ;

av1.visit( null, "Lcom/sample/Foo;" ) ;

av1.visit( null, "" ) ;

av1.visit( null, "bar" ) ;

av1.visit( null, "" ) ;

av1.visit( null, "getBar" ) ;

av1.visit( null, "()I" ) ;

av1.visitEnd() ; } B

{ C

mv = cw.visitMethod( ACC_PUBLIC + ACC_ABSTRACT ,

"getBar" , "()I" , null, null ) ;

mv.visitEnd() ; } D b

b

{ A

AnnotationVisitor av1 = av0.visitArray( "d2" ) ;

av1.visit( null, "Lcom/sample/Foo;" ) ;

av1.visit( null, "" ) ;

av1.visit( null, "bar" ) ;

av1.visit( null, "" ) ;

av1.visit( null, " bar " ) ;

av1.visit( null, "()I" ) ;

av1.visitEnd() ; } B

{ C

mv = cw.visitMethod( ACC_PUBLIC + ACC_ABSTRACT ,

" bar " , "()I" , null, null ) ;

mv.visitEnd() ; } D

{ A

AnnotationVisitor av1 = av0.visitArray( "d2" ) ;

av1.visit( null, "Lcom/sample/Foo;" ) ;

av1.visit( null, "" ) ;

av1.visit( null, "bar" ) ;

av1.visit( null, "" ) ;

av1.visit( null, " bar " ) ;

av1.visit( null, "()I" ) ;

av1.visitEnd() ; } B

{ C

mv = cw.visitMethod( ACC_PUBLIC + ACC_ABSTRACT ,

" bar " , "()I" , null, null ) ;

mv.visitEnd() ; } D

{ AnnotationVisitor av1 = av0.visitArray( "d2" ) ;

av1.visit( null, "Lcom/sample/Foo;" ) ;

av1.visit( null, "" ) ;

av1.visit( null, "bar" ) ;

av1.visit( null, "" ) ;

av1.visit( null, " bar " ) ;

av1.visit( null, "()I" ) ;

av1.visitEnd() ; } { mv = cw.visitMethod( ACC_PUBLIC + ACC_ABSTRACT ,

" bar " , "()I" , null, null ) ;

mv.visitEnd() ; } public abstract interface com/sample/Foo {

// access flags 0x401

public abstract

bar ()Ljava/lang/String ;

@Lorg/jetbrains/annotations/NotNull ; () // invisible

LOCALVARIABLE 

this Lcom/sample/Foo ; L0 L1 0

@Lkotlin/Metadata ; ( 
 mv={ 1 ,

1 ,

7 } , 
 bv={ 1 ,

0 ,

2 } , 
 k= 1 , 
 d1={ “…” } , 
 d2={ "Lcom/sample/Foo;" ,

"" ,

"bar" ,

"" ,

“ bar ” } 


)

// compiled from: Foo.kt

}

class KotlinFoo( override val bar: Int): Foo 
 
 class JavaFoo implements Foo { @Override

public int bar() {

return 0 ;

} }

void setPadding ( int l , int t , int r , int b) {

// … }

void setPadding ( int l , int t , int r , int b) {

// … } z

view.setPadding( 10 , 0 , 0 , 10 )

void setPadding ( int l , int t , int r , int b) {

// … } z

view.setPadding( left = 10 , bottom = 10 )

Bytecode

Multiplatform

Bytecode

Bytecode

Bytecode LLVM

enum class Direction { NORTH , SOUTH , EAST , WEST } class GamePeice( movement: Set<Direction> = EnumSet. of (Direction. NORTH ) ) Z

enum class Direction { NORTH , SOUTH , EAST , WEST } class GamePeice( movement: Set<Direction> = EnumSet. of (Direction. NORTH ) ) Z EnumSet. of

import java.util.EnumSet enum class Direction { NORTH , SOUTH , EAST , WEST } class GamePeice( movement: Set<Direction> = EnumSet. of (Direction. NORTH ) ) Z

                           s   O
                           EnumSet.

of

enum class Direction { NORTH , SOUTH , EAST , WEST } class GamePeice( movement: Set<Direction> = setOf (Direction. NORTH ) ) Z

S o

import com.google.common.collect

@AutoImplement interface Party {

val people : ImmutableList<Person> }

import com.google.common.collect

@AutoImplement interface Party {

val people : ImmutableList<Person> }

import com.google.common.collect

@Generated class AutoParty( override val people : ImmutableList<Person>) : Party

CREATE TABLE user(

name TEXT NOT NULL,

location TEXT AS android.location.Location )

CREATE TABLE user(

name TEXT NOT NULL,

location TEXT AS android.location.Location ) import android.location.Location data class User(

val

name : String,

val

location : Location )

@file:JvmName ( "ByteStrings" ) @JvmName ( "from" ) fun ByteArray. asByteString (): ByteString = …

@file:JvmName ( "ByteStrings" ) @JvmName ( "from" ) fun ByteArray. asByteString (): ByteString = …

byte [] bytes = …

ByteString b = ByteStrings. from (bytes) ;

@file:JvmName ( "ByteStrings" ) @JvmName ( "from" ) fun ByteArray. asByteString (): ByteString = …

byte [] bytes = …

ByteString b = ByteStrings. from (bytes) ; Cannot access 'JvmName': it is internal in 'kotlin.jvm' s

KT-19507

@JsName ( "from" ) fun ByteArray. asByteString (): ByteString = … @file:JvmName("ByteStrings")

@ JsName ( "from" ) fun ByteArray. asByteString (): ByteString = … @file:JvmName("ByteStrings")

KT-19508

Verbosity is okay. API is the #1 priority.

Verbosity is okay. API is the #1 priority. If you do any kind of Java codegen, keep Kotlin in mind.

Verbosity is okay. API is the #1 priority. If you do any kind of Java codegen, keep Kotlin in mind. Don’t depend on platform types unless the user does.

KotlinPoet 0.6.0 released!

@Strongolopolis & @JakeWharton Kotlin Code Generation