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.2k
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
180
khronos
hotchemi
4
2k
Other Decks in Programming
See All in Programming
Creating Composable Callables in Contemporary C++
rollbear
0
130
技術記事、AIに書かせるか、自分で書くか? 〜それでも私が自分の手で書く理由〜 / #QiitaConference
jnchito
2
1.4k
A2UI という光を覗いてみる
satohjohn
1
130
PHPで使える日時の表現と、その知り方 #frontend_phpcon_do
o0h
PRO
0
240
作って学ぶ、 JSX (TSX) ランタイムの基本
syumai
7
1.6k
Developing with AI Agents — Codex, Claude Code & Cowork Practical Guide
x5gtrn
PRO
0
1.3k
コンテキストの使い捨てをやめる — ビジネスルール駆動開発と miko —
ioki
0
200
CSC307 Lecture 17
javiergs
PRO
0
320
「エンジニアインターン、どうやって取った?」準備のリアルを語るLT会 Progate BAR
akiomatic
0
130
Signal Forms: Beyond the Basics @ngBaguette 2026 in Paris
manfredsteyer
PRO
0
250
JavaDoc 再入門
nagise
1
330
JJUG CCC 2026 Spring: JSpecify で実現する Kotlin フレンドリーな Java API 設計
ternbusty
1
170
Featured
See All Featured
Context Engineering - Making Every Token Count
addyosmani
9
960
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
430
Amusing Abliteration
ianozsvald
1
200
HTML-Aware ERB: The Path to Reactive Rendering @ RubyCon 2026, Rimini, Italy
marcoroth
1
190
Statistics for Hackers
jakevdp
799
230k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.8k
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
2.8k
Rebuilding a faster, lazier Slack
samanthasiow
85
9.5k
YesSQL, Process and Tooling at Scale
rocio
174
15k
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
200
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.7k
sira's awesome portfolio website redesign presentation
elsirapls
0
280
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