Kapt: load all annotations, even if annotation processors does not require it explicitly. Some annotation processors may want to process some more annotations (see DbFlow, Database annotation).
Blacklist some common-used Java and Kotlin annotations instead (like Deprecated, Nullable or Metadata). (cherry picked from commit 6856a7c)
This commit is contained in:
committed by
Yan Zhulanow
parent
975364b2ed
commit
c6c1673902
+1
-3
@@ -164,14 +164,12 @@ abstract class AbstractAnnotationProcessingExtension(
|
||||
}
|
||||
|
||||
private fun KotlinProcessingEnvironment.doAnnotationProcessing(files: Collection<KtFile>): ProcessingResult {
|
||||
val allSupportedAnnotationFqNames = run initializeProcessors@ {
|
||||
run initializeProcessors@ {
|
||||
processors.forEach { it.init(this) }
|
||||
log { "Initialized processors: " + processors.joinToString { it.javaClass.name } }
|
||||
processors.flatMapTo(mutableSetOf()) { it.supportedAnnotationTypes }
|
||||
}
|
||||
|
||||
val firstRoundAnnotations = RoundAnnotations(
|
||||
allSupportedAnnotationFqNames,
|
||||
incrementalCompilationComponents?.getSourceRetentionAnnotationHandler(),
|
||||
bindingContext,
|
||||
createTypeMapper())
|
||||
|
||||
+11
-5
@@ -29,23 +29,29 @@ import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
|
||||
internal class RoundAnnotations(
|
||||
val supportedAnnotationFqNames: Set<String>,
|
||||
val sourceRetentionAnnotationHandler: SourceRetentionAnnotationHandler?,
|
||||
val bindingContext: BindingContext,
|
||||
val typeMapper: KotlinTypeMapper
|
||||
) {
|
||||
private companion object {
|
||||
private val BLACKLISTED_ANNOTATATIONS = listOf(
|
||||
"java.lang.Deprecated", "kotlin.Deprecated", // Deprecated annotations
|
||||
"java.lang.annotation.", // Java annotations
|
||||
"org.jetbrains.annotations.", // Nullable/NotNull, ReadOnly, Mutable
|
||||
"kotlin.jvm.", "kotlin.Metadata" // Kotlin annotations from runtime
|
||||
)
|
||||
}
|
||||
|
||||
private val mutableAnnotationsMap = mutableMapOf<String, MutableList<PsiModifierListOwner>>()
|
||||
private val mutableAnalyzedClasses = mutableSetOf<String>()
|
||||
|
||||
private val acceptsAnyAnnotation = "*" in supportedAnnotationFqNames
|
||||
|
||||
val annotationsMap: Map<String, List<PsiModifierListOwner>>
|
||||
get() = mutableAnnotationsMap
|
||||
|
||||
val analyzedClasses: Set<String>
|
||||
get() = mutableAnalyzedClasses
|
||||
|
||||
fun copy() = RoundAnnotations(supportedAnnotationFqNames, sourceRetentionAnnotationHandler, bindingContext, typeMapper)
|
||||
fun copy() = RoundAnnotations(sourceRetentionAnnotationHandler, bindingContext, typeMapper)
|
||||
|
||||
fun analyzeFiles(files: Collection<KtFile>) = files.forEach { analyzeFile(it) }
|
||||
|
||||
@@ -105,7 +111,7 @@ internal class RoundAnnotations(
|
||||
}
|
||||
}
|
||||
|
||||
if (!acceptsAnyAnnotation && fqName !in supportedAnnotationFqNames) continue
|
||||
if (BLACKLISTED_ANNOTATATIONS.any { fqName.startsWith(it) }) continue
|
||||
mutableAnnotationsMap.getOrPut(fqName, { mutableListOf() }).add(declaration)
|
||||
|
||||
// Add only top-level classes
|
||||
|
||||
Reference in New Issue
Block a user