Messager implementation for KSP

I couldn't add a test for KSP because right now logging
errors do not fail compilation in KSP.
If by the time we need to run room compiler's failure tests,
if it is still not implemented in KSP, I'll update the
abstraction to track messages and throw error at the end
of compilation.

https://github.com/google/ksp/issues/122.

I've added a test to be enabled once that issue is fixed.

Bug: 160322705
Test: XProcessingEnvTest
Change-Id: Ied320bf329e026a71cc8c107f40e371e6bf8691e
diff --git a/room/compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KspMessager.kt b/room/compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KspMessager.kt
new file mode 100644
index 0000000..557cfa3
--- /dev/null
+++ b/room/compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KspMessager.kt
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package androidx.room.compiler.processing.ksp
+
+import androidx.room.compiler.processing.XElement
+import androidx.room.compiler.processing.XMessager
+import com.google.devtools.ksp.processing.KSPLogger
+import javax.tools.Diagnostic
+
+internal class KspMessager(
+    private val logger: KSPLogger
+) : XMessager {
+    override fun printMessage(kind: Diagnostic.Kind, msg: String, element: XElement?) {
+        val ksNode = (element as? KspElement)?.declaration
+        when (kind) {
+            Diagnostic.Kind.ERROR -> logger.error(msg, ksNode)
+            Diagnostic.Kind.WARNING -> logger.warn(msg, ksNode)
+            else -> logger.info(msg, ksNode)
+        }
+    }
+}
diff --git a/room/compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KspProcessingEnv.kt b/room/compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KspProcessingEnv.kt
index 2780f0a..0788904 100644
--- a/room/compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KspProcessingEnv.kt
+++ b/room/compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KspProcessingEnv.kt
@@ -37,7 +37,7 @@
 internal class KspProcessingEnv(
     override val options: Map<String, String>,
     codeGenerator: CodeGenerator,
-    private val logger: KSPLogger,
+    logger: KSPLogger,
     val resolver: Resolver
 ) : XProcessingEnv {
 
@@ -53,8 +53,7 @@
             }
         }
 
-    override val messager: XMessager
-        get() = TODO("Not yet implemented")
+    override val messager: XMessager = KspMessager(logger)
 
     override val filer: XFiler = KspFiler(codeGenerator)
 
diff --git a/room/compiler-processing/src/test/java/androidx/room/compiler/processing/XProcessingEnvTest.kt b/room/compiler-processing/src/test/java/androidx/room/compiler/processing/XProcessingEnvTest.kt
index 6c8efc0..0187061 100644
--- a/room/compiler-processing/src/test/java/androidx/room/compiler/processing/XProcessingEnvTest.kt
+++ b/room/compiler-processing/src/test/java/androidx/room/compiler/processing/XProcessingEnvTest.kt
@@ -19,6 +19,7 @@
 import androidx.room.compiler.processing.util.Source
 import androidx.room.compiler.processing.util.TestInvocation
 import androidx.room.compiler.processing.util.runProcessorTest
+import androidx.room.compiler.processing.util.runProcessorTestForFailedCompilation
 import androidx.room.compiler.processing.util.runProcessorTestIncludingKsp
 import com.google.common.truth.Truth.assertThat
 import com.squareup.javapoet.ClassName
@@ -29,6 +30,7 @@
 import org.junit.runner.RunWith
 import org.junit.runners.JUnit4
 import javax.lang.model.element.Modifier
+import javax.tools.Diagnostic
 
 @RunWith(JUnit4::class)
 class XProcessingEnvTest {
@@ -214,6 +216,25 @@
         }
     }
 
+    @Test
+    fun errorLogFailsCompilation() {
+        val src = Source.java(
+            "Foo.java",
+            """
+            class Foo {}
+            """.trimIndent()
+        )
+        // TODO include KSP when https://github.com/google/ksp/issues/122 is fixed.
+        runProcessorTestForFailedCompilation(
+            sources = listOf(src)
+        ) {
+            it.processingEnv.messager.printMessage(
+                Diagnostic.Kind.ERROR,
+                "intentional failure"
+            )
+        }
+    }
+
     companion object {
         val PRIMITIVE_TYPES = mapOf(
             TypeName.BOOLEAN.toString() to TypeName.BOOLEAN,