Fix collecting source retention annotations

#KT-12187 fixed
This commit is contained in:
Alexey Tsvetkov
2016-05-05 20:05:07 +03:00
parent cac49b6609
commit 10b9be9f51
30 changed files with 268 additions and 30 deletions
@@ -1,8 +0,0 @@
package example
import java.lang.annotation.ElementType
import java.lang.annotation.Inherited
import java.lang.annotation.Target
@Inherited
annotation public class ExampleAnnotation
@@ -6,19 +6,31 @@ import javax.annotation.processing.RoundEnvironment
import javax.lang.model.SourceVersion
import javax.lang.model.element.ElementKind
import javax.lang.model.element.TypeElement
import javax.tools.Diagnostic
import kotlin.reflect.KClass
public class ExampleAnnotationProcessor : AbstractProcessor() {
class ExampleAnnotationProcessor : AbstractProcessor() {
private companion object {
val ANNOTATION_FQ_NAME = ExampleAnnotation::class.java.canonicalName
val ANNOTATION_TO_PREFIX = mapOf(ExampleAnnotation::class to "",
ExampleSourceAnnotation::class to "SourceAnnotated",
ExampleRuntimeAnnotation::class to "RuntimeAnnotated",
ExampleBinaryAnnotation::class to "BinaryAnnotated")
val SUFFIX_OPTION = "suffix"
val GENERATE_KOTLIN_CODE_OPTION = "generate.kotlin.code"
val KAPT_KOTLIN_GENERATED_OPTION = "kapt.kotlin.generated"
}
override fun process(annotations: MutableSet<out TypeElement>?, roundEnv: RoundEnvironment): Boolean {
val elements = roundEnv.getElementsAnnotatedWith(ExampleAnnotation::class.java)
for ((annotation, prefix) in ANNOTATION_TO_PREFIX) {
processAnnotation(roundEnv, annotation, prefix)
}
return true
}
private fun <T : Annotation> processAnnotation(roundEnv: RoundEnvironment, annotationClass: KClass<T>, generatedFilePrefix: String) {
val elements = roundEnv.getElementsAnnotatedWith(annotationClass.java)
val elementUtils = processingEnv.elementUtils
val filer = processingEnv.filer
@@ -30,28 +42,26 @@ public class ExampleAnnotationProcessor : AbstractProcessor() {
for (element in elements) {
val packageName = elementUtils.getPackageOf(element).qualifiedName.toString()
val simpleName = element.simpleName
val className = simpleName.toString().capitalize() + generatedFileSuffix
val simpleName = element.simpleName.toString()
val generatedJavaClassName = generatedFilePrefix.capitalize() + simpleName.capitalize() + generatedFileSuffix
filer.createSourceFile(className).openWriter().use { with(it) {
filer.createSourceFile(generatedJavaClassName).openWriter().use { with(it) {
appendln("package $packageName;")
appendln("public final class $className {}")
appendln("public final class $generatedJavaClassName {}")
}}
if (generateKotlinCode && kotlinGenerated != null && element.kind == ElementKind.CLASS) {
File(kotlinGenerated, "$simpleName.kt").writer().buffered().use {
it.appendln("package $packageName")
it.appendln("fun $simpleName.customToString() = \"$simpleName: \" + toString()")
it.appendln("fun $simpleName.customToString() = \"$generatedJavaClassName: \" + toString()")
}
}
}
return true;
}
override fun getSupportedSourceVersion() = SourceVersion.RELEASE_6
override fun getSupportedAnnotationTypes() = setOf(ANNOTATION_FQ_NAME)
override fun getSupportedAnnotationTypes() = ANNOTATION_TO_PREFIX.keys.map { it.java.canonicalName }.toSet()
override fun getSupportedOptions() = setOf(SUFFIX_OPTION)
}
@@ -0,0 +1,18 @@
package example
import java.lang.annotation.Inherited
@Inherited
annotation class ExampleAnnotation
@Inherited
@Retention(AnnotationRetention.SOURCE)
annotation class ExampleSourceAnnotation
@Inherited
@Retention(AnnotationRetention.BINARY)
annotation class ExampleBinaryAnnotation
@Inherited
@Retention(AnnotationRetention.RUNTIME)
annotation class ExampleRuntimeAnnotation