Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
KanmobaInKanto
Search
Takuji Nishibayashi
November 21, 2017
Technology
1
90
KanmobaInKanto
Takuji Nishibayashi
November 21, 2017
Tweet
Share
More Decks by Takuji Nishibayashi
See All by Takuji Nishibayashi
compose-hot-reload を試そうとした話
takuji31
0
59
CameraX使ってみた
takuji31
0
200
kotlinx.datetime 使ってみた
takuji31
0
630
HiltのCustom Componentについて
takuji31
0
270
java.timeをAndroidで使う
takuji31
0
120
KSPを使ってコード生成
takuji31
0
370
Kotlin Symbol Processing API (KSP) を使って Kotlin ア プリケーションの開発を効率化する
takuji31
1
2.7k
kotlinx.serialization
takuji31
0
600
kanmoba-returns-02.pdf
takuji31
0
220
Other Decks in Technology
See All in Technology
kintone開発組織のDevOpsへの移り変わりと実践
ueokande
1
130
ゴリラ.vim #36 ~ Vim x SNS ~ スポンサーセッション
yasunori0418
1
360
データプレーンプログラミングとは? DPU&スイッチASICの開発経験から語る
ebiken
PRO
1
270
Zero Data Loss Autonomous Recovery Service サービス概要
oracle4engineer
PRO
2
7.2k
オープンソースのハードウェアのコンテストに参加している話
iotengineer22
0
660
CloudBruteによる外部からのS3バケットの探索・公開の発見について / 20250605 Kumiko Hennmi
shift_evolve
3
200
Babylon.jsでゲームを作ってみよう
limes2018
0
100
Houtou.pm #1
papix
0
670
AIコードエディタは開発を変えるか?Cursorをチームに導入して1ヶ月経った本音
ota1022
1
710
Digitization部 紹介資料
sansan33
PRO
1
3.8k
ソフトウェアは捨てやすく作ろう/Let's make software easy to discard
sanogemaru
10
5.8k
Scale Security Programs with Scorecarding
ramimac
0
440
Featured
See All Featured
Building Adaptive Systems
keathley
41
2.6k
Producing Creativity
orderedlist
PRO
346
40k
KATA
mclloyd
29
14k
Making the Leap to Tech Lead
cromwellryan
134
9.3k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
6
660
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
29
9.5k
The Straight Up "How To Draw Better" Workshop
denniskardys
233
140k
Being A Developer After 40
akosma
91
590k
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
32
5.8k
The Language of Interfaces
destraynor
158
25k
Building an army of robots
kneath
306
45k
Transcript
Pluggable Annotation ProcessingͰ KotlinͷίʔυΛੜ͢Δ ؔϞό in ؔ౦ @takuji31
@takuji31 (Takuji Nishibayashi) Application Engineer at Hatena
Loves • ! (AVG 175) • " # • http://photo.takuji31.jp
• $ • http://nazuna.takuji31.jp • Android • Kotlin
AGENDA • Pluggable Annotation Processing • KotlinPoet
Pluggable Annotation Processing
JavaͰιʔείʔυʹ༩͞Εͨ ऍΛղऍͯ͠ɺιʔείʔυΛੜ ͢ΔΈ
Α͘APTͬͯݺͿ(ݺͼؒҧ͑ΒΕ Δͭ)Ͱ͢Ͷ
APTDeprecatedʹͳ͍ͬͯΔ
Pluggable Annotation Processiong ͱͪΌΜͱݺͼ·͠ΐ͏
Data Binding
Dagger2
ButterKnife
etc.
͍ํ
͍ํ ࡞Γํ
AbstractAnnotationProcessorΛ ܧঝ
processΛ࣮
ྫ // ιʔείʔυͷόʔδϣϯ @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 } }
࣮Kotlinͷ ίʔυੜͰ͖Δ
Kotlin ͷίʔυੜͰ͖Δʂ
େࣄͳ͜ͱͰ͢Ͷ
ੜ͢ΔίʔυKotlinʹ͍ͨ͠
͔͠͠खͰॻ͘ͷ໘
KotlinPoet
JavaPoetͷKotlin൛
https://github.com/square/ kotlinpoet/
ίʔυੜͯ͠ΈΔ
ΫϥεΛ࡞Δ val packageName = "jp.takuji31.kanmoba" val userClassName = ClassName(packageName =
packageName, simpleName = "User") val userClass = TypeSpec .classBuilder(userClassName) .addModifiers(KModifier.DATA) .build()
ίϯετϥΫλʔΛఆٛ val userClass = TypeSpec .classBuilder(userClassName) .primaryConstructor( FunSpec .constructorBuilder() .addParameter(ParameterSpec
.builder("id", Int::class) .build() ) .addParameter(ParameterSpec .builder("name", String::class) .build() ) .build() ) .build()
ϓϩύςΟʔΛఆٛ val userClass = TypeSpec .classBuilder(userClassName) .addProperty( PropertySpec .builder("id", Int::class)
.initializer("id") .build() ) .addProperty( PropertySpec .builder("name", String::class) .initializer("name") .build() ) .build()
ग़ྗ val kotlinFile = KotlinFile.builder(packageName, "User") .addType(userClass) .build() print(kotlinFile.toString())
݁Ռ package jp.takuji31.kanmoba import kotlin.Int import kotlin.String data class User(val
id: Int, val name: String)
!
͜ΕΛAnnotation Processor্Ͱ࣮ ߦͯ͠ΕΑ͍
Ͳ͏ͬͯॻ͖ग़͢ͷ͔ʁ
ॻ͖ग़͢ 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))
kaptͷ࣮ߦޙʹίϯύΠϧ͞ΕΔ
※ ੜ͞ΕͨΫϥεΛ ࠶ॲཧͰ͖ͳ͍
Javaͷ࣌ͱҧ͏ͷͰཁҙ
Annotation Processor࡞ͬͨ
R.kt
Android͕ੜ͢ΔR.javaͷ͍͢͝ ͭ
Πϝʔδ 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 } }
takuji31/R.kt
ࠓ·ͩಈ͔ͳ͍ !
ੜ͞ΕͨίʔυΛIDE͕ೝࣝͰ͖ ͳ͍
https://youtrack.jetbrains.com/ issue/KT-20269
1.2ͰΔͬΆ͍…ʁ
Enjoy Annotation Processing !