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
Observability in Practice:Grafana 與 Edge Device SRE 的那些事
blueswen
0
170
Snowflake Summitでの新機能 CoCo / CoWork / snowflake-summit-2026-overall-what-new-coco
tatsuhiro
1
180
Lemonade + Foundry Toolkit でお手軽アプリ開発
seosoft
1
370
Creating Composable Callables in Contemporary C++
rollbear
0
160
「AIで開発し、AIを届ける」をEvalでつなぐ 〜AIネイティブに始めるプロダクト開発の実践〜 / Connecting "Develop with AI, deliver AI" with Eval
rkaga
4
5.4k
Datadog × OpenTelemetry 入門と実践のあいだ
kn_to_maxpno
1
180
Spec Driven Development | AI Summit Lisbon
danielsogl
PRO
0
210
エンジニアと一緒にテストコードの設計と実装を改善した話
mototakatsu
0
220
エンジニア向け会社紹介/Findy Company Profile
findyinc
6
350k
Developing with AI Agents — Codex, Claude Code & Cowork Practical Guide
x5gtrn
PRO
0
1.3k
Even G2とAWSで推しのエージェントを召喚しよう!
har1101
1
120
Lessons from Spec-Driven Development
simas
PRO
0
220
Featured
See All Featured
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
Code Review Best Practice
trishagee
74
20k
Color Theory Basics | Prateek | Gurzu
gurzu
0
370
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
0
330
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
200
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
210
4 Signs Your Business is Dying
shpigford
187
22k
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
590
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
210
Building Flexible Design Systems
yeseniaperezcruz
330
40k
Code Reviewing Like a Champion
maltzj
528
40k
Scaling GitHub
holman
464
140k
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