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
66
kotlinx.datetime 使ってみた
takuji31
0
260
HiltのCustom Componentについて
takuji31
0
150
java.timeをAndroidで使う
takuji31
0
60
KSPを使ってコード生成
takuji31
0
290
Kotlin Symbol Processing API (KSP) を使って Kotlin ア プリケーションの開発を効率化する
takuji31
1
960
kotlinx.serialization
takuji31
0
540
kanmoba-returns-02.pdf
takuji31
0
180
AndroidXとKotlin Coroutines
takuji31
0
330
Other Decks in Technology
See All in Technology
VideoMamba: State Space Model for Efficient Video Understanding
chou500
0
190
SREが投資するAIOps ~ペアーズにおけるLLM for Developerへの取り組み~
takumiogawa
1
430
DynamoDB でスロットリングが発生したとき/when_throttling_occurs_in_dynamodb_short
emiki
0
250
Security-JAWS【第35回】勉強会クラウドにおけるマルウェアやコンテンツ改ざんへの対策
4su_para
0
180
開発生産性を上げながらビジネスも30倍成長させてきたチームの姿
kamina_zzz
2
1.7k
Zennのパフォーマンスモニタリングでやっていること
ryosukeigarashi
0
150
いざ、BSC討伐の旅
nikinusu
2
780
Application Development WG Intro at AppDeveloperCon
salaboy
0
190
Python(PYNQ)がテーマのAMD主催のFPGAコンテストに参加してきた
iotengineer22
0
500
[CV勉強会@関東 ECCV2024 読み会] オンラインマッピング x トラッキング MapTracker: Tracking with Strided Memory Fusion for Consistent Vector HD Mapping (Chen+, ECCV24)
abemii
0
220
Can We Measure Developer Productivity?
ewolff
1
150
マルチプロダクトな開発組織で 「開発生産性」に向き合うために試みたこと / Improving Multi-Product Dev Productivity
sugamasao
1
310
Featured
See All Featured
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
126
18k
Art, The Web, and Tiny UX
lynnandtonic
297
20k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
28
8.2k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
364
24k
GitHub's CSS Performance
jonrohan
1030
460k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
It's Worth the Effort
3n
183
27k
[RailsConf 2023] Rails as a piece of cake
palkan
52
4.9k
Making Projects Easy
brettharned
115
5.9k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
42
9.2k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
169
50k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5k
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 !