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
kompile-testing internal
Search
@hotchemi
April 12, 2019
Programming
290
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
kompile-testing internal
At shibuya.apk
@hotchemi
April 12, 2019
More Decks by @hotchemi
See All by @hotchemi
The things we’ve learned from iOS×React Native hybrid development
hotchemi
2
5.5k
React Nativeを活用したアプリ開発体制/sapuri meetup
hotchemi
3
8.3k
Type-Safe i18n on RN
hotchemi
2
1.2k
Navigation in a hybrid app
hotchemi
3
1.4k
PermissionsDispatcher × Kotlin
hotchemi
0
3.4k
kotlin compiler plugin
hotchemi
1
820
Rx and Preferences
hotchemi
2
180
Introducing PermissionsDispatcher
hotchemi
1
190
khronos
hotchemi
4
2k
Other Decks in Programming
See All in Programming
どこまでゆるくて許されるのか
tk3fftk
0
500
ビデオ通話が繋がる0.2秒で何が起きているのか
supurazako
2
150
AIキャラアプリkaiwaの低遅延音声通話基盤をどう作ったか - AWS Gravitonで支える低遅延・低コストAI Agent基盤
mogamit
0
170
JAWS-UG横浜 #102 AWSサ終供養LT会 成仏できない AWS サービスたち 〜本日、三体供養します〜
maroon1st
0
210
壊れたパーサから始める関数型設計と構成的なパーサ #fp_matsuri
raiga0310
2
230
The Bowling Game- From Imperative to Functional Programming - Part 1
philipschwarz
PRO
0
330
音楽のための関数型プログラミング言語mimiumにおける多段階計算の活用
tomoyanonymous
1
330
エンジニア向け会社紹介/Findy Company Profile
findyinc
6
360k
トークンをケチるな、設計しろ:GitHub Copilotを賢く使うコンテキスト戦略
ochtum
0
320
Laravelで学ぶ Webアプリケーションチューニング入門/web_application_tuning_101
hanhan1978
4
680
Snowflake Summitでの新機能 CoCo / CoWork / snowflake-summit-2026-overall-what-new-coco
tatsuhiro
1
230
【やさしく解説 設計編・中級 #4】ルールの寿命と、システムの年輪
panda728
PRO
2
140
Featured
See All Featured
Abbi's Birthday
coloredviolet
3
8.7k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
Documentation Writing (for coders)
carmenintech
77
5.4k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
249
1.3M
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
55k
brightonSEO & MeasureFest 2025 - Christian Goodrich - Winning strategies for Black Friday CRO & PPC
cargoodrich
3
750
How to make the Groovebox
asonas
2
2.3k
How STYLIGHT went responsive
nonsquared
100
6.2k
Reflections from 52 weeks, 52 projects
jeffersonlam
356
21k
ラッコキーワード サービス紹介資料
rakko
1
3.9M
SEO for Brand Visibility & Recognition
aleyda
0
4.6k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
55
3.4k
Transcript
kompile-testing internal @hotchemi
• google/compile-testing • Testing tools for javac and apt •
Used in some code-gen libraries • Dagger2 • JavaPoet • Auto
Compilation compilation = javac() .withProcessors(new Processor()) .compile(JavaFileObjects.forResource("HelloWorld.java")); assertThat(compilation).succeeded(); assertThat(compilation) .generatedSourceFile("GeneratedHelloWorld")
.hasSourceEquivalentTo( JavaFileObjects.forResource("GeneratedHelloWorld.java"));
None
• kompile-testing • Simple API as compile-testing provides • Supports
kotlinc and kapt • Version: 0.1.2(alpha)
kotlinc() .withProcessors(Processor()) .addKotlin("input.kt", """ import kompile.testing.TestAnnotation @TestAnnotation class TestClass """.trimIndent())
.compile() .succeededWithoutWarnings() .generatedFile("generatedKtFile.kt") .hasSourceEquivalentTo(""" class GeneratedKtFile """.trimIndent())
Demo
Under the hood
Register processors - Write KClass info as a jar file
- For specifying kapt options Add Kotlin file - Just write contents to .kt file - Under source directory Run Kotlin compiler API - Define classpath and kapt options - Then run K2JVMCompiler#exec
K2JVMCompiler().exec( errStream = /* stream to output error */), args
= /* CLI options */)
• Command line compiler kotlinc hello.kt -d hello.jar kotlin -classpath
hello.jar HelloKt
• plugin/kapt options kotlinc hello.kt -d hello.jar -Xplugin=$KOTLIN_HOME/lib/kotlin-annotation-processing.jar -P plugin:org.jetbrains.kotlin.kapt3:sources=build/kapt/sources
-P plugin:org.jetbrains.kotlin.kapt3:classes=build/kapt/classes -P plugin:org.jetbrains.kotlin.kapt3:stubs=build/kapt/stubs -P plugin:org.jetbrains.kotlin.kapt3:aptMode=stubsAndApt -P plugin:org.jetbrains.kotlin.kapt3:correctErrorTypes=true -P plugin:org.jetbrains.kotlin.kapt3:apclasspath=lib/apt.jar
fun compile(): Compilation { val args = mutableListOf<String>() args.add("-d") args.add(classesDir.toString())
args.add("-no-stdlib") args.add("-classpath") args.add(fullClasspath().joinToString(separator = ":")) val sourceFiles = sourcesDir.listFiles() if (sourceFiles != null && sourceFiles.isNotEmpty()) { sourceFiles.forEach { args.add(it.toString()) } } val generatedDir = File(rootDir, "generated") args.addAll(annotationProcessorArgs(generatedDir.path)) val systemErrBuffer = Buffer() val stream = PrintStream(systemErrBuffer.outputStream()) val args = *args.toTypedArray() val exitCode = K2JVMCompiler().exec(stream, args) return Compilation(systemErrBuffer.readUtf8(), exitCode, generatedDir) }
private fun annotationProcessorArgs(generatedDirPath: String): List<String> { val sourceDir = File(rootDir,
"kapt/sources") val stubsDir = File(rootDir, "kapt/stubs") val kaptArgs = mutableMapOf<String, String>() kaptArgs["kapt.kotlin.generated"] = generatedDirPath val plugin = classpathFiles.find { it.name.startsWith(“kotlin-annotation-processing-embeddable”) } return listOf( "-Xplugin=$plugin", "-P", "plugin:org.jetbrains.kotlin.kapt3:sources=$sourceDir", "-P", "plugin:org.jetbrains.kotlin.kapt3:classes=$classesDir", "-P", "plugin:org.jetbrains.kotlin.kapt3:stubs=$stubsDir", "-P", "plugin:org.jetbrains.kotlin.kapt3:apclasspath=$servicesJar", "-P", "plugin:org.jetbrains.kotlin.kapt3:aptMode=stubsAndApt", "-P", "plugin:org.jetbrains.kotlin.kapt3:correctErrorTypes=true", "-P", "plugin:org.jetbrains.kotlin.kapt3:apoptions=${options(kaptArgs)}" ) }
• Conclusion • Extensible compiler API once you figure out
• Go into jetbrains/kotlin! • Support Kotlin in your code-gen project with kompile-testing • Still alpha though:D
• References • https://kotlinlang.org/docs/tutorials/command-line.html • https://kotlinlang.org/docs/reference/kapt.html • https://kotlinlang.org/docs/reference/compiler- plugins.html •
https://speakerdeck.com/kevinmost/writing-your-first- kotlin-compiler-plugin