= 0 } —————— public final class Bar { @FirstAnn private int foo; @SecondAnn public final int getFoo() { return this.foo; } @ThirdAnn public final void setFoo(@FourthAnn int var1) { this.foo = var1; } }
} —————— public final class Bar { @FirstAnn @SecondAnn private int foo; public final int getFoo() { return this.foo; } public final void setFoo(int var1) { this.foo = var1; } }
in binary output, not visible for reflection at runtime AnnotationRetention.RUNTIME (default) -> stored in binary output and visible for reflection at runtime
const val PIE = 28 // Same using @StringDef @IntDef(KITKAT, LOLLIPOP, PIE) @Retention(AnnotationRetention.SOURCE) annotation class AndroidVersion @AndroidVersion fun obtainAndroidVersion(): Int = when (Build.VERSION.SDK_INT) { Build.VERSION_CODES.KITKAT -> KITKAT Build.VERSION_CODES.LOLLIPOP -> LOLLIPOP Build.VERSION_CODES.P -> PIE else -> throw IllegalArgumentException("The version is unsupported") }
(Kotlin Annotation Processing Tool) • Can not manipulate the existent code • Takes the source files as input and generates new source files • The processing runs in multiple rounds
age: Int = 0 fun setName(name: String): PersonBuilder = apply { this.name = name } fun setAge(age: Int): PersonBuilder = apply { this.age = age } fun build(): Person = Person(name = name, age = age) }