Share
Embed
Annotation Processing in a Kotlin World
Annotation processing is a powerful tool to extend your codebase at build time, and the advent of Kotlin in the android community introduces a dimension to this. Kapt works with existing Java processors, but it doesn’t come without its quirks at the interop layer. This talk will dive into how you can leverage Kotlin bytecode’s @Metadata annotations to better understand Kotlin code during annotation processing, covering anywhere from case studies in Dagger and Moshi to leveraging tools like KotlinPoet to even generate Kotlin code that seamlessly leverages language features.
Zac Sweers
October 04, 2018
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
162 slides
-
@Provides fun provideList(): List<Taco> { return emptyList() }a @Inject lateinit
var tacoList: List<Taco> -
@Provides fun provideList(): List<Taco> { return emptyList() }a @Inject lateinit
var tacoList: List<@SuppressJvmWildcard Taco> -
@Provides fun provideList(): List<Taco> { return emptyList() }a @Inject lateinit
var tacoList: List<@SuppressJvmWildcard Taco> -
@Breakfast @Provides fun provideTaco(): Taco { return BreakfastTaco() }a @Breakfast
@Inject lateinit var taco: Taco -
@Breakfast @Provides fun provideTaco(): Taco { return BreakfastTaco() }a @field:Breakfast
@Inject lateinit var taco: Taco -
@Breakfast @Provides fun provideTaco(): Taco { return BreakfastTaco() }a @field:Breakfast
@Inject lateinit var taco: Taco -
@Module class NetworkModule { @Provides static HttpClient provideHttpClient() { return
HttpClient.create(); }a }b -
@Module class NetworkModule { @Provides static HttpClient provideHttpClient() { return
HttpClient.create(); }a companion object { }c }b -
@Module class NetworkModule { companion object { @Provides fun provideHttpClient():
HttpClient { return HttpClient.create() }a }c }b -
@Module class NetworkModule { companion object { @JvmStatic @Provides fun
provideHttpClient(): HttpClient { return HttpClient.create() }a }c }b -
@Module class NetworkModule { @Module companion object { @JvmStatic @Provides
fun provideHttpClient(): HttpClient { return HttpClient.create() }a }c }b -
@Module class NetworkModule { @Module companion object { @JvmStatic @Provides
fun provideHttpClient(): HttpClient { return HttpClient.create() }a }c }b -
@Module class NetworkModule { @Module companion object { @JvmStatic @Provides
fun provideHttpClient(): HttpClient { return HttpClient.create() }a }c }b -
Quick refresher • Compile time • Since Java 5ish (APIs
in Java 6) • Reads Java (byte) code in intermediate format -
Quick refresher • Compile time • Since Java 5ish (APIs
in Java 6) • Reads Java (byte) code in intermediate format • Elements, Types, Filer, etc APIs -
Quick refresher • Compile time • Since Java 5ish (APIs
in Java 6) • Reads Java (byte) code in intermediate format • Elements, Types, Filer, etc APIs • Cannot modify code, only generate more -
Quick refresher • Compile time • Since Java 5ish (APIs
in Java 6) • Reads Java (byte) code in intermediate format • Elements, Types, Filer, etc APIs • Cannot modify code, only generate more • Multiple rounds -
Quick refresher • Compile time • Since Java 5ish (APIs
in Java 6) • Reads Java (byte) code in intermediate format • Elements, Types, Filer, etc APIs • Cannot modify code, only generate more • Multiple rounds -
Quick refresher • Compile time • Since Java 5ish (APIs
in Java 6) • Reads Java (byte) code in intermediate format • Elements, Types, Filer, etc APIs • Cannot modify code, only generate more • Multiple rounds -
@kotlin.Metadata( mv = {1, 1, 11}, bv = {1, 0,
2}, k = 1, d1 = {"\u0000\u0012\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0000\n\u0002\u0010\u000e\n\u0002\b\u0004\b\u0007\u0018 \u00002\u00020\u0001B\r\u0012\u0006\u0010\u0002\u001a\u00020\u0003\u00a2\u0006\u0002\u0010\u0004R\u0011\u0010\u0 002\u001a\u00020\u0003\u00a2\u0006\b\n\u0000\u001a\u0004\b\u0005\u0010\u0006\u00a8\u0006\u0007"}, d2 = {"Ltest/Person;", "", "name", "", "(Ljava/lang/String;)V", "getName", "()Ljava/lang/String;", "test- project"} ) public final class Person { @org.jetbrains.annotations.NotNull() private final java.lang.String name = null; @org.jetbrains.annotations.NotNull() public final java.lang.String getName() { return null; } public Person(@org.jetbrains.annotations.NotNull() java.lang.String name) { super(); } } -
public final class Person { @org.jetbrains.annotations.NotNull() private final java.lang.String name
= null; @org.jetbrains.annotations.NotNull() public final java.lang.String getName() { return null; } public Person(@org.jetbrains.annotations.NotNull() java.lang.String name) { super(); } } -
public final class Person { @org.jetbrains.annotations.NotNull() private final java.lang.String name
= null; @org.jetbrains.annotations.NotNull() public final java.lang.String getName() { return null; }c public Person(@org.jetbrains.annotations.NotNull() java.lang.String name) { super(); }a }b -
public final class Person { @org.jetbrains.annotations.NotNull() private final java.lang.String name
= null; @org.jetbrains.annotations.NotNull() public final java.lang.String getName() { return null; }c public Person(@org.jetbrains.annotations.NotNull() java.lang.String name) { super(); }a }b -
public final class Person { @org.jetbrains.annotations.NotNull() private final java.lang.String name
= null; @org.jetbrains.annotations.NotNull() public final java.lang.String getName() { return null; }c public Person(@org.jetbrains.annotations.NotNull() java.lang.String name) { super(); }a }b -
public final class Person { @org.jetbrains.annotations.NotNull() private final java.lang.String name
= null; @org.jetbrains.annotations.NotNull() public final java.lang.String getName() { return null; }c public Person(@org.jetbrains.annotations.NotNull() java.lang.String name) { super(); }a }b -
public final class Person { @org.jetbrains.annotations.NotNull() private final java.lang.String name
= null; @org.jetbrains.annotations.NotNull() public final java.lang.String getName() { return null; }c public Person(@org.jetbrains.annotations.NotNull() java.lang.String name) { super(); }a public Person() { super(); }d }b -
public final class Person { @org.jetbrains.annotations.NotNull() private final java.lang.String name
= null; @org.jetbrains.annotations.NotNull() public final java.lang.String getName() { return null; }c public Person(@org.jetbrains.annotations.NotNull() java.lang.String name) { super(); }a public Person() { super(); }d }b -
public final class Person { @org.jetbrains.annotations.NotNull() private final java.lang.String name
= null; @org.jetbrains.annotations.NotNull() public final java.lang.String getName() { return null; } public Person(@org.jetbrains.annotations.NotNull() java.lang.String name) { super(); } } -
@kotlin.Metadata( mv = {1, 1, 11}, bv = {1, 0,
2}, k = 1, d1 = {"\u0000\u0012\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0000\n\u0002\u0010\u000e\n\u0002\b\u0004\b\u0007\u0018 \u00002\u00020\u0001B\r\u0012\u0006\u0010\u0002\u001a\u00020\u0003\u00a2\u0006\u0002\u0010\u0004R\u0011\u0010\u0 002\u001a\u00020\u0003\u00a2\u0006\b\n\u0000\u001a\u0004\b\u0005\u0010\u0006\u00a8\u0006\u0007"}, d2 = {"Ltest/Person;", "", "name", "", "(Ljava/lang/String;)V", "getName", "()Ljava/lang/String;", "test- project"} )a public final class Person { @org.jetbrains.annotations.NotNull() private final java.lang.String name = null; @org.jetbrains.annotations.NotNull() public final java.lang.String getName() { return null; } public Person(@org.jetbrains.annotations.NotNull() java.lang.String name) { super(); } } -
@kotlin.Metadata( mv = {1, 1, 11}, bv = {1, 0,
2}, k = 1, d1 = {"\u0000\u0012\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0000\n\u0002\u0010\u000e\n\u0002\b\u0004\b\u0007\u0018 \u00002\u00020\u0001B\r\u0012\u0006\u0010\u0002\u001a\u00020\u0003\u00a2\u0006\u0002\u0010\u0004R\u0011\u0010\u0 002\u001a\u00020\u0003\u00a2\u0006\b\n\u0000\u001a\u0004\b\u0005\u0010\u0006\u00a8\u0006\u0007"}, d2 = {"Ltest/Person;", "", "name", "", "(Ljava/lang/String;)V", "getName", "()Ljava/lang/String;", "test- project"} )a public final class Person { @org.jetbrains.annotations.NotNull() private final java.lang.String name = null; @org.jetbrains.annotations.NotNull() public final java.lang.String getName() { return null; } public Person(@org.jetbrains.annotations.NotNull() java.lang.String name) { super(); } } -
annotation class Metadata( val k: Int = 1, val mv:
IntArray = [] )a -
annotation class Metadata( val k: Int = 1, val mv:
IntArray = [], val bv: IntArray = [] )a -
annotation class Metadata( val k: Int = 1, val mv:
IntArray = [], val bv: IntArray = [], val d1: Array<String> = [] )a -
annotation class Metadata( val k: Int = 1, val mv:
IntArray = [], val bv: IntArray = [], val d1: Array<String> = [], val d2: Array<String> = [] )a -
internal annotation class Metadata( val k: Int = 1, val
mv: IntArray = [], val bv: IntArray = [], val d1: Array<String> = [], val d2: Array<String> = [] )a -
@kotlin.Metadata( mv = {1, 1, 11}, bv = {1, 0,
2}, k = 1, d1 = {"\u0000\u0012\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0000\n\u0002\u0010\u000e\n\u0002\b\u0004\b\u0007\u0018 \u00002\u00020\u0001B\r\u0012\u0006\u0010\u0002\u001a\u00020\u0003\u00a2\u0006\u0002\u0010\u0004R\u0011\u0010\u0 002\u001a\u00020\u0003\u00a2\u0006\b\n\u0000\u001a\u0004\b\u0005\u0010\u0006\u00a8\u0006\u0007"}, d2 = {"Ltest/Person;", "", "name", "", "(Ljava/lang/String;)V", "getName", "()Ljava/lang/String;", "test- project"} )a public final class Person { @org.jetbrains.annotations.NotNull() private final java.lang.String name = null; @org.jetbrains.annotations.NotNull() public final java.lang.String getName() { return null; } public Person(@org.jetbrains.annotations.NotNull() java.lang.String name) { super(); } } -
@kotlin.Metadata( mv = {1, 1, 11}, bv = {1, 0,
2}, k = 1, d1 = {"\u0000\u0012\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n \u0000\n\u0002\u0010\u000e\n\u0002\b\u0004\b\u0007\u0018 \u00002\u00020\u0001B\r\u0012\u0006\u0010\u0002\u001a\u0 0020\u0003\u00a2\u0006\u0002\u0010\u0004R\u0011\u0010\u0 002\u001a\u00020\u0003\u00a2\u0006\b\n\u0000\u001a\u0004 \b\u0005\u0010\u0006\u00a8\u0006\u0007"}, d2 = {"Ltest/Person;", "", "name", "", "(Ljava/lang/ String;)V", "getName", "()Ljava/lang/String;", "test- project"} )a -
@kotlin.Metadata( mv = {1, 1, 11}, bv = {1, 0,
2}, k = 1, d1 = {"\u0000\u0012\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n \u0000\n\u0002\u0010\u000e\n\u0002\b\u0004\b\u0007\u0018 \u00002\u00020\u0001B\r\u0012\u0006\u0010\u0002\u001a\u0 0020\u0003\u00a2\u0006\u0002\u0010\u0004R\u0011\u0010\u0 002\u001a\u00020\u0003\u00a2\u0006\b\n\u0000\u001a\u0004 \b\u0005\u0010\u0006\u00a8\u0006\u0007"}, d2 = {"Ltest/Person;", "", "name", "", "(Ljava/lang/ String;)V", "getName", "()Ljava/lang/String;", "test- project"} )a -
@kotlin.Metadata( mv = {1, 1, 11}, bv = {1, 0,
2}, k = 1, d1 = {"\u0000\u0012\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n \u0000\n\u0002\u0010\u000e\n\u0002\b\u0004\b\u0007\u0018 \u00002\u00020\u0001B\r\u0012\u0006\u0010\u0002\u001a\u0 0020\u0003\u00a2\u0006\u0002\u0010\u0004R\u0011\u0010\u0 002\u001a\u00020\u0003\u00a2\u0006\b\n\u0000\u001a\u0004 \b\u0005\u0010\u0006\u00a8\u0006\u0007"}, d2 = {"Ltest/Person;", "", "name", "", "(Ljava/lang/ String;)V", "getName", "()Ljava/lang/String;", "test- project"} )a -
@kotlin.Metadata( mv = {1, 1, 11}, bv = {1, 0,
2}, k = 1, d1 = {"\u0000\u0012\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n \u0000\n\u0002\u0010\u000e\n\u0002\b\u0004\b\u0007\u0018 \u00002\u00020\u0001B\r\u0012\u0006\u0010\u0002\u001a\u0 0020\u0003\u00a2\u0006\u0002\u0010\u0004R\u0011\u0010\u0 002\u001a\u00020\u0003\u00a2\u0006\b\n\u0000\u001a\u0004 \b\u0005\u0010\u0006\u00a8\u0006\u0007"}, d2 = {"Ltest/Person;", "", "name", "", "(Ljava/lang/ String;)V", "getName", "()Ljava/lang/String;", "test- project"} )a -
@kotlin.Metadata( mv = {1, 1, 11}, bv = {1, 0,
2}, k = 1, d1 = {"\u0000\u0012\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n \u0000\n\u0002\u0010\u000e\n\u0002\b\u0004\b\u0007\u0018 \u00002\u00020\u0001B\r\u0012\u0006\u0010\u0002\u001a\u0 0020\u0003\u00a2\u0006\u0002\u0010\u0004R\u0011\u0010\u0 002\u001a\u00020\u0003\u00a2\u0006\b\n\u0000\u001a\u0004 \b\u0005\u0010\u0006\u00a8\u0006\u0007"}, d2 = {"Ltest/Person;", "", "name", "", "(Ljava/lang/ String;)V", "getName", "()Ljava/lang/String;", "test- project"} )a -
@kotlin.Metadata( mv = {1, 1, 11}, bv = {1, 0,
2}, k = 1, d1 = {"\u0000\u0012\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n \u0000\n\u0002\u0010\u000e\n\u0002\b\u0004\b\u0007\u0018 \u00002\u00020\u0001B\r\u0012\u0006\u0010\u0002\u001a\u0 0020\u0003\u00a2\u0006\u0002\u0010\u0004R\u0011\u0010\u0 002\u001a\u00020\u0003\u00a2\u0006\b\n\u0000\u001a\u0004 \b\u0005\u0010\u0006\u00a8\u0006\u0007"}, d2 = {"Ltest/Person;", "", "name", "", "(Ljava/lang/ String;)V", "getName", "()Ljava/lang/String;", "test- project"} )a -
@kotlin.Metadata( mv = {1, 1, 11}, bv = {1, 0,
2}, k = 1, d1 = {"\u0000\u0012\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n \u0000\n\u0002\u0010\u000e\n\u0002\b\u0004\b\u0007\u0018 \u00002\u00020\u0001B\r\u0012\u0006\u0010\u0002\u001a\u0 0020\u0003\u00a2\u0006\u0002\u0010\u0004R\u0011\u0010\u0 002\u001a\u00020\u0003\u00a2\u0006\b\n\u0000\u001a\u0004 \b\u0005\u0010\u0006\u00a8\u0006\u0007"}, d2 = {"Ltest/Person;", "", "name", "", "(Ljava/lang/ String;)V", "getName", "()Ljava/lang/String;", "test- project"} )a -
@kotlin.Metadata( mv = {1, 1, 11}, bv = {1, 0,
2}, k = 1, d1 = {"\u0000\u0012\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n \u0000\n\u0002\u0010\u000e\n\u0002\b\u0004\b\u0007\u0018 \u00002\u00020\u0001B\r\u0012\u0006\u0010\u0002\u001a\u0 0020\u0003\u00a2\u0006\u0002\u0010\u0004R\u0011\u0010\u0 002\u001a\u00020\u0003\u00a2\u0006\b\n\u0000\u001a\u0004 \b\u0005\u0010\u0006\u00a8\u0006\u0007"}, d2 = {"Ltest/Person;", "", "name", "", "(Ljava/lang/ String;)V", "getName", "()Ljava/lang/String;", "test- project"} )a -
@kotlin.Metadata( mv = {1, 1, 11}, bv = {1, 0,
2}, k = 1, d1 = {"\u0000\u0012\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n \u0000\n\u0002\u0010\u000e\n\u0002\b\u0004\b\u0007\u0018 \u00002\u00020\u0001B\r\u0012\u0006\u0010\u0002\u001a\u0 0020\u0003\u00a2\u0006\u0002\u0010\u0004R\u0011\u0010\u0 002\u001a\u00020\u0003\u00a2\u0006\b\n\u0000\u001a\u0004 \b\u0005\u0010\u0006\u00a8\u0006\u0007"}, d2 = {"Ltest/Person;", "", "name", "", "(Ljava/lang/ String;)V", "getName", "()Ljava/lang/String;", "test- project"} )a -
@kotlin.Metadata( mv = {1, 1, 11}, bv = {1, 0,
2}, k = 1, d1 = {"\u0000\u0012\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n \u0000\n\u0002\u0010\u000e\n\u0002\b\u0004\b\u0007\u0018 \u00002\u00020\u0001B\r\u0012\u0006\u0010\u0002\u001a\u0 0020\u0003\u00a2\u0006\u0002\u0010\u0004R\u0011\u0010\u0 002\u001a\u00020\u0003\u00a2\u0006\b\n\u0000\u001a\u0004 \b\u0005\u0010\u0006\u00a8\u0006\u0007"}, d2 = {"Ltest/Person;", "", "name", "", "(Ljava/lang/ String;)V", "getName", "()Ljava/lang/String;", "test- project"} )a -
@kotlin.Metadata( mv = {1, 1, 11}, bv = {1, 0,
2}, k = 1, d1 = {"\u0000\u0012\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n \u0000\n\u0002\u0010\u000e\n\u0002\b\u0004\b\u0007\u0018 \u00002\u00020\u0001B\r\u0012\u0006\u0010\u0002\u001a\u0 0020\u0003\u00a2\u0006\u0002\u0010\u0004R\u0011\u0010\u0 002\u001a\u00020\u0003\u00a2\u0006\b\n\u0000\u001a\u0004 \b\u0005\u0010\u0006\u00a8\u0006\u0007"}, d2 = {"Ltest/Person;", "", "name", "", "(Ljava/lang/ String;)V", "getName", "()Ljava/lang/String;", "test- project"} )a -
BUT! • Undocumented and not stable • Only applies to
Kotlin sources -
BUT! • Undocumented and not stable • Only applies to
Kotlin sources • Tooling can be painful -
BUT! • Undocumented and not stable • Only applies to
Kotlin sources • Tooling can be painful • github.com/hzsweers/redacted-compiler- plugin -
BUT! • Undocumented and not stable • Only applies to
Kotlin sources • Tooling can be painful • github.com/hzsweers/redacted-compiler- plugin • The talk title was Annotation Processing in a Kotlin -
BUT!* • Undocumented and not stable • Only applies to
Kotlin sources • Tooling can be painful • github.com/hzsweers/redacted-compiler- plugin • The talk title was Annotation Processing in a Kotlin -
when (typeElement.kotlinMetadata) { is KotlinPackageMetadata -> is KotlinClassMetadata -> is
KotlinFileMetadata -> is KotlinMultiFileClassPartMetadata -> is KotlinMultiFileClassFacadeMetadata -> is KotlinSyntheticClassMetadata -> is KotlinUnknownMetadata -> }a -
when (metadata) { is KotlinClassMetadata -> { val classData: ClassData
= metadata.data }a }b -
when (metadata) { is KotlinClassMetadata -> { val classData: ClassData
= metadata.data val proto: ProtoBuf.Class = classData.classProto }a }b -
when (metadata) { is KotlinClassMetadata -> { val classData: ClassData
= metadata.data val proto: ProtoBuf.Class = classData.classProto }a }b -
when (metadata) { is KotlinClassMetadata -> { val classData: ClassData
= metadata.data val proto: ProtoBuf.Class = classData.classProto val nameResolver = classData.nameResolver }a }b -
@kotlin.Metadata( mv = {1, 1, 11}, bv = {1, 0,
2}, k = 1, d1 = {"\u0000\u0012\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n \u0000\n\u0002\u0010\u000e\n\u0002\b\u0004\b\u0007\u0018 \u00002\u00020\u0001B\r\u0012\u0006\u0010\u0002\u001a\u0 0020\u0003\u00a2\u0006\u0002\u0010\u0004R\u0011\u0010\u0 002\u001a\u00020\u0003\u00a2\u0006\b\n\u0000\u001a\u0004 \b\u0005\u0010\u0006\u00a8\u0006\u0007"}, d2 = {"Ltest/Person;", "", "name", "", "(Ljava/lang/ String;)V", "getName", "()Ljava/lang/String;", "test- project"} )a -
@kotlin.Metadata( mv = {1, 1, 11}, bv = {1, 0,
2}, k = 1, d1 = {"\u0000\u0012\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n \u0000\n\u0002\u0010\u000e\n\u0002\b\u0004\b\u0007\u0018 \u00002\u00020\u0001B\r\u0012\u0006\u0010\u0002\u001a\u0 0020\u0003\u00a2\u0006\u0002\u0010\u0004R\u0011\u0010\u0 002\u001a\u00020\u0003\u00a2\u0006\b\n\u0000\u001a\u0004 \b\u0005\u0010\u0006\u00a8\u0006\u0007"}, d2 = {"Ltest/Person;", "", "name", "", "(Ljava/lang/ String;)V", "getName", "()Ljava/lang/String;", "test- project"} )a -
@kotlin.Metadata( mv = {1, 1, 11}, bv = {1, 0,
2}, k = 1, d1 = {"\u0000\u0012\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n \u0000\n\u0002\u0010\u000e\n\u0002\b\u0004\b\u0007\u0018 \u00002\u00020\u0001B\r\u0012\u0006\u0010\u0002\u001a\u0 0020\u0003\u00a2\u0006\u0002\u0010\u0004R\u0011\u0010\u0 002\u001a\u00020\u0003\u00a2\u0006\b\n\u0000\u001a\u0004 \b\u0005\u0010\u0006\u00a8\u0006\u0007"}, d2 = {"Ltest/Person;", "", "name", "", "(Ljava/lang/ String;)V", "getName", "()Ljava/lang/String;", "test- project"} )a -
when (metadata) { is KotlinClassMetadata -> { val classData: ClassData
= metadata.data val proto: ProtoBuf.Class = classData.classProto val nameResolver = classData.nameResolver }a }b -
val proto: ProtoBuf.Class = classData.classProto val constructors: List<Constructor> = proto.constructorList
val properties: List<Property> = proto.propertyList val functions: List<Function> = proto.functionList -
class Person(val name: String = "Joe") val name = TODO()
val mutable = TODO() -
class Person(val name: String = "Joe") val name = TODO()
val mutable = TODO() val nullable = TODO() -
class Person(val name: String = "Joe") val name = TODO()
val mutable = TODO() val nullable = TODO() val hasDefault = TODO() -
class Person(val name: String = "Joe") val name = TODO()
val mutable = TODO() val nullable = TODO() val hasDefault = TODO() -
class Person(val name: String = "Joe") val proto = classData.classProto
val constr = proto.constructorList.find { it.isPrimary } val name = TODO() val mutable = TODO() val nullable = TODO() val hasDefault = TODO() -
class Person(val name: String = "Joe") val proto = classData.classProto
val constr = proto.constructorList.find { it.isPrimary } val name = TODO() val mutable = TODO() val nullable = TODO() val hasDefault = TODO() -
class Person(val name: String = "Joe") val proto = classData.classProto
val constr = proto.constructorList.find { it.isPrimary } val nameParam = constr.valueParameterList[0] val name = TODO() val mutable = TODO() val nullable = TODO() val hasDefault = TODO() -
class Person(val name: String = "Joe") val proto = classData.classProto
val constr = proto.constructorList.find { it.isPrimary } val nameParam = constr.valueParameterList[0] val name = nameResolver.getString(nameParam.name) val mutable = TODO() val nullable = TODO() val hasDefault = TODO() -
class Person(val name: String = "Joe") val proto = classData.classProto
val constr = proto.constructorList.find { it.isPrimary } val nameParam = constr.valueParameterList[0] val name = nameResolver.getString(nameParam.name) val mutable = TODO() val nullable = TODO() val hasDefault = nameParam.declaresDefaultValue -
class Person(val name: String = "Joe") val proto = classData.classProto
val constr = proto.constructorList.find { it.isPrimary } val nameParam = constr.valueParameterList[0] val name = nameResolver.getString(nameParam.name) val mutable = TODO() val nullable = nameParam.type.nullable val hasDefault = nameParam.declaresDefaultValue -
class Person(val name: String = "Joe") val proto = classData.classProto
val constr = proto.constructorList.find { it.isPrimary } val nameParam = constr.valueParameterList[0] val name = nameResolver.getString(nameParam.name) val mutable = proto.propertyList[0].isVar val nullable = nameParam.type.nullable val hasDefault = nameParam.declaresDefaultValue -
class Person(val name: String = "Joe") val proto = classData.classProto
val constr = proto.constructorList.find { it.isPrimary } val nameParam = constr.valueParameterList[0] val name = nameResolver.getString(nameParam.name) val mutable = proto.propertyList[0].isVar val nullable = nameParam.type.nullable val hasDefault = nameParam.declaresDefaultValue -
–Chris Banes, somewhere online once upon a time “I wish
I could use copy but like, conditionally set properties” -
data class Taco( val seasoning: Set<String>, val isBreakfast: Boolean )a
val taco = Taco(setOf("el paso"), true) -
data class Taco( val seasoning: Set<String>, val isBreakfast: Boolean )a
val taco = Taco(setOf("el paso"), true) val newTaco = taco.copyDynamic({ if (isBreakfastTime) { isBreakfast = true } } -
class TacoDynamicBuilder constructor( private val source: Taco) { var seasoning:
Set<String> = source.seasoning var isBreakfast: Boolean = source.isBreakfast fun build(): Taco = source.copy(seasoning, isBreakfast) }a fun Taco.copyDynamic( copyBlock: TacoDynamicBuilder.() -> Unit): Taco { return TacoDynamicBuilder(this) .also { copyBlock(it) } .build() }b -
class TacoDynamicBuilder constructor( private val source: Taco) { var seasoning:
Set<String> = source.seasoning var isBreakfast: Boolean = source.isBreakfast fun build(): Taco = source.copy(seasoning, isBreakfast) }a fun Taco.copyDynamic( copyBlock: TacoDynamicBuilder.() -> Unit): Taco { return TacoDynamicBuilder(this) .also { copyBlock(it) } .build() }b -
class TacoDynamicBuilder constructor( private val source: Taco) { var seasoning:
Set<String> = source.seasoning var isBreakfast: Boolean = source.isBreakfast fun build(): Taco = source.copy(seasoning, isBreakfast) }a fun Taco.copyDynamic( copyBlock: TacoDynamicBuilder.() -> Unit): Taco { return TacoDynamicBuilder(this) .also { copyBlock(it) } .build() }b -
@CopyDynamic data class Taco( val seasoning: Set<String>, val isBreakfast: Boolean
)aa •Read the proto off Taco •Ensure it's a data class •Find constructor •Get all params + matching properties •Generate with KotlinPoet -
@CopyDynamic data class Taco( val seasoning: Set<String>, val isBreakfast: Boolean
)a •Read the proto off Taco •Ensure it's a data class •Find constructor •Get all params + matching properties •Generate with KotlinPoet -
@CopyDynamic data class Taco( val seasoning: Set<String>, val isBreakfast: Boolean
)a •Read the proto off Taco •Ensure it's a data class •Find constructor •Get all params + matching properties •Generate with KotlinPoet -
@CopyDynamic data class Taco( val seasoning: Set<String>, val isBreakfast: Boolean
)a •Read the proto off Taco •Ensure it's a data class •Find constructor •Get all params + matching properties •Generate with KotlinPoet -
@CopyDynamic data class Taco( val seasoning: Set<String>, val isBreakfast: Boolean
)a •Read the proto off Taco •Ensure it's a data class •Find constructor •Get all params + matching properties •Generate with KotlinPoet -
@CopyDynamic data class Taco( val seasoning: Set<String>, val isBreakfast: Boolean
)a •Read the proto off Taco •Ensure it's a data class •Find constructor •Get all params + matching properties •Generate with KotlinPoet -
@CopyDynamic data class Taco( val seasoning: Set<String>, val isBreakfast: Boolean
)a •Read the proto off Taco •Ensure it's a data class •Find constructor •Get all params + matching properties •Generate with KotlinPoet "kapt.kotlin.generated" -
Quirks & Caveats • Not public yet • Not incremental
• No multiplatform -
Quirks & Caveats • Not public yet • Not incremental
• No multiplatform • (Kapt) No multiple rounds* -
Quirks & Caveats • Not public yet • Not incremental
• No multiplatform • (Kapt) No multiple rounds* • (Kapt) Varying build system support -
Quirks & Caveats • Not public yet • Not incremental
• No multiplatform • (Kapt) No multiple rounds* • (Kapt) Varying build system support -
–you? “I have a processor and want to support kotlin
today! What should I use?” -
“I have a processor and want to support kotlin today!
What should I use?” “kotlin-metadata, probably” -
“I have a processor and want to support kotlin today!
What should I use?” “kotlin-metadata, probably” “and KotlinPoet” -
Thanks! • kotlin-metadata: github.com/takhion/kotlin-metadata • kotlinx-metadata: https://github.com/JetBrains/kotlin/tree/master/ libraries/kotlinx-metadata/jvm • Moshi:
github.com/square/moshi • Km API prototype: github.com/square/moshi/pull/642 • CopyDynamic: github.com/hzsweers/copydynamic • FR for value type API: youtrack.jetbrains.com/issue/KT-26602 • Multiple rounds demo: github.com/Takhion/generate-kotlin-multiple-rounds • redacted compiler plugin: github.com/hzsweers/redacted-compiler-plugin • KotlinPoet: github.com/square/kotlinpoet