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
82
KanmobaInKanto
Takuji Nishibayashi
November 21, 2017
Tweet
Share
More Decks by Takuji Nishibayashi
See All by Takuji Nishibayashi
java.timeをAndroidで使う
takuji31
0
24
KSPを使ってコード生成
takuji31
0
160
Kotlin Symbol Processing API (KSP) を使って Kotlin ア プリケーションの開発を効率化する
takuji31
1
710
kotlinx.serialization
takuji31
0
440
kanmoba-returns-02.pdf
takuji31
0
130
AndroidXとKotlin Coroutines
takuji31
0
270
AndroidXに潜む便利なヤツら
takuji31
0
120
Kotlin Inline Class
takuji31
0
96
No more Adapter with Epoxy and Data Binding
takuji31
0
780
Other Decks in Technology
See All in Technology
推しは推せるときに推せ! プロダクトにフィードバックしていこう
nakasho
0
450
コードファーストの考え方。 Amplify Gen2から学ぶAWS次世代のWeb開発体験
yoshiitaka
1
290
[新卒向け研修資料] テスト文字列に「うんこ」と入れるな(2024年版)
infiniteloop_inc
4
18k
Microsoft for Startups Founders Hub_20240429 update
daikikanemitsu
1
2.4k
web-application-security
matsuihidetoshi
1
190
Google Cloud Next '24でブログを10本書いた方法と勉強会を沸かせた方法
yasumuusan
0
330
Building a RAG-poweredAI chat appwith Python and VS Code
pamelafox
0
140
Azure犬駆動開発の記録/GlobalAzureFukuoka2024_20240420
nina01
1
240
Cypress or Playwright?
rainerhahnekamp
0
170
VSCodeの拡張機能を作っている話
ebarakazuhiro
1
790
GrafanaMeetup_AmazonManagedGrafanaのアクセス制御機能とマルチテナント環境下でのアクセス制御について
daitak
0
390
よく聞くけど使ったことないソフトウェアNo.1 KafkaとSnowflake
foursue
4
500
Featured
See All Featured
Teambox: Starting and Learning
jrom
128
8.4k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
245
20k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
155
14k
Git: the NoSQL Database
bkeepers
PRO
423
63k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
14
1.5k
Bootstrapping a Software Product
garrettdimon
PRO
302
110k
10 Git Anti Patterns You Should be Aware of
lemiorhan
649
58k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
22
1.6k
Robots, Beer and Maslow
schacon
PRO
155
7.9k
Side Projects
sachag
451
41k
Intergalactic Javascript Robots from Outer Space
tanoku
266
26k
4 Signs Your Business is Dying
shpigford
176
21k
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 !