Slide 1

Slide 1 text

Kotlin-friendly Annotation Processing Sergey Ryabov

Slide 2

Slide 2 text

JSR-269

Slide 3

Slide 3 text

• Annotation processing works inside javac JSR-269

Slide 4

Slide 4 text

• Annotation processing works inside javac • Separate Annotation Processors are pluggable JSR-269

Slide 5

Slide 5 text

• Annotation processing works inside javac • Separate Annotation Processors are pluggable • Gradle’s annotationProcessor dependencies configuration JSR-269

Slide 6

Slide 6 text

• Annotation processing works inside javac • Separate Annotation Processors are pluggable • Gradle’s annotationProcessor dependencies configuration • /META-INF/services/javax.annotation.processing.Processor 
 declares your AP’s main entry point JSR-269

Slide 7

Slide 7 text

• Annotation processing works inside javac • Separate Annotation Processors are pluggable • Gradle’s annotationProcessor dependencies configuration • /META-INF/services/javax.annotation.processing.Processor 
 declares your AP’s main entry point • Multiple rounds of processing JSR-269

Slide 8

Slide 8 text

*.java JSR-269

Slide 9

Slide 9 text

*.java apt JSR-269

Slide 10

Slide 10 text

*.java apt javac JSR-269

Slide 11

Slide 11 text

Kotlin-way

Slide 12

Slide 12 text

• APT can’t process Kotlin sources — build KAPT Kotlin-way

Slide 13

Slide 13 text

• APT can’t process Kotlin sources — build KAPT • Kotlin’s own JSR-269 implementation? Kotlin-way

Slide 14

Slide 14 text

• APT can’t process Kotlin sources — build KAPT • Kotlin’s own JSR-269 implementation? • Generate Java code/stubs from Kotlin? Kotlin-way

Slide 15

Slide 15 text

• APT can’t process Kotlin sources — build KAPT • Kotlin’s own JSR-269 implementation? • Generate Java code/stubs from Kotlin? • Precompile Kotlin classes and feed them to javac? Kotlin-way

Slide 16

Slide 16 text

• APT can’t process Kotlin sources — build KAPT • Kotlin’s own JSR-269 implementation? • Generate Java code/stubs from Kotlin? • Precompile Kotlin classes and feed them to javac? Kotlin-way

Slide 17

Slide 17 text

• APT can’t process Kotlin sources — build KAPT • Kotlin’s own JSR-269 implementation? • Generate Java code/stubs from Kotlin? • Precompile Kotlin classes and feed them to javac? Kotlin-way

Slide 18

Slide 18 text

• APT can’t process Kotlin sources — build KAPT • Kotlin’s own JSR-269 implementation? • Generate Java code/stubs from Kotlin? • Precompile Kotlin classes and feed them to javac? Kotlin-way

Slide 19

Slide 19 text

• APT can’t process Kotlin sources — build KAPT • Kotlin’s own JSR-269 implementation? • Generate Java code/stubs from Kotlin? • Precompile Kotlin classes and feed them to javac? • No multiple APT rounds over generated Kotlin files Kotlin-way

Slide 20

Slide 20 text

*.kt Kotlin-way

Slide 21

Slide 21 text

*.kt kapt Kotlin-way

Slide 22

Slide 22 text

*.kt kapt *.java Kotlin-way

Slide 23

Slide 23 text

*.kt kapt apt *.java Kotlin-way

Slide 24

Slide 24 text

*.kt kapt kotlinc apt *.java Kotlin-way

Slide 25

Slide 25 text

class KotlinProcessor : AbstractProcessor() { } JSR-269

Slide 26

Slide 26 text

class KotlinProcessor : AbstractProcessor() { override fun getSupportedOptions(): Set } JSR-269

Slide 27

Slide 27 text

class KotlinProcessor : AbstractProcessor() { override fun getSupportedOptions(): Set override fun getSupportedAnnotationTypes(): Set } JSR-269

Slide 28

Slide 28 text

class KotlinProcessor : AbstractProcessor() { override fun getSupportedOptions(): Set override fun getSupportedAnnotationTypes(): Set override fun getSupportedSourceVersion(): SourceVersion } JSR-269

Slide 29

Slide 29 text

class KotlinProcessor : AbstractProcessor() { override fun getSupportedOptions(): Set override fun getSupportedAnnotationTypes(): Set override fun getSupportedSourceVersion(): SourceVersion override fun init(processingEnv: ProcessingEnvironment) } JSR-269

Slide 30

Slide 30 text

class KotlinProcessor : AbstractProcessor() { override fun getSupportedOptions(): Set override fun getSupportedAnnotationTypes(): Set override fun getSupportedSourceVersion(): SourceVersion override fun init(processingEnv: ProcessingEnvironment) override fun process( annotations: Set, roundEnv: RoundEnvironment ): Boolean } JSR-269

Slide 31

Slide 31 text

class KotlinProcessor : AbstractProcessor() { override fun getSupportedOptions(): Set override fun getSupportedAnnotationTypes(): Set override fun getSupportedSourceVersion(): SourceVersion override fun init(processingEnv: ProcessingEnvironment) override fun process( annotations: Set, roundEnv: RoundEnvironment ): Boolean } JSR-269

Slide 32

Slide 32 text

Problems

Slide 33

Slide 33 text

Problems • No info about Kotlin-specifics is available for Annotation Processors

Slide 34

Slide 34 text

Problems • No info about Kotlin-specifics is available for Annotation Processors • No Nullability

Slide 35

Slide 35 text

Problems • No info about Kotlin-specifics is available for Annotation Processors • No Nullability • No default arguments

Slide 36

Slide 36 text

Problems • No info about Kotlin-specifics is available for Annotation Processors • No Nullability • No default arguments • No sealed & data classes

Slide 37

Slide 37 text

Problems • No info about Kotlin-specifics is available for Annotation Processors • No Nullability • No default arguments • No sealed & data classes • …

Slide 38

Slide 38 text

Problems • No info about Kotlin-specifics is available for Annotation Processors • No Nullability • No default arguments • No sealed & data classes • … • No nothing!

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

Dad! APT can’t get any Kotlin info. And StackOverflow can’t help!

Slide 41

Slide 41 text

Dad! APT can’t get any Kotlin info. And StackOverflow can’t help! If even StackOverflow keeps silence…

Slide 42

Slide 42 text

Dad! APT can’t get any Kotlin info. And StackOverflow can’t help! If even StackOverflow keeps silence… B-b-b-but compiler..?

Slide 43

Slide 43 text

Dad! APT can’t get any Kotlin info. And StackOverflow can’t help! If even StackOverflow keeps silence… B-b-b-but compiler..? Well, this city has a Hero!

Slide 44

Slide 44 text

@Metadata

Slide 45

Slide 45 text

@Metadata( mv = {1, 1, 11}, bv = {1, 0, 2}, k = 1, d1 = {"\u0000\u0080\u0001\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u00… d2 = {""Lxyz/ryabov/gsonkot/User;", "", "id", "", "username", "", "isAdmin ) public final class User { ... }

Slide 46

Slide 46 text

@Metadata

Slide 47

Slide 47 text

@Metadata • Provides Kotlin-specific info

Slide 48

Slide 48 text

@Metadata • Provides Kotlin-specific info • Class-level annotation

Slide 49

Slide 49 text

@Metadata • Provides Kotlin-specific info • Class-level annotation • Accessible both at compile time and runtime

Slide 50

Slide 50 text

@Metadata • Provides Kotlin-specific info • Class-level annotation • Accessible both at compile time and runtime • Used by compiler as well

Slide 51

Slide 51 text

@Metadata • mv • bv • k • d1 • d2 • xs • xi • pn

Slide 52

Slide 52 text

@Metadata • metadataVersion • bytecodeVersion • kind • data1 • data2 • extraString • extraInt • packageName

Slide 53

Slide 53 text

What can we do with that?

Slide 54

Slide 54 text

What can we do with that? Parse data1 Protocol Buffers using kind to define its format

Slide 55

Slide 55 text

What can we do with that? Parse data1 Protocol Buffers using kind to define its format https://github.com/JetBrains/kotlin/blob/master/core/metadata/src/metadata.proto

Slide 56

Slide 56 text

Protocol Buffers

Slide 57

Slide 57 text

Protocol Buffers

Slide 58

Slide 58 text

What tools do we have?

Slide 59

Slide 59 text

What tools do we have? • Hands

Slide 60

Slide 60 text

What tools do we have? • Hands

Slide 61

Slide 61 text

What tools do we have? • Hands • Use kotlin-metadata by Eugenio Marletti
 https://github.com/Takhion/kotlin-metadata

Slide 62

Slide 62 text

What tools do we have? • Hands • Use kotlin-metadata by Eugenio Marletti
 https://github.com/Takhion/kotlin-metadata • Or…


Slide 63

Slide 63 text

What tools do we have? • Hands • Use kotlin-metadata by Eugenio Marletti
 https://github.com/Takhion/kotlin-metadata • Or… kotlinx-metadata by JetBrains
 https://github.com/JetBrains/kotlin/tree/master/libraries/kotlinx-metadata/jvm

Slide 64

Slide 64 text

Two patterns

Slide 65

Slide 65 text

Two patterns • Visitors API

Slide 66

Slide 66 text

Two patterns • Visitors API • DOM-like API

Slide 67

Slide 67 text

Visitors API

Slide 68

Slide 68 text

Visitors API public interface Element { R accept(ElementVisitor v, P p); }

Slide 69

Slide 69 text

Visitors API public interface ElementVisitor { R visit(Element e, P p); }

Slide 70

Slide 70 text

Visitors API public interface ElementVisitor { R visit(Element e, P p); R visitPackage(PackageElement e, P p); }

Slide 71

Slide 71 text

Visitors API public interface ElementVisitor { R visit(Element e, P p); R visitPackage(PackageElement e, P p); R visitType(TypeElement e, P p); }

Slide 72

Slide 72 text

Visitors API public interface ElementVisitor { R visit(Element e, P p); R visitPackage(PackageElement e, P p); R visitType(TypeElement e, P p); R visitVariable(VariableElement e, P p); }

Slide 73

Slide 73 text

Visitors API public interface ElementVisitor { R visit(Element e, P p); R visitPackage(PackageElement e, P p); R visitType(TypeElement e, P p); R visitVariable(VariableElement e, P p); R visitExecutable(ExecutableElement e, P p); }

Slide 74

Slide 74 text

Visitors API public interface ElementVisitor { R visit(Element e, P p); R visitPackage(PackageElement e, P p); R visitType(TypeElement e, P p); R visitVariable(VariableElement e, P p); R visitExecutable(ExecutableElement e, P p); R visitTypeParameter(TypeParameterElement e, P p); }

Slide 75

Slide 75 text

Visitors API public interface ElementVisitor { R visit(Element e, P p); R visitPackage(PackageElement e, P p); R visitType(TypeElement e, P p); R visitVariable(VariableElement e, P p); R visitExecutable(ExecutableElement e, P p); R visitTypeParameter(TypeParameterElement e, P p); R visitUnknown(Element e, P p); }

Slide 76

Slide 76 text

Visitors API

Slide 77

Slide 77 text

Visitors API class KotlinProcessor : AbstractProcessor() { override fun process( annotations: Set, roundEnv: RoundEnvironment ): Boolean { return false } }

Slide 78

Slide 78 text

Visitors API class KotlinProcessor : AbstractProcessor() { override fun process( annotations: Set, roundEnv: RoundEnvironment ): Boolean { for (element in roundEnv.getElementsAnnotatedWith(Builder::class.java)) { }. return false } }

Slide 79

Slide 79 text

Visitors API class KotlinProcessor : AbstractProcessor() { override fun process( annotations: Set, roundEnv: RoundEnvironment ): Boolean { for (element in roundEnv.getElementsAnnotatedWith(Builder::class.java)) { element.accept(visitor, null) }.. return false } }

Slide 80

Slide 80 text

Visitors API class KotlinProcessor : AbstractProcessor() { override fun process( annotations: Set, roundEnv: RoundEnvironment ): Boolean { for (element in roundEnv.getElementsAnnotatedWith(Builder::class.java)) { val visitor = object : ElementScanner7() { override fun visitVariable(e: VariableElement, p: Unit?) { handleVariable(e) } } element.accept(visitor, null) }. return false } }

Slide 81

Slide 81 text

Visitors API class KotlinProcessor : AbstractProcessor() { override fun process( annotations: Set, roundEnv: RoundEnvironment ): Boolean { for (element in roundEnv.getElementsAnnotatedWith(Builder::class.java)) { val visitor = object : ElementScanner7() { override fun visitVariable(e: VariableElement, p: Unit?) { handleVariable(e) } } element.accept(visitor, null) }. return false } }

Slide 82

Slide 82 text

Two patterns • Visitors API • DOM-like API

Slide 83

Slide 83 text

DOM-like API

Slide 84

Slide 84 text

DOM-like API public interface Element { }

Slide 85

Slide 85 text

DOM-like API public interface Element { Element getEnclosingElement(); }

Slide 86

Slide 86 text

DOM-like API public interface Element { Element getEnclosingElement(); List getEnclosedElements(); }

Slide 87

Slide 87 text

DOM-like API public interface Element { Element getEnclosingElement(); List getEnclosedElements(); ElementKind getKind(); }

Slide 88

Slide 88 text

DOM-like API class KotlinProcessor : AbstractProcessor() { override fun process( annotations: Set, roundEnv: RoundEnvironment ): Boolean { for (element in roundEnv.getElementsAnnotatedWith(BindView::class.java)) { } return false } }

Slide 89

Slide 89 text

DOM-like API class KotlinProcessor : AbstractProcessor() { override fun process( annotations: Set, roundEnv: RoundEnvironment ): Boolean { for (element in roundEnv.getElementsAnnotatedWith(BindView::class.java)) { } return false } }

Slide 90

Slide 90 text

DOM-like API class KotlinProcessor : AbstractProcessor() { override fun process( annotations: Set, roundEnv: RoundEnvironment ): Boolean { for (element in roundEnv.getElementsAnnotatedWith(BindView::class.java)) { if (element.kind == ElementKind.FIELD) { handleVariable(element as VariableElement) }. } return false } }

Slide 91

Slide 91 text

DOM-like API class KotlinProcessor : AbstractProcessor() { override fun process( annotations: Set, roundEnv: RoundEnvironment ): Boolean { for (element in roundEnv.getElementsAnnotatedWith(BindView::class.java)) { if (element.kind == ElementKind.FIELD) { handleVariable(element as VariableElement) }. } return false } }

Slide 92

Slide 92 text

kotlinx-metadata

Slide 93

Slide 93 text

kotlinx-metadata Has only low-level Visitors API

Slide 94

Slide 94 text

Visitors API

Slide 95

Slide 95 text

Visitors API abstract class KmFunctionVisitor abstract class KmPropertyVisitor abstract class KmPackageVisitor abstract class KmClassVisitor abstract class KmConstructorVisitor abstract class KmValueParameterVisitor abstract class KmLambdaVisitor abstract class KmTypeVisitor abstract class KmTypeAliasVisitor abstract class KmTypeParameterVisitor

Slide 96

Slide 96 text

Visitors API abstract class KmClassVisitor { open fun visit*(...) }.

Slide 97

Slide 97 text

Visitors API abstract class KmClassVisitor { open fun visitTypeParameter( flags: Flags, name: String, id: Int, variance: KmVariance ): KmTypeParameterVisitor? }.

Slide 98

Slide 98 text

Visitors API class Type abstract class KmClassVisitor { open fun visitTypeParameter( flags: Flags, name: String, id: Int, variance: KmVariance ): KmTypeParameterVisitor? }.

Slide 99

Slide 99 text

Visitors API class Type abstract class KmClassVisitor { open fun visitTypeParameter( flags: Flags, name: String, id: Int, variance: KmVariance ): KmTypeParameterVisitor? }.

Slide 100

Slide 100 text

Visitors API class Type abstract class KmClassVisitor { open fun visitTypeParameter( flags: Flags, name: String, id: Int, variance: KmVariance ): KmTypeParameterVisitor? }.

Slide 101

Slide 101 text

Visitors API class Type abstract class KmClassVisitor { open fun visitTypeParameter( flags: Flags, name: String, id: Int, variance: KmVariance ): KmTypeParameterVisitor? }.

Slide 102

Slide 102 text

Visitors API class Type abstract class KmClassVisitor { open fun visitTypeParameter( flags: Flags, name: String, id: Int, variance: KmVariance ): KmTypeParameterVisitor? }.

Slide 103

Slide 103 text

Visitors API class Type abstract class KmClassVisitor { open fun visitTypeParameter( flags: Flags, name: String, id: Int, variance: KmVariance ): KmTypeParameterVisitor? }.

Slide 104

Slide 104 text

Visitors API class Type abstract class KmClassVisitor { open fun visitTypeParameter( flags: Flags, name: String, id: Int, variance: KmVariance ): KmTypeParameterVisitor? }.

Slide 105

Slide 105 text

Visitors API abstract class KmClassVisitor { open fun visitTypeParameter( flags: Flags, name: String, id: Int, variance: KmVariance ): KmTypeParameterVisitor? }.

Slide 106

Slide 106 text

Visitors API abstract class KmClassVisitor { open fun visitTypeParameter( flags: Flags, name: String, id: Int, variance: KmVariance ): KmTypeParameterVisitor? }. interface ElementVisitor { R visitTypeParameter( TypeParameterElement e, P p ); }

Slide 107

Slide 107 text

Visitors API abstract class KmClassVisitor { open fun visitTypeParameter( flags: Flags, name: String, id: Int, variance: KmVariance ): KmTypeParameterVisitor? }. interface ElementVisitor { R visitTypeParameter( TypeParameterElement e, P p ); }

Slide 108

Slide 108 text

Visitors API abstract class KmClassVisitor { open fun visitTypeParameter( flags: Flags, name: String, id: Int, variance: KmVariance ): KmTypeParameterVisitor? }. interface ElementVisitor { R visitTypeParameter( TypeParameterElement e, P p ); }

Slide 109

Slide 109 text

GsonTypeAdapter

Slide 110

Slide 110 text

GsonTypeAdapter @GsonAdapter data class User( val id: Long, val username: String, val isAdmin: Boolean )

Slide 111

Slide 111 text

val annotation = GsonAdapter::class.java for (element in roundEnv.getElementsAnnotatedWith(annotation)) { val input = getInputFrom(element) ?: continue if (!input.generateAndWrite()) return true }

Slide 112

Slide 112 text

val annotation = GsonAdapter::class.java for (element in roundEnv.getElementsAnnotatedWith(annotation)) { val input = getInputFrom(element) ?: continue if (!input.generateAndWrite()) return true }

Slide 113

Slide 113 text

val metadata = getMetadata(element)

Slide 114

Slide 114 text

fun getMetadata(element: Element): KotlinClassMetadata? { val annotation = element.getAnnotation(Metadata::class.java) val header = annotation.run { KotlinClassHeader(k, mv, bv, d1, d2, xs, pn, xi) }. return KotlinClassMetadata.read(header) }.

Slide 115

Slide 115 text

fun getMetadata(element: Element): KotlinClassMetadata? { val annotation = element.getAnnotation(Metadata::class.java) val header = annotation.run { KotlinClassHeader(k, mv, bv, d1, d2, xs, pn, xi) }. return KotlinClassMetadata.read(header) }.

Slide 116

Slide 116 text

fun getMetadata(element: Element): KotlinClassMetadata? { val annotation = element.getAnnotation(Metadata::class.java) val header = annotation.run { KotlinClassHeader(k, mv, bv, d1, d2, xs, pn, xi) }. return KotlinClassMetadata.read(header) }.

Slide 117

Slide 117 text

fun getMetadata(element: Element): KotlinClassMetadata? { val annotation = element.getAnnotation(Metadata::class.java) val header = annotation.run { KotlinClassHeader(k, mv, bv, d1, d2, xs, pn, xi) }. return KotlinClassMetadata.read(header) }.

Slide 118

Slide 118 text

sealed class KotlinClassMetadata { class Class : KotlinClassMetadata class FileFacade : KotlinClassMetadata class SyntheticClass : KotlinClassMetadata class MultiFileClassFacade : KotlinClassMetadata class MultiFileClassPart : KotlinClassMetadata class Unknown : KotlinClassMetadata }

Slide 119

Slide 119 text

sealed class KotlinClassMetadata { class Class : KotlinClassMetadata class FileFacade : KotlinClassMetadata class SyntheticClass : KotlinClassMetadata class MultiFileClassFacade : KotlinClassMetadata class MultiFileClassPart : KotlinClassMetadata class Unknown : KotlinClassMetadata }

Slide 120

Slide 120 text

val metadata = getMetadata(element)

Slide 121

Slide 121 text

val metadata = getMetadata(element) if (metadata !is KotlinClassMetadata.Class) { errorMustBeKotlinClass(element) return null }.

Slide 122

Slide 122 text

val metadata = getMetadata(element) if (metadata !is KotlinClassMetadata.Class) { errorMustBeKotlinClass(element) return null }. metadata.accept(object : KmClassVisitor() { })

Slide 123

Slide 123 text

val metadata = getMetadata(element) if (metadata !is KotlinClassMetadata.Class) { errorMustBeKotlinClass(element) return null }. metadata.accept(object : KmClassVisitor() { override fun visit(flags: Flags, name: ClassName) { } })

Slide 124

Slide 124 text

val metadata = getMetadata(element) if (metadata !is KotlinClassMetadata.Class) { errorMustBeKotlinClass(element) return null }. metadata.accept(object : KmClassVisitor() { override fun visit(flags: Flags, name: ClassName) { pkg = name.substringBeforeLast('/').fqName() className = name.substringAfterLast('/') fqClassName = name.fqName() } })

Slide 125

Slide 125 text

return null }. metadata.accept(object : KmClassVisitor() { override fun visit(flags: Flags, name: ClassName) { pkg = name.substringBeforeLast('/').fqName() className = name.substringAfterLast('/') fqClassName = name.fqName() } override fun visitConstructor(flags: Flags): KmConstructorVisitor? { } })

Slide 126

Slide 126 text

return null }. metadata.accept(object : KmClassVisitor() { override fun visit(flags: Flags, name: ClassName) { pkg = name.substringBeforeLast('/').fqName() className = name.substringAfterLast('/') fqClassName = name.fqName() } override fun visitConstructor(flags: Flags): KmConstructorVisitor? { if (!flags.isPrimaryConstructor) return null } })

Slide 127

Slide 127 text

val Flags.isPrimaryConstructor: Boolean get() = Flag.Constructor.IS_PRIMARY(this)

Slide 128

Slide 128 text

val Flags.isNullableType: Boolean get() = Flag.Type.IS_NULLABLE(this) val Flags.isDataClass: Boolean get() = Flag.Class.IS_DATA(this) val Flags.isPrimaryConstructor: Boolean get() = Flag.Constructor.IS_PRIMARY(this) val Flags.hasDefaultValue: Boolean get() = Flag.ValueParameter.DECLARES_DEFAULT_VALUE(this)

Slide 129

Slide 129 text

return null }. metadata.accept(object : KmClassVisitor() { override fun visit(flags: Flags, name: ClassName) { pkg = name.substringBeforeLast('/').fqName() className = name.substringAfterLast('/') fqClassName = name.fqName() } override fun visitConstructor(flags: Flags): KmConstructorVisitor? { if (!flags.isPrimaryConstructor) return null } })

Slide 130

Slide 130 text

pkg = name.substringBeforeLast('/').fqName() className = name.substringAfterLast('/') fqClassName = name.fqName() } override fun visitConstructor(flags: Flags): KmConstructorVisitor? { if (!flags.isPrimaryConstructor) return null return object : KmConstructorVisitor() { override fun visitValueParameter( flags: Flags, name: String ): KmValueParameterVisitor? { }. }. } })

Slide 131

Slide 131 text

} override fun visitConstructor(flags: Flags): KmConstructorVisitor? { if (!flags.isPrimaryConstructor) return null return object : KmConstructorVisitor() { override fun visitValueParameter( flags: Flags, name: String ): KmValueParameterVisitor? { return object : KmValueParameterVisitor() { override fun visitType(flags: Flags): KmTypeVisitor? { return ... } } }. }. } })

Slide 132

Slide 132 text

return object :.KmTypeVisitor().{. }.

Slide 133

Slide 133 text

return object :.KmTypeVisitor().{. override fun visitAbbreviatedType(flags: Flags): KmTypeVisitor? { } override fun visitArgument( flags: Flags, variance: KmVariance ): KmTypeVisitor? { } }.

Slide 134

Slide 134 text

class KmTypeInfoVisitor() :.KmTypeVisitor().{. }.

Slide 135

Slide 135 text

class KmTypeInfoVisitor( private val flags: Flags, private val onVisitEnd: (KmTypeInfoVisitor) -> Unit ) : KmTypeVisitor() {. }.

Slide 136

Slide 136 text

class KmTypeInfoVisitor( private val flags: Flags, private val onVisitEnd: (KmTypeInfoVisitor) -> Unit ) : KmTypeVisitor() {. lateinit var fqName: String var isNullable: Boolean = false private val typeArgs = arrayListOf() }.

Slide 137

Slide 137 text

class KmTypeInfoVisitor( private val flags: Flags, private val onVisitEnd: (KmTypeInfoVisitor) -> Unit ) : KmTypeVisitor() { lateinit var fqName: String var isNullable: Boolean = false private val typeArgs = arrayListOf() override fun visitEnd() { } }.

Slide 138

Slide 138 text

private val onVisitEnd: (KmTypeInfoVisitor) -> Unit ) : KmTypeVisitor() { lateinit var fqName: String var isNullable: Boolean = false private val typeArgs = arrayListOf() override fun visitEnd() { if (typeArgs.isNotEmpty()) { fqName += typeArgs.joinToString(prefix = "<", postfix = ">") typeArgs.clear() }, isNullable = flags.isNullableType onVisitEnd(this) } }.

Slide 139

Slide 139 text

fqName += typeArgs.joinToString(prefix = "<", postfix = ">") typeArgs.clear() }, isNullable = flags.isNullableType onVisitEnd(this) } override fun visitClass(name: ClassName) { fqName = name.fqName() }. }.

Slide 140

Slide 140 text

onVisitEnd(this) } override fun visitClass(name: ClassName) { fqName = name.fqName() }. override fun visitAbbreviatedType(flags: Flags): KmTypeVisitor? { return KmTypeInfoVisitor(flags, typeResolver) { fqName = it.fqName typeArgs.clear() }. }, }.

Slide 141

Slide 141 text

onVisitEnd(this) } override fun visitClass(name: ClassName) { fqName = name.fqName() }. override fun visitAbbreviatedType(flags: Flags): KmTypeVisitor? { return KmTypeInfoVisitor(flags, typeResolver) { fqName = it.fqName typeArgs.clear() }. }, }.

Slide 142

Slide 142 text

fqName = name.fqName() }. override fun visitAbbreviatedType(flags: Flags): KmTypeVisitor? { return KmTypeInfoVisitor(flags, typeResolver) { fqName = it.fqName typeArgs.clear() }. },’ override fun visitTypeAlias(name: ClassName) { fqName = name.fqName() }. }.

Slide 143

Slide 143 text

return KmTypeInfoVisitor(flags, typeResolver) { fqName = it.fqName typeArgs.clear() }. }, override fun visitTypeAlias(name: ClassName) { fqName = name.fqName() }. override fun visitTypeParameter(id: Int) { fqName = typeResolver(id) ?: throw error("Missing param: id=$id") }, }.

Slide 144

Slide 144 text

override fun visitTypeAlias(name: ClassName) { fqName = name.fqName() }. override fun visitTypeParameter(id: Int) { fqName = typeResolver(id) ?: throw error("Missing param: id=$id") }, override fun visitArgument( flags: Flags, variance: KmVariance ): KmTypeVisitor? { return KmTypeInfoVisitor(flags, typeResolver) { typeArgs += it.fqName }, }, }.

Slide 145

Slide 145 text

override fun visitTypeAlias(name: ClassName) { fqName = name.fqName() }. override fun visitTypeParameter(id: Int) { fqName = typeResolver(id) ?: throw error("Missing param: id=$id") }, override fun visitArgument( flags: Flags, variance: KmVariance ): KmTypeVisitor? { return KmTypeInfoVisitor(flags, typeResolver) { typeArgs += it.fqName },. }, }.

Slide 146

Slide 146 text

} override fun visitConstructor(flags: Flags): KmConstructorVisitor? { if (!flags.isPrimaryConstructor) return null return object : KmConstructorVisitor() { override fun visitValueParameter( flags: Flags, name: String ): KmValueParameterVisitor? { return object : KmValueParameterVisitor() { override fun visitType(flags: Flags): KmTypeVisitor? { return ... }. }. }. }. } })

Slide 147

Slide 147 text

} override fun visitConstructor(flags: Flags): KmConstructorVisitor? { if (!flags.isPrimaryConstructor) return null return object : KmConstructorVisitor() { override fun visitValueParameter( flags: Flags, name: String ): KmValueParameterVisitor? { return object : KmValueParameterVisitor() { override fun visitType(flags: Flags): KmTypeVisitor? { return KmTypeInfoVisitor(flags, typeArgsRegistry::get) { parameters += Parameter(name, it.fqName, it.isNullable) } }. }. }. }. } })

Slide 148

Slide 148 text

SHOW ME THE CODE

Slide 149

Slide 149 text

Worth noting

Slide 150

Slide 150 text

Worth noting • Metadata has only partial info

Slide 151

Slide 151 text

Worth noting • Metadata has only partial info • Separate code model from generation

Slide 152

Slide 152 text

Worth noting • Metadata has only partial info • Separate code model from generation • Both Kotlin & Java can be supported in one place

Slide 153

Slide 153 text

Worth noting • Metadata has only partial info • Separate code model from generation • Both Kotlin & Java can be supported in one place • Multiplatform support will follow

Slide 154

Slide 154 text

Other applications?

Slide 155

Slide 155 text

Other applications? • Annotation processing with codegen

Slide 156

Slide 156 text

Other applications? • Annotation processing with codegen • Bytecode weaving

Slide 157

Slide 157 text

Other applications? • Annotation processing with codegen • Bytecode weaving • Code validation

Slide 158

Slide 158 text

Other applications? • Annotation processing with codegen • Bytecode weaving • Code validation • Generating headers for interop with other languages: TypeScript, C/C++

Slide 159

Slide 159 text

Links • Metadata ProtoBuffer spec: github.com/JetBrains/kotlin/blob/master/core/metadata/src/metadata.proto • kotlin-metadata by Eugenio Marletti: github.com/Takhion/kotlin-metadata • kotlinx-metadata by JetBrains: github.com/JetBrains/kotlin/tree/master/libraries/kotlinx-metadata/jvm • Value-based API for kotlinx-metadata: youtrack.jetbrains.com/issue/KT-26602 • Gson TypeAdapters codegen: github.com/colriot/GsonKotgen • KotlinPoet: github.com/square/kotlinpoet • Annotation Processing in a Kotlin World by Zac Sweers: youtu.be/_yaaCtWF8aE?t=18968 • Annotation Processing Boilerplate Destruction by Jake Wharton: youtu.be/dOcs-NKK-RA • Multiple KAPT rounds demo: github.com/Takhion/generate-kotlin-multiple-rounds

Slide 160

Slide 160 text

Kotlin-friendly Annotation Processing Sergey Ryabov @colriot