Report non-incremental annotation processors correctly

This commit fixes an issue when all APs would be reported as non-incremental,
even if only a single one is non-incremental. Now, additional declared type
is added that is used to denote processors that are incremental, but have
been forced to run non-incrementally in presence of non-incremental APs.

This means that only APs that do not support incremental annotation processing,
or APs that are dynamic and are non-incremental at runtime will be reported.
This commit is contained in:
Ivan Gavrilovic
2019-07-22 14:24:41 +01:00
committed by Yan Zhulanow
parent ad352355de
commit be3fe9495c
3 changed files with 41 additions and 21 deletions
@@ -64,10 +64,15 @@ open class ProcessorLoader(private val options: KaptOptions, private val logger:
val processorsInfo: Map<String, DeclaredProcType> = getIncrementalProcessorsFromClasspath(processorNames, classpath)
val nonIncremental = processorNames.filter { !processorsInfo.containsKey(it) }
return if (nonIncremental.isNotEmpty()) {
processors.map { IncrementalProcessor(it, DeclaredProcType.NON_INCREMENTAL) }
} else {
processors.map { IncrementalProcessor(it, processorsInfo.getValue(it.javaClass.name)) }
return processors.map {
val procType = processorsInfo[it.javaClass.name]?.let {
if (nonIncremental.isEmpty()) {
it
} else {
DeclaredProcType.INCREMENTAL_BUT_OTHER_APS_ARE_NOT
}
} ?: DeclaredProcType.NON_INCREMENTAL
IncrementalProcessor(it, procType)
}
}
@@ -13,7 +13,9 @@ import com.sun.tools.javac.processing.JavacFiler
import com.sun.tools.javac.processing.JavacProcessingEnvironment
import com.sun.tools.javac.tree.JCTree
import org.jetbrains.kotlin.base.kapt3.KaptFlag
import org.jetbrains.kotlin.kapt3.base.incremental.*
import org.jetbrains.kotlin.kapt3.base.incremental.GeneratedTypesTaskListener
import org.jetbrains.kotlin.kapt3.base.incremental.IncrementalProcessor
import org.jetbrains.kotlin.kapt3.base.incremental.MentionedTypesTaskListener
import org.jetbrains.kotlin.kapt3.base.util.KaptBaseError
import org.jetbrains.kotlin.kapt3.base.util.isJava9OrLater
import org.jetbrains.kotlin.kapt3.base.util.measureTimeMillisWithResult
@@ -51,7 +53,7 @@ fun KaptContext.doAnnotationProcessing(
val parsedJavaFiles = parseJavaFiles(javaSourceFiles)
val sourcesStructureListener = cacheManager?.let {
if (processors.any { it.kind == DeclaredProcType.NON_INCREMENTAL }) return@let null
if (processors.any { it.isUnableToRunIncrementally() }) return@let null
val recordTypesListener = MentionedTypesTaskListener(cacheManager.javaCache, processingEnvironment.elementUtils, Trees.instance(processingEnvironment))
compiler.getTaskListeners().add(recordTypesListener)
@@ -89,13 +91,15 @@ fun KaptContext.doAnnotationProcessing(
logger.info("Analyzing sources structure took ${it.time}[ms].")
}
}
if (cacheManager != null && processors.any { it.getRuntimeType() == RuntimeProcType.NON_INCREMENTAL }) {
val nonIncremental =
processors.filter { it.getRuntimeType() == RuntimeProcType.NON_INCREMENTAL }.map { it.processorName }
logger.warn(
"Incremental annotation processing requested, but support is disabled because the following " +
"processors are not incremental: ${nonIncremental.joinToString()}."
)
if (cacheManager != null) {
val missingIncrementalSupport = processors.filter { it.isMissingIncrementalSupport() }
if (missingIncrementalSupport.isNotEmpty()) {
val nonIncremental = missingIncrementalSupport.map { "${it.processorName} (${it.incrementalSupportType})" }
logger.warn(
"Incremental annotation processing requested, but support is disabled because the following " +
"processors are not incremental: ${nonIncremental.joinToString()}."
)
}
}
val log = compilerAfterAP.log
@@ -19,14 +19,15 @@ import javax.tools.JavaFileObject
private val ALLOWED_RUNTIME_TYPES = setOf(RuntimeProcType.AGGREGATING.name, RuntimeProcType.ISOLATING.name)
class IncrementalProcessor(private val processor: Processor, val kind: DeclaredProcType) : Processor by processor {
class IncrementalProcessor(private val processor: Processor, private val kind: DeclaredProcType) : Processor by processor {
private var dependencyCollector = lazy { createDependencyCollector() }
val processorName: String = processor.javaClass.name
val incrementalSupportType: String = kind.name
override fun init(processingEnv: ProcessingEnvironment) {
if (kind == DeclaredProcType.NON_INCREMENTAL) {
if (!kind.canRunIncrementally) {
processor.init(processingEnv)
} else {
val originalFiler = processingEnv.filer
@@ -58,6 +59,12 @@ class IncrementalProcessor(private val processor: Processor, val kind: DeclaredP
return AnnotationProcessorDependencyCollector(type)
}
fun isMissingIncrementalSupport(): Boolean {
if (kind == DeclaredProcType.NON_INCREMENTAL) return true
return kind == DeclaredProcType.DYNAMIC && getRuntimeType() == RuntimeProcType.NON_INCREMENTAL
}
fun isUnableToRunIncrementally() = !kind.canRunIncrementally
fun getGeneratedToSources() = dependencyCollector.value.getGeneratedToSources()
fun getRuntimeType(): RuntimeProcType = dependencyCollector.value.getRuntimeType()
}
@@ -137,19 +144,23 @@ private fun getSrcFiles(elements: Array<out Element?>): Set<File> {
}.toSet()
}
enum class DeclaredProcType {
AGGREGATING {
enum class DeclaredProcType(val canRunIncrementally: Boolean) {
AGGREGATING(true) {
override fun toRuntimeType() = RuntimeProcType.AGGREGATING
},
ISOLATING {
ISOLATING(true) {
override fun toRuntimeType() = RuntimeProcType.ISOLATING
},
DYNAMIC {
DYNAMIC(true) {
override fun toRuntimeType() = throw IllegalStateException("This should not be used")
},
NON_INCREMENTAL {
NON_INCREMENTAL(false) {
override fun toRuntimeType() = RuntimeProcType.NON_INCREMENTAL
};
},
INCREMENTAL_BUT_OTHER_APS_ARE_NOT(false) {
override fun toRuntimeType() = RuntimeProcType.NON_INCREMENTAL
},
;
abstract fun toRuntimeType(): RuntimeProcType
}