Update XProcessingStep.processOver() to include annotated elements.

The elements given to the step in processOver() are both from the last round environment and those that have been deferred.

Test: ./gradlew room:room-compiler-processing:test
Change-Id: I82af75bf857c24b4c695d6ab80801fe1ae82c965
diff --git a/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/XBasicAnnotationProcessor.kt b/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/XBasicAnnotationProcessor.kt
index 6ad8e6d..cb1a19e 100644
--- a/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/XBasicAnnotationProcessor.kt
+++ b/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/XBasicAnnotationProcessor.kt
@@ -52,6 +52,9 @@
             step: XProcessingStep,
             annotationQualifiedName: String
         ): Set<XElement> {
+            if (annotationQualifiedName == "*") {
+                return emptySet()
+            }
             val className = xProcessingEnv.requireTypeElement(annotationQualifiedName).className
             return elementsDeferredBySteps.getValue(step)
                 .filter { it.hasAnnotation(className) }
diff --git a/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/XProcessingStep.kt b/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/XProcessingStep.kt
index 234cb15..d88fb64 100644
--- a/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/XProcessingStep.kt
+++ b/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/XProcessingStep.kt
@@ -36,9 +36,12 @@
     /**
      * An optional hook for logic to be executed in the last round of processing.
      *
+     * Unlike [process], the elements in [elementsByAnnotation] are not validated and are those
+     * that have been kept being deferred.
+     *
      * @see [XRoundEnv.isProcessingOver]
      */
-    fun processOver(env: XProcessingEnv) { }
+    fun processOver(env: XProcessingEnv, elementsByAnnotation: Map<String, Set<XElement>>) { }
 
     /**
      * The set of annotation qualified names processed by this step.
diff --git a/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/javac/JavacBasicAnnotationProcessor.kt b/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/javac/JavacBasicAnnotationProcessor.kt
index 9f28325..7f0521a 100644
--- a/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/javac/JavacBasicAnnotationProcessor.kt
+++ b/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/javac/JavacBasicAnnotationProcessor.kt
@@ -57,7 +57,22 @@
     ): Boolean {
         val xRoundEnv = JavacRoundEnv(xEnv, roundEnv)
         if (roundEnv.processingOver()) {
-            steps.forEach { it.processOver(xEnv) }
+            steps.forEach { step ->
+                val elementsByAnnotation = step.annotations().mapNotNull { annotation ->
+                    val annotatedElements = xRoundEnv.getElementsAnnotatedWith(annotation) +
+                        getStepDeferredElementsAnnotatedWith(
+                            elementsDeferredBySteps,
+                            step,
+                            annotation
+                        )
+                    if (annotatedElements.isNotEmpty()) {
+                        annotation to annotatedElements
+                    } else {
+                        null
+                    }
+                }.toMap()
+                step.processOver(xEnv, elementsByAnnotation)
+            }
             postRound(xEnv, xRoundEnv)
             if (!roundEnv.errorRaised()) {
                 // Report missing elements if no error was raised to avoid being noisy.
@@ -76,9 +91,6 @@
         val currentElementsDeferredBySteps = steps.associateWith { step ->
             val deferredElements = mutableSetOf<XElement>()
             val elementsByAnnotation = step.annotations().mapNotNull { annotation ->
-                if (annotation == "*") {
-                    return@mapNotNull null
-                }
                 val annotatedElements = xRoundEnv.getElementsAnnotatedWith(annotation) +
                     getStepDeferredElementsAnnotatedWith(elementsDeferredBySteps, step, annotation)
                 val (validElements, invalidElements) = annotatedElements.partition {
diff --git a/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/javac/JavacRoundEnv.kt b/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/javac/JavacRoundEnv.kt
index ed6effa..d8d2c94 100644
--- a/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/javac/JavacRoundEnv.kt
+++ b/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/javac/JavacRoundEnv.kt
@@ -44,6 +44,10 @@
     }
 
     override fun getElementsAnnotatedWith(annotationQualifiedName: String): Set<XElement> {
+        if (annotationQualifiedName == "*") {
+            return emptySet()
+        }
+
         val element = env.elementUtils.getTypeElement(annotationQualifiedName)
             ?: error("Cannot find TypeElement: $annotationQualifiedName")
 
diff --git a/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KspBasicAnnotationProcessor.kt b/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KspBasicAnnotationProcessor.kt
index 2b14de2..d2c32fd 100644
--- a/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KspBasicAnnotationProcessor.kt
+++ b/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KspBasicAnnotationProcessor.kt
@@ -67,9 +67,6 @@
         val currentElementsDeferredBySteps = steps.associateWith { step ->
             val deferredElements = mutableSetOf<XElement>()
             val elementsByAnnotation = step.annotations().mapNotNull { annotation ->
-                if (annotation == "*") {
-                    return@mapNotNull null
-                }
                 val annotatedElements = xRoundEnv.getElementsAnnotatedWith(annotation) +
                     getStepDeferredElementsAnnotatedWith(elementsDeferredBySteps, step, annotation)
                 val (validElements, invalidElements) = annotatedElements.partition {
@@ -94,8 +91,20 @@
     }
 
     final override fun finish() {
-        steps.forEach { it.processOver(xEnv) }
-        postRound(xEnv, KspRoundEnv(xEnv, true))
+        val xRoundEnv = KspRoundEnv(xEnv, true)
+        steps.forEach { step ->
+            val elementsByAnnotation = step.annotations().mapNotNull { annotation ->
+                val annotatedElements = xRoundEnv.getElementsAnnotatedWith(annotation) +
+                    getStepDeferredElementsAnnotatedWith(elementsDeferredBySteps, step, annotation)
+                if (annotatedElements.isNotEmpty()) {
+                    annotation to annotatedElements
+                } else {
+                    null
+                }
+            }.toMap()
+            step.processOver(xEnv, elementsByAnnotation)
+        }
+        postRound(xEnv, xRoundEnv)
         if (!logger.hasError) {
             // Report missing elements if no error was raised to avoid being noisy.
             reportMissingElements(elementsDeferredBySteps.values.flatten().toSet())
diff --git a/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KspProcessingEnv.kt b/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KspProcessingEnv.kt
index ea17e57..ae79a0d 100644
--- a/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KspProcessingEnv.kt
+++ b/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KspProcessingEnv.kt
@@ -241,7 +241,6 @@
     }
 
     internal fun clearCache() {
-        _resolver = null
         typeElementStore.clear()
     }
 
diff --git a/room/room-compiler-processing/src/test/java/androidx/room/compiler/processing/XProcessingStepTest.kt b/room/room-compiler-processing/src/test/java/androidx/room/compiler/processing/XProcessingStepTest.kt
index b848cfc..62252e0 100644
--- a/room/room-compiler-processing/src/test/java/androidx/room/compiler/processing/XProcessingStepTest.kt
+++ b/room/room-compiler-processing/src/test/java/androidx/room/compiler/processing/XProcessingStepTest.kt
@@ -705,7 +705,10 @@
                 return deferredElements
             }
 
-            override fun processOver(env: XProcessingEnv) {
+            override fun processOver(
+                env: XProcessingEnv,
+                elementsByAnnotation: Map<String, Set<XElement>>
+            ) {
                 invokedProcessOver++
             }
         }
@@ -972,7 +975,10 @@
                 return deferredElements
             }
 
-            override fun processOver(env: XProcessingEnv) {
+            override fun processOver(
+                env: XProcessingEnv,
+                elementsByAnnotation: Map<String, Set<XElement>>
+            ) {
                 invokedProcessOver++
             }
         }