Save 37% off PRO during our Black Friday Sale! »

KanmobaInKanto

 KanmobaInKanto

E28404a128b6014e6f499d2a967c72a0?s=128

Takuji Nishibayashi

November 21, 2017
Tweet

Transcript

  1. Pluggable Annotation ProcessingͰ KotlinͷίʔυΛੜ੒͢Δ ؔϞό in ؔ౦ @takuji31

  2. @takuji31 (Takuji Nishibayashi) Application Engineer at Hatena

  3. Loves • ! (AVG 175) • " # • http://photo.takuji31.jp

    • $ • http://nazuna.takuji31.jp • Android • Kotlin
  4. AGENDA • Pluggable Annotation Processing • KotlinPoet

  5. Pluggable Annotation Processing

  6. JavaͰιʔείʔυʹ෇༩͞Εͨ஫ ऍΛղऍͯ͠ɺιʔείʔυΛੜ੒ ͢Δ࢓૊Έ

  7. Α͘APTͬͯݺͿ(ݺͼؒҧ͑ΒΕ Δ΍ͭ)Ͱ͢Ͷ

  8. APT͸Deprecatedʹͳ͍ͬͯΔ

  9. Pluggable Annotation Processiong ͱͪΌΜͱݺͼ·͠ΐ͏

  10. Data Binding

  11. Dagger2

  12. ButterKnife

  13. etc.

  14. ࢖͍ํ

  15. ࢖͍ํ ࡞Γํ

  16. AbstractAnnotationProcessorΛ ܧঝ

  17. processΛ࣮૷

  18. ྫ // ιʔείʔυͷόʔδϣϯ @SupportedSourceVersion(SourceVersion.RELEASE_8) // ॲཧ͢ΔΞϊςʔγϣϯͷҰཡ @SupportedAnnotationTypes("jp.takuji31.annotations.SomeAnnotation") // αϙʔτ͢ΔΦϓγϣϯ @SupportedOptions("kapt.kotlin.generated")

    class ExampleProcessor : AbstractProcessor() { override fun process(annotations: MutableSet<out TypeElement>, roundEnv: RoundEnvironment): Boolean { // do something return true } }
  19. ࣮͸Kotlinͷ ίʔυ΋ੜ੒Ͱ͖Δ

  20. Kotlin ͷίʔυ΋ੜ੒Ͱ͖Δʂ

  21. େࣄͳ͜ͱͰ͢Ͷ

  22. ੜ੒͢Δίʔυ΋Kotlinʹ͍ͨ͠

  23. ͔͠͠खͰॻ͘ͷ͸໘౗

  24. KotlinPoet

  25. JavaPoetͷKotlin൛

  26. https://github.com/square/ kotlinpoet/

  27. ίʔυੜ੒ͯ͠ΈΔ

  28. ΫϥεΛ࡞Δ val packageName = "jp.takuji31.kanmoba" val userClassName = ClassName(packageName =

    packageName, simpleName = "User") val userClass = TypeSpec .classBuilder(userClassName) .addModifiers(KModifier.DATA) .build()
  29. ίϯετϥΫλʔΛఆٛ val userClass = TypeSpec .classBuilder(userClassName) .primaryConstructor( FunSpec .constructorBuilder() .addParameter(ParameterSpec

    .builder("id", Int::class) .build() ) .addParameter(ParameterSpec .builder("name", String::class) .build() ) .build() ) .build()
  30. ϓϩύςΟʔΛఆٛ val userClass = TypeSpec .classBuilder(userClassName) .addProperty( PropertySpec .builder("id", Int::class)

    .initializer("id") .build() ) .addProperty( PropertySpec .builder("name", String::class) .initializer("name") .build() ) .build()
  31. ग़ྗ val kotlinFile = KotlinFile.builder(packageName, "User") .addType(userClass) .build() print(kotlinFile.toString())

  32. ݁Ռ package jp.takuji31.kanmoba import kotlin.Int import kotlin.String data class User(val

    id: Int, val name: String)
  33. !

  34. ͜ΕΛAnnotation Processor্Ͱ࣮ ߦͯ͠΍Ε͹Α͍

  35. Ͳ͏΍ͬͯॻ͖ग़͢ͷ͔ʁ

  36. ॻ͖ग़͢ val generatedDir = processingEnv.options["kapt.kotlin.generated"] ?: throw IllegalStateException("Needs kapt support.")

    KotlinFile.builder(packageName = packageName, fileName = "User") .addType(userClass.build()) .build() .writeTo(File(generatedDir))
  37. kaptͷ࣮ߦޙʹίϯύΠϧ͞ΕΔ

  38. ※ ੜ੒͞ΕͨΫϥεΛ ࠶౓ॲཧͰ͖ͳ͍

  39. Javaͷ࣌ͱҧ͏ͷͰཁ஫ҙ

  40. Annotation Processor࡞ͬͨ

  41. R.kt

  42. Android͕ੜ੒͢ΔR.javaͷ͍͢͝ ΍ͭ

  43. Πϝʔδ class MainActivity() : Activity() { val R by lazy

    { RKt(context = this) } fun onCreate(savedInstanceState: Bundle?) { // ic_launcher.png val icon: Drawable = R.drawable.ic_launcher.asDrawable() // Hello %1$s val message: String = R.string.welcome_message.format("Kanmoba") // Hello Kanmoba } }
  44. takuji31/R.kt

  45. ࠓ͸·ͩಈ͔ͳ͍ !

  46. ੜ੒͞ΕͨίʔυΛIDE͕ೝࣝͰ͖ ͳ͍

  47. https://youtrack.jetbrains.com/ issue/KT-20269

  48. 1.2Ͱ௚ΔͬΆ͍…ʁ

  49. Enjoy Annotation Processing !