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
73
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 SageMaker Unified Studio(Preview)、Lakehouse と Amazon S3 Tables
ishikawa_satoru
0
150
ゼロから創る横断SREチーム 挑戦と進化の軌跡
rvirus0817
2
260
祝!Iceberg祭開幕!re:Invent 2024データレイク関連アップデート10分総ざらい
kniino
2
250
[Ruby] Develop a Morse Code Learning Gem & Beep from Strings
oguressive
1
150
PHPからGoへのマイグレーション for DMMアフィリエイト
yabakokobayashi
1
160
開発生産性向上! 育成を「改善」と捉えるエンジニア育成戦略
shoota
1
230
AI時代のデータセンターネットワーク
lycorptech_jp
PRO
1
280
権威ドキュメントで振り返る2024 #年忘れセキュリティ2024
hirotomotaguchi
2
730
ハイテク休憩
sat
PRO
2
140
DevOps視点でAWS re:invent2024の新サービス・アプデを振り返ってみた
oshanqq
0
180
統計データで2024年の クラウド・インフラ動向を眺める
ysknsid25
2
840
オプトインカメラ:UWB測位を応用したオプトイン型のカメラ計測
matthewlujp
0
170
Featured
See All Featured
Code Reviewing Like a Champion
maltzj
520
39k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
GraphQLの誤解/rethinking-graphql
sonatard
67
10k
Adopting Sorbet at Scale
ufuk
73
9.1k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
6
510
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
127
18k
Music & Morning Musume
bryan
46
6.2k
Building Your Own Lightsaber
phodgson
103
6.1k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
132
33k
Speed Design
sergeychernyshev
25
670
Automating Front-end Workflow
addyosmani
1366
200k
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 !