Kapt: Attach generated Kotlin sources in 'compilation' mode (KT-32535)

This commit is contained in:
Yan Zhulanow
2019-07-23 20:42:52 +09:00
parent 1c63d3aa2f
commit ccff347ffc
11 changed files with 168 additions and 7 deletions
@@ -0,0 +1,37 @@
package apt
import java.io.File
import javax.annotation.processing.*
import javax.lang.model.SourceVersion
import javax.lang.model.element.TypeElement
import javax.tools.Diagnostic.Kind.*
annotation class Anno
class SampleApt : AbstractProcessor() {
private companion object {
const val KAPT_KOTLIN_GENERATED_OPTION_NAME = "kapt.kotlin.generated"
}
override fun process(annotations: Set<TypeElement>, roundEnv: RoundEnvironment): Boolean {
val kaptKotlinGeneratedDir = processingEnv.options[KAPT_KOTLIN_GENERATED_OPTION_NAME] ?: run {
processingEnv.messager.printMessage(ERROR, "Can't find the target directory for generated Kotlin files.")
return false
}
val baseDir = File(kaptKotlinGeneratedDir, "generated")
baseDir.mkdirs()
for (element in roundEnv.getElementsAnnotatedWith(Anno::class.java)) {
val generatedSimpleName = element.simpleName.toString().capitalize()
val file = File(baseDir, "$generatedSimpleName.kt")
file.writeText("package generated\nclass $generatedSimpleName")
}
return true
}
override fun getSupportedOptions() = emptySet<String>()
override fun getSupportedSourceVersion() = SourceVersion.RELEASE_8
override fun getSupportedAnnotationTypes() = setOf("apt.Anno")
}