KT-12303 Pass only relevant annotations to annotation processors

(cherry picked from commit 01742aa)
This commit is contained in:
Yan Zhulanow
2016-07-13 16:48:09 +03:00
committed by Yan Zhulanow
parent 7810678389
commit 1a1eb7dcfa
17 changed files with 327 additions and 4 deletions
@@ -26,11 +26,11 @@ import javax.lang.model.element.TypeElement
import javax.tools.Diagnostic
import kotlin.properties.Delegates
public class AnnotationProcessorStub : AbstractProcessor() {
class AnnotationProcessorStub : AbstractProcessor() {
override fun process(annotations: Set<TypeElement>?, roundEnv: RoundEnvironment?) = true
}
public abstract class AnnotationProcessorWrapper(
abstract class AnnotationProcessorWrapper(
private val processorFqName: String,
private val taskQualifier: String
) : Processor {
@@ -38,6 +38,13 @@ public abstract class AnnotationProcessorWrapper(
private companion object {
val KAPT_ANNOTATION_OPTION = "kapt.annotations"
val KAPT_KOTLIN_GENERATED_OPTION = "kapt.kotlin.generated"
/*
* If no Java sources are provided, javac finishes without running Annotation Processing.
* This is not what we want, so we generate a special class for this case
* with a field annotated with KAPT_SPECIAL_ANNOTATION (see AnnotationProcessingManager.generateJavaHackFile).
*/
val KAPT_SPECIAL_ANNOTATION = "__gen.KotlinAptAnnotation"
}
private val processor: Processor by lazy {
@@ -56,6 +63,8 @@ public abstract class AnnotationProcessorWrapper(
private var roundCounter = 0
private var handledAnnotationTypes: Set<String> = emptySet()
override fun getCompletions(
element: Element?,
annotation: AnnotationMirror?,
@@ -83,11 +92,13 @@ public abstract class AnnotationProcessorWrapper(
}
processor.init(processingEnv)
handledAnnotationTypes = this.supportedAnnotationTypes
}
override fun getSupportedAnnotationTypes(): MutableSet<String> {
val supportedAnnotations = processor.supportedAnnotationTypes.toMutableSet()
supportedAnnotations.add("__gen.KotlinAptAnnotation")
supportedAnnotations.add(KAPT_SPECIAL_ANNOTATION)
return supportedAnnotations
}
@@ -106,7 +117,10 @@ public abstract class AnnotationProcessorWrapper(
if (roundCounter == 1) {
for (annotationFqName in kotlinAnnotationsProvider.annotatedKotlinElements.keys) {
if (annotationFqName in existingFqNames) continue
if (annotationFqName in existingFqNames
|| annotationFqName == KAPT_SPECIAL_ANNOTATION
|| annotationFqName !in handledAnnotationTypes // Filter out irrelevant annotations) continue
) continue
existingFqNames.add(annotationFqName)
processingEnv.elementUtils.getTypeElement(annotationFqName)?.let { wrappedAnnotations += it }
}