Last active
September 24, 2017 13:24
-
-
Save miquelbeltran/dafac1fafda8d20399173d0661e77372 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package work.beltran.sample | |
| import com.google.auto.service.AutoService | |
| import com.squareup.kotlinpoet.FileSpec | |
| import com.squareup.kotlinpoet.FunSpec | |
| import com.squareup.kotlinpoet.TypeSpec | |
| import java.io.File | |
| import javax.annotation.processing.* | |
| import javax.lang.model.SourceVersion | |
| import javax.lang.model.element.TypeElement | |
| @AutoService(Processor::class) | |
| class Generator: AbstractProcessor() { | |
| override fun getSupportedAnnotationTypes(): MutableSet<String> { | |
| println("getSupportedAnnotationTypes") | |
| return mutableSetOf(GenName::class.java.name) | |
| } | |
| override fun getSupportedSourceVersion(): SourceVersion { | |
| return SourceVersion.latest() | |
| } | |
| override fun process(set: MutableSet<out TypeElement>, roundEnv: RoundEnvironment): Boolean { | |
| println("process") | |
| roundEnv.getElementsAnnotatedWith(GenName::class.java) | |
| .forEach { | |
| val className = it.simpleName.toString() | |
| println("Processing: $className") | |
| val pack = processingEnv.elementUtils.getPackageOf(it).toString() | |
| generateClass(className, pack) | |
| } | |
| return true | |
| } | |
| private fun generateClass(className: String, pack: String) { | |
| val fileName = "Generated_$className" | |
| val file = FileSpec.builder(pack, fileName) | |
| .addType(TypeSpec.classBuilder(fileName) | |
| .addFunction(FunSpec.builder("getName") | |
| .addStatement("return \"World\"") | |
| .build()) | |
| .build()) | |
| .build() | |
| val kaptKotlinGeneratedDir = processingEnv.options[KAPT_KOTLIN_GENERATED_OPTION_NAME] | |
| file.writeTo(File(kaptKotlinGeneratedDir, "$fileName.kt")) | |
| } | |
| companion object { | |
| const val KAPT_KOTLIN_GENERATED_OPTION_NAME = "kapt.kotlin.generated" | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment