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
88
KanmobaInKanto
Takuji Nishibayashi
November 21, 2017
Tweet
Share
More Decks by Takuji Nishibayashi
See All by Takuji Nishibayashi
CameraX使ってみた
takuji31
0
86
kotlinx.datetime 使ってみた
takuji31
0
330
HiltのCustom Componentについて
takuji31
0
190
java.timeをAndroidで使う
takuji31
0
74
KSPを使ってコード生成
takuji31
0
310
Kotlin Symbol Processing API (KSP) を使って Kotlin ア プリケーションの開発を効率化する
takuji31
1
990
kotlinx.serialization
takuji31
0
550
kanmoba-returns-02.pdf
takuji31
0
180
AndroidXとKotlin Coroutines
takuji31
0
340
Other Decks in Technology
See All in Technology
Amazon VPC Lattice 最新アップデート紹介 - PrivateLink も似たようなアップデートあったけど違いとは
bigmuramura
0
200
継続的にアウトカムを生み出し ビジネスにつなげる、 戦略と運営に対するタイミーのQUEST(探求)
zigorou
0
590
生成AIをより賢く エンジニアのための RAG入門 - Oracle AI Jam Session #20
kutsushitaneko
4
260
NilAway による静的解析で「10 億ドル」を節約する #kyotogo / Kyoto Go 56th
ytaka23
3
380
バクラクのドキュメント解析技術と実データにおける課題 / layerx-ccc-winter-2024
shimacos
2
1.1k
Snykで始めるセキュリティ担当者とSREと開発者が楽になる脆弱性対応 / Getting started with Snyk Vulnerability Response
yamaguchitk333
2
190
新機能VPCリソースエンドポイント機能検証から得られた考察
duelist2020jp
0
230
ゼロから創る横断SREチーム 挑戦と進化の軌跡
rvirus0817
2
270
ずっと昔に Star をつけたはずの思い出せない GitHub リポジトリを見つけたい!
rokuosan
0
150
OpenAIの蒸留機能(Model Distillation)を使用して運用中のLLMのコストを削減する取り組み
pharma_x_tech
4
560
どちらを使う?GitHub or Azure DevOps Ver. 24H2
kkamegawa
0
840
KubeCon NA 2024 Recap / Running WebAssembly (Wasm) Workloads Side-by-Side with Container Workloads
z63d
1
250
Featured
See All Featured
Mobile First: as difficult as doing things right
swwweet
222
9k
How to train your dragon (web standard)
notwaldorf
88
5.7k
Building an army of robots
kneath
302
44k
Fashionably flexible responsive web design (full day workshop)
malarkey
405
66k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
Product Roadmaps are Hard
iamctodd
PRO
49
11k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Optimising Largest Contentful Paint
csswizardry
33
3k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
29
2k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.5k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.1k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
59k
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 !