Make getTypeElement calls null-safe

We use elementUtils.getTypeElement in many places and sometimes it can be null,
sometimes it shouldn't.

This CL moves all of those calls into extension functions on the
processing environment where the expected nullability is explicit
in the method call.

There was also many usages of getTypeElement().asType() and
getTypeElement(typeName.toString) so I've created find/requreTypeMirror
and overloads that receive TypeName.

Bug: 160323720
Test: existing test suite
Change-Id: I3f31a38c86abd1b43a8a93d9b457c61bac802167
diff --git a/room/compiler/src/main/kotlin/androidx/room/ext/processing_env_ext.kt b/room/compiler/src/main/kotlin/androidx/room/ext/processing_env_ext.kt
new file mode 100644
index 0000000..101bdcd
--- /dev/null
+++ b/room/compiler/src/main/kotlin/androidx/room/ext/processing_env_ext.kt
@@ -0,0 +1,110 @@
+/*
+ * 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.ext
+
+import com.squareup.javapoet.TypeName
+import javax.annotation.processing.ProcessingEnvironment
+import javax.lang.model.element.TypeElement
+import javax.lang.model.type.TypeMirror
+import kotlin.reflect.KClass
+
+/**
+ * Query a type element by KClass and return null if it does not exist
+ */
+fun ProcessingEnvironment.findTypeElement(
+    klass: KClass<*>
+): TypeElement? = findTypeElement(klass.java.canonicalName!!)
+
+/**
+ * Query a type element by TypeName and return null if it does not exist
+ */
+fun ProcessingEnvironment.findTypeElement(
+    typeName: TypeName
+): TypeElement? = findTypeElement(typeName.toString())
+
+/**
+ * Query a type element by qualified name and return null if it does not exist
+ */
+fun ProcessingEnvironment.findTypeElement(
+    qName: String
+): TypeElement? = elementUtils.getTypeElement(qName)
+
+/**
+ * Query a type element by KClass and throw if it does not exist
+ */
+fun ProcessingEnvironment.requireTypeElement(
+    klass: KClass<*>
+): TypeElement = requireTypeElement(klass.java.canonicalName!!)
+
+/**
+ * Query a type element by TypeName and throw if it does not exist
+ */
+fun ProcessingEnvironment.requireTypeElement(
+    typeName: TypeName
+): TypeElement = requireTypeElement(typeName.toString())
+
+/**
+ * Query a type element by qualified name and throw if it does not exist
+ */
+fun ProcessingEnvironment.requireTypeElement(
+    qName: String
+): TypeElement = checkNotNull(elementUtils.getTypeElement(qName)) {
+    // we do not throw MissingTypeException here as this should be called only if the type should
+    // be there
+    "Couldn't find required type $qName"
+}
+
+/**
+ * Query a TypeMirror by KClass and throw if it does not exist
+ */
+fun ProcessingEnvironment.requireTypeMirror(
+    klass: KClass<*>
+): TypeMirror = requireTypeMirror(klass.java.canonicalName!!)
+
+/**
+ * Query a TypeMirror by TypeName and throw if it does not exist
+ */
+fun ProcessingEnvironment.requireTypeMirror(
+    typeName: TypeName
+): TypeMirror = requireTypeMirror(typeName.toString())
+
+/**
+ * Query a TypeMirror by qualified name and throw if it does not exist
+ */
+fun ProcessingEnvironment.requireTypeMirror(
+    qName: String
+): TypeMirror = requireTypeElement(qName).asType()
+
+/**
+ * Query a type mirror by KClass and return null if it does not exist
+ */
+fun ProcessingEnvironment.findTypeMirror(
+    klass: KClass<*>
+): TypeMirror? = findTypeMirror(klass.java.canonicalName!!)
+
+/**
+ * Query a type mirror by TypeName and return null if it does not exist
+ */
+fun ProcessingEnvironment.findTypeMirror(
+    typeName: TypeName
+): TypeMirror? = findTypeMirror(typeName.toString())
+
+/**
+ * Query a type mirror by qualified name and return null if it does not exist
+ */
+fun ProcessingEnvironment.findTypeMirror(
+    qName: String
+): TypeMirror? = findTypeElement(qName)?.asType()
diff --git a/room/compiler/src/main/kotlin/androidx/room/parser/SqlParser.kt b/room/compiler/src/main/kotlin/androidx/room/parser/SqlParser.kt
index 8a2d160..4d817c7 100644
--- a/room/compiler/src/main/kotlin/androidx/room/parser/SqlParser.kt
+++ b/room/compiler/src/main/kotlin/androidx/room/parser/SqlParser.kt
@@ -17,6 +17,7 @@
 package androidx.room.parser
 
 import androidx.room.ColumnInfo
+import androidx.room.ext.requireTypeMirror
 import org.antlr.v4.runtime.tree.ParseTree
 import org.antlr.v4.runtime.tree.TerminalNode
 import javax.annotation.processing.ProcessingEnvironment
@@ -191,7 +192,7 @@
     fun getTypeMirrors(env: ProcessingEnvironment): List<TypeMirror>? {
         val typeUtils = env.typeUtils
         return when (this) {
-            TEXT -> listOf(env.elementUtils.getTypeElement("java.lang.String").asType())
+            TEXT -> listOf(env.requireTypeMirror("java.lang.String"))
             INTEGER -> withBoxedTypes(
                 env, TypeKind.INT, TypeKind.BYTE, TypeKind.CHAR,
                 TypeKind.LONG, TypeKind.SHORT
diff --git a/room/compiler/src/main/kotlin/androidx/room/processor/Context.kt b/room/compiler/src/main/kotlin/androidx/room/processor/Context.kt
index 9eacbd3..408b149 100644
--- a/room/compiler/src/main/kotlin/androidx/room/processor/Context.kt
+++ b/room/compiler/src/main/kotlin/androidx/room/processor/Context.kt
@@ -18,6 +18,7 @@
 
 import androidx.room.RewriteQueriesToDropUnusedColumns
 import androidx.room.ext.hasAnnotation
+import androidx.room.ext.requireTypeMirror
 import androidx.room.log.RLog
 import androidx.room.parser.expansion.ProjectionExpander
 import androidx.room.parser.optimization.RemoveUnusedColumnQueryRewriter
@@ -96,13 +97,13 @@
 
     class CommonTypes(val processingEnv: ProcessingEnvironment) {
         val VOID: TypeMirror by lazy {
-            processingEnv.elementUtils.getTypeElement("java.lang.Void").asType()
+            processingEnv.requireTypeMirror("java.lang.Void")
         }
         val STRING: TypeMirror by lazy {
-            processingEnv.elementUtils.getTypeElement("java.lang.String").asType()
+            processingEnv.requireTypeMirror("java.lang.String")
         }
         val COLLECTION: TypeMirror by lazy {
-            processingEnv.elementUtils.getTypeElement("java.util.Collection").asType()
+            processingEnv.requireTypeMirror("java.util.Collection")
         }
     }
 
diff --git a/room/compiler/src/main/kotlin/androidx/room/processor/DatabaseProcessor.kt b/room/compiler/src/main/kotlin/androidx/room/processor/DatabaseProcessor.kt
index d4c3af7..e99788b 100644
--- a/room/compiler/src/main/kotlin/androidx/room/processor/DatabaseProcessor.kt
+++ b/room/compiler/src/main/kotlin/androidx/room/processor/DatabaseProcessor.kt
@@ -21,6 +21,7 @@
 import androidx.room.ext.RoomTypeNames
 import androidx.room.ext.hasAnnotation
 import androidx.room.ext.hasAnyOf
+import androidx.room.ext.requireTypeMirror
 import androidx.room.ext.toAnnotationBox
 import androidx.room.verifier.DatabaseVerificationErrors
 import androidx.room.verifier.DatabaseVerifier
@@ -48,9 +49,8 @@
     val context = baseContext.fork(element)
 
     val roomDatabaseType: TypeMirror by lazy {
-        context.processingEnv.elementUtils.getTypeElement(
+        context.processingEnv.requireTypeMirror(
                 RoomTypeNames.ROOM_DB.packageName() + "." + RoomTypeNames.ROOM_DB.simpleName())
-                .asType()
     }
 
     fun process(): Database {
diff --git a/room/compiler/src/main/kotlin/androidx/room/processor/FtsTableEntityProcessor.kt b/room/compiler/src/main/kotlin/androidx/room/processor/FtsTableEntityProcessor.kt
index 1059368..bfd0191 100644
--- a/room/compiler/src/main/kotlin/androidx/room/processor/FtsTableEntityProcessor.kt
+++ b/room/compiler/src/main/kotlin/androidx/room/processor/FtsTableEntityProcessor.kt
@@ -22,6 +22,7 @@
 import androidx.room.FtsOptions.Order
 import androidx.room.ext.AnnotationBox
 import androidx.room.ext.hasAnnotation
+import androidx.room.ext.requireTypeMirror
 import androidx.room.ext.toAnnotationBox
 import androidx.room.parser.FtsVersion
 import androidx.room.parser.SQLTypeAffinity
@@ -157,8 +158,7 @@
             return null
         }
 
-        val defaultType = context.processingEnv.elementUtils
-                    .getTypeElement(Object::class.java.canonicalName).asType()
+        val defaultType = context.processingEnv.requireTypeMirror(Object::class)
         if (context.processingEnv.typeUtils.isSameType(entityType, defaultType)) {
             return null
         }
diff --git a/room/compiler/src/main/kotlin/androidx/room/processor/MethodProcessorDelegate.kt b/room/compiler/src/main/kotlin/androidx/room/processor/MethodProcessorDelegate.kt
index 6274c80..e40797e 100644
--- a/room/compiler/src/main/kotlin/androidx/room/processor/MethodProcessorDelegate.kt
+++ b/room/compiler/src/main/kotlin/androidx/room/processor/MethodProcessorDelegate.kt
@@ -21,7 +21,9 @@
 import androidx.room.ext.N
 import androidx.room.ext.RoomCoroutinesTypeNames
 import androidx.room.ext.T
+import androidx.room.ext.findTypeElement
 import androidx.room.ext.getSuspendFunctionReturnType
+import androidx.room.ext.requireTypeMirror
 import androidx.room.kotlin.KotlinMetadataElement
 import androidx.room.parser.ParsedQuery
 import androidx.room.solver.prepared.binder.CallablePreparedQueryResultBinder.Companion.createPreparedBinder
@@ -98,8 +100,8 @@
             val kotlinMetadata =
                 KotlinMetadataElement.createFor(context, executableElement.enclosingElement)
             return if (kotlinMetadata?.isSuspendFunction(executableElement) == true) {
-                val hasCoroutineArtifact = context.processingEnv.elementUtils
-                    .getTypeElement(RoomCoroutinesTypeNames.COROUTINES_ROOM.toString()) != null
+                val hasCoroutineArtifact = context.processingEnv
+                    .findTypeElement(RoomCoroutinesTypeNames.COROUTINES_ROOM.toString()) != null
                 if (!hasCoroutineArtifact) {
                     context.logger.e(ProcessorErrors.MISSING_ROOM_COROUTINE_ARTIFACT)
                 }
@@ -172,9 +174,8 @@
     private val continuationParam: VariableElement by lazy {
         val typesUtil = context.processingEnv.typeUtils
         val continuationType = typesUtil.erasure(
-            context.processingEnv.elementUtils
-                .getTypeElement(KotlinTypeNames.CONTINUATION.toString())
-                .asType()
+            context.processingEnv
+                .requireTypeMirror(KotlinTypeNames.CONTINUATION.toString())
         )
         executableElement.parameters.last {
             typesUtil.isSameType(typesUtil.erasure(it.asType()), continuationType)
diff --git a/room/compiler/src/main/kotlin/androidx/room/processor/PojoProcessor.kt b/room/compiler/src/main/kotlin/androidx/room/processor/PojoProcessor.kt
index ad7fbb24..86f9cd1 100644
--- a/room/compiler/src/main/kotlin/androidx/room/processor/PojoProcessor.kt
+++ b/room/compiler/src/main/kotlin/androidx/room/processor/PojoProcessor.kt
@@ -23,6 +23,7 @@
 import androidx.room.PrimaryKey
 import androidx.room.Relation
 import androidx.room.ext.extendsBoundOrSelf
+import androidx.room.ext.findTypeElement
 import androidx.room.ext.getAllFieldsIncludingPrivateSupers
 import androidx.room.ext.getAllMethodsIncludingSupers
 import androidx.room.ext.hasAnnotation
@@ -99,10 +100,10 @@
             referenceStack: LinkedHashSet<Name> = LinkedHashSet()
         ): PojoProcessor {
             val (pojoElement, delegate) = if (element.hasAnnotation(AutoValue::class)) {
-                val elementUtils = context.processingEnv.elementUtils
+                val processingEnv = context.processingEnv
                 val autoValueGeneratedElement = element.let {
                     val typeName = AutoValuePojoProcessorDelegate.getGeneratedClassName(it)
-                    elementUtils.getTypeElement(typeName) ?: throw MissingTypeException(typeName)
+                    processingEnv.findTypeElement(typeName) ?: throw MissingTypeException(typeName)
                 }
                 autoValueGeneratedElement to AutoValuePojoProcessorDelegate(context, element)
             } else {
diff --git a/room/compiler/src/main/kotlin/androidx/room/processor/RawQueryMethodProcessor.kt b/room/compiler/src/main/kotlin/androidx/room/processor/RawQueryMethodProcessor.kt
index e98d7e6..bb38482 100644
--- a/room/compiler/src/main/kotlin/androidx/room/processor/RawQueryMethodProcessor.kt
+++ b/room/compiler/src/main/kotlin/androidx/room/processor/RawQueryMethodProcessor.kt
@@ -21,6 +21,7 @@
 import androidx.room.ext.SupportDbTypeNames
 import androidx.room.ext.hasAnnotation
 import androidx.room.ext.isEntityElement
+import androidx.room.ext.requireTypeMirror
 import androidx.room.ext.toAnnotationBox
 import androidx.room.ext.typeName
 import androidx.room.parser.SqlParser
@@ -111,16 +112,15 @@
                     types,
                     containing,
                     extractParams[0])
-            val elementUtils = context.processingEnv.elementUtils
-            val supportQueryType = elementUtils
-                    .getTypeElement(SupportDbTypeNames.QUERY.toString()).asType()
+            val processingEnv = context.processingEnv
+            val supportQueryType = processingEnv.requireTypeMirror(SupportDbTypeNames.QUERY)
             val isSupportSql = supportQueryType.isAssignableFrom(types, param)
             if (isSupportSql) {
                 return RawQueryMethod.RuntimeQueryParameter(
                         paramName = extractParams[0].simpleName.toString(),
                         type = supportQueryType.typeName())
             }
-            val stringType = elementUtils.getTypeElement("java.lang.String").asType()
+            val stringType = processingEnv.requireTypeMirror("java.lang.String")
             val isString = stringType.isAssignableFrom(types, param)
             if (isString) {
                 // special error since this was initially allowed but removed in 1.1 beta1
diff --git a/room/compiler/src/main/kotlin/androidx/room/processor/ShortcutParameterProcessor.kt b/room/compiler/src/main/kotlin/androidx/room/processor/ShortcutParameterProcessor.kt
index 78d2a93..8b2621b 100644
--- a/room/compiler/src/main/kotlin/androidx/room/processor/ShortcutParameterProcessor.kt
+++ b/room/compiler/src/main/kotlin/androidx/room/processor/ShortcutParameterProcessor.kt
@@ -17,6 +17,7 @@
 package androidx.room.processor
 
 import androidx.room.ext.extendsBound
+import androidx.room.ext.requireTypeMirror
 import androidx.room.vo.ShortcutQueryParameter
 import com.google.auto.common.MoreTypes
 import isAssignableFrom
@@ -55,6 +56,7 @@
     @Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN")
     private fun extractPojoType(typeMirror: TypeMirror): Pair<TypeMirror?, Boolean> {
 
+        val processingEnv = context.processingEnv
         val elementUtils = context.processingEnv.elementUtils
         val typeUtils = context.processingEnv.typeUtils
 
@@ -81,8 +83,8 @@
             throw IllegalArgumentException("iterator() not found in Iterable $iterableType")
         }
 
-        val iterableType = typeUtils.erasure(elementUtils
-                .getTypeElement("java.lang.Iterable").asType())
+        val iterableType = typeUtils.erasure(processingEnv
+                .requireTypeMirror("java.lang.Iterable"))
         if (iterableType.isAssignableFrom(typeUtils, typeMirror)) {
             val declared = MoreTypes.asDeclared(typeMirror)
             val pojo = extractPojoTypeFromIterator(declared)
diff --git a/room/compiler/src/main/kotlin/androidx/room/processor/TransactionMethodProcessor.kt b/room/compiler/src/main/kotlin/androidx/room/processor/TransactionMethodProcessor.kt
index 9bed2ad..506e422 100644
--- a/room/compiler/src/main/kotlin/androidx/room/processor/TransactionMethodProcessor.kt
+++ b/room/compiler/src/main/kotlin/androidx/room/processor/TransactionMethodProcessor.kt
@@ -21,6 +21,7 @@
 import androidx.room.ext.RxJava2TypeNames
 import androidx.room.ext.RxJava3TypeNames
 import androidx.room.ext.findKotlinDefaultImpl
+import androidx.room.ext.findTypeMirror
 import androidx.room.ext.hasAnyOf
 import androidx.room.vo.TransactionMethod
 import isAssignableFrom
@@ -41,19 +42,19 @@
 
     fun process(): TransactionMethod {
         val delegate = MethodProcessorDelegate.createFor(context, containing, executableElement)
-        val kotlinDefaultImpl =
-                executableElement.findKotlinDefaultImpl(context.processingEnv.typeUtils)
+        val typeUtils = context.processingEnv.typeUtils
+        val kotlinDefaultImpl = executableElement.findKotlinDefaultImpl(typeUtils)
         context.checker.check(
                 !executableElement.hasAnyOf(PRIVATE, FINAL) &&
                         (!executableElement.hasAnyOf(ABSTRACT) || kotlinDefaultImpl != null),
                 executableElement, ProcessorErrors.TRANSACTION_METHOD_MODIFIERS)
 
         val returnType = delegate.extractReturnType()
-        val erasureReturnType = context.processingEnv.typeUtils.erasure(returnType)
+        val erasureReturnType = typeUtils.erasure(returnType)
 
         DEFERRED_TYPES.firstOrNull { className ->
-            context.processingEnv.elementUtils.getTypeElement(className.toString())?.asType()?.let {
-                erasureReturnType.isAssignableFrom(context.processingEnv.typeUtils, it)
+            context.processingEnv.findTypeMirror(className)?.let {
+                erasureReturnType.isAssignableFrom(typeUtils, it)
             } ?: false
         }?.let { returnTypeName ->
             context.logger.e(
diff --git a/room/compiler/src/main/kotlin/androidx/room/solver/binderprovider/DataSourceFactoryQueryResultBinderProvider.kt b/room/compiler/src/main/kotlin/androidx/room/solver/binderprovider/DataSourceFactoryQueryResultBinderProvider.kt
index a92a0d2..d01d1b8 100644
--- a/room/compiler/src/main/kotlin/androidx/room/solver/binderprovider/DataSourceFactoryQueryResultBinderProvider.kt
+++ b/room/compiler/src/main/kotlin/androidx/room/solver/binderprovider/DataSourceFactoryQueryResultBinderProvider.kt
@@ -17,6 +17,7 @@
 package androidx.room.solver.binderprovider
 
 import androidx.room.ext.PagingTypeNames
+import androidx.room.ext.findTypeMirror
 import androidx.room.parser.ParsedQuery
 import androidx.room.processor.Context
 import androidx.room.processor.ProcessorErrors
@@ -31,8 +32,7 @@
 
 class DataSourceFactoryQueryResultBinderProvider(val context: Context) : QueryResultBinderProvider {
     private val dataSourceFactoryTypeMirror: TypeMirror? by lazy {
-        context.processingEnv.elementUtils
-                .getTypeElement(PagingTypeNames.DATA_SOURCE_FACTORY.toString())?.asType()
+        context.processingEnv.findTypeMirror(PagingTypeNames.DATA_SOURCE_FACTORY)
     }
 
     override fun provide(declared: DeclaredType, query: ParsedQuery): QueryResultBinder {
diff --git a/room/compiler/src/main/kotlin/androidx/room/solver/binderprovider/DataSourceQueryResultBinderProvider.kt b/room/compiler/src/main/kotlin/androidx/room/solver/binderprovider/DataSourceQueryResultBinderProvider.kt
index a1d9e65..fe57782 100644
--- a/room/compiler/src/main/kotlin/androidx/room/solver/binderprovider/DataSourceQueryResultBinderProvider.kt
+++ b/room/compiler/src/main/kotlin/androidx/room/solver/binderprovider/DataSourceQueryResultBinderProvider.kt
@@ -17,6 +17,7 @@
 package androidx.room.solver.binderprovider
 
 import androidx.room.ext.PagingTypeNames
+import androidx.room.ext.findTypeMirror
 import androidx.room.parser.ParsedQuery
 import androidx.room.processor.Context
 import androidx.room.processor.ProcessorErrors
@@ -30,13 +31,11 @@
 
 class DataSourceQueryResultBinderProvider(val context: Context) : QueryResultBinderProvider {
     private val dataSourceTypeMirror: TypeMirror? by lazy {
-        context.processingEnv.elementUtils
-                .getTypeElement(PagingTypeNames.DATA_SOURCE.toString())?.asType()
+        context.processingEnv.findTypeMirror(PagingTypeNames.DATA_SOURCE)
     }
 
     private val positionalDataSourceTypeMirror: TypeMirror? by lazy {
-        context.processingEnv.elementUtils
-                .getTypeElement(PagingTypeNames.POSITIONAL_DATA_SOURCE.toString())?.asType()
+        context.processingEnv.findTypeMirror(PagingTypeNames.POSITIONAL_DATA_SOURCE)
     }
 
     override fun provide(declared: DeclaredType, query: ParsedQuery): QueryResultBinder {
diff --git a/room/compiler/src/main/kotlin/androidx/room/solver/binderprovider/LiveDataQueryResultBinderProvider.kt b/room/compiler/src/main/kotlin/androidx/room/solver/binderprovider/LiveDataQueryResultBinderProvider.kt
index 165d1f6..ad73e34 100644
--- a/room/compiler/src/main/kotlin/androidx/room/solver/binderprovider/LiveDataQueryResultBinderProvider.kt
+++ b/room/compiler/src/main/kotlin/androidx/room/solver/binderprovider/LiveDataQueryResultBinderProvider.kt
@@ -17,6 +17,7 @@
 package androidx.room.solver.binderprovider
 
 import androidx.room.ext.LifecyclesTypeNames
+import androidx.room.ext.findTypeMirror
 import androidx.room.processor.Context
 import androidx.room.solver.ObservableQueryResultBinderProvider
 import androidx.room.solver.query.result.LiveDataQueryResultBinder
@@ -29,8 +30,7 @@
 class LiveDataQueryResultBinderProvider(context: Context) :
     ObservableQueryResultBinderProvider(context) {
     private val liveDataTypeMirror: TypeMirror? by lazy {
-        context.processingEnv.elementUtils
-                .getTypeElement(LifecyclesTypeNames.LIVE_DATA.toString())?.asType()
+        context.processingEnv.findTypeMirror(LifecyclesTypeNames.LIVE_DATA)
     }
 
     override fun extractTypeArg(declared: DeclaredType): TypeMirror = declared.typeArguments.first()
diff --git a/room/compiler/src/main/kotlin/androidx/room/solver/binderprovider/PagingSourceQueryResultBinderProvider.kt b/room/compiler/src/main/kotlin/androidx/room/solver/binderprovider/PagingSourceQueryResultBinderProvider.kt
index 150df6a..b1e7f97 100644
--- a/room/compiler/src/main/kotlin/androidx/room/solver/binderprovider/PagingSourceQueryResultBinderProvider.kt
+++ b/room/compiler/src/main/kotlin/androidx/room/solver/binderprovider/PagingSourceQueryResultBinderProvider.kt
@@ -17,6 +17,7 @@
 package androidx.room.solver.binderprovider
 
 import androidx.room.ext.PagingTypeNames
+import androidx.room.ext.findTypeMirror
 import androidx.room.ext.typeName
 import androidx.room.parser.ParsedQuery
 import androidx.room.processor.Context
@@ -33,8 +34,7 @@
 
 class PagingSourceQueryResultBinderProvider(val context: Context) : QueryResultBinderProvider {
     private val pagingSourceTypeMirror: TypeMirror? by lazy {
-        context.processingEnv.elementUtils
-            .getTypeElement(PagingTypeNames.PAGING_SOURCE.toString())?.asType()
+        context.processingEnv.findTypeMirror(PagingTypeNames.PAGING_SOURCE)
     }
 
     override fun provide(declared: DeclaredType, query: ParsedQuery): QueryResultBinder {
diff --git a/room/compiler/src/main/kotlin/androidx/room/solver/binderprovider/QueryResultBinderProviderWithRequiredArtifact.kt b/room/compiler/src/main/kotlin/androidx/room/solver/binderprovider/QueryResultBinderProviderWithRequiredArtifact.kt
index 0c83dd8..2a6c57a 100644
--- a/room/compiler/src/main/kotlin/androidx/room/solver/binderprovider/QueryResultBinderProviderWithRequiredArtifact.kt
+++ b/room/compiler/src/main/kotlin/androidx/room/solver/binderprovider/QueryResultBinderProviderWithRequiredArtifact.kt
@@ -15,6 +15,7 @@
  */
 package androidx.room.solver.binderprovider
 
+import androidx.room.ext.findTypeElement
 import androidx.room.parser.ParsedQuery
 import androidx.room.processor.Context
 import androidx.room.solver.QueryResultBinderProvider
@@ -43,7 +44,7 @@
     val delegate: QueryResultBinderProvider
 ) : QueryResultBinderProvider {
     private val hasRequiredArtifact by lazy(LazyThreadSafetyMode.NONE) {
-        context.processingEnv.elementUtils.getTypeElement(requiredType.toString()) != null
+        context.processingEnv.findTypeElement(requiredType) != null
     }
 
     override fun provide(declared: DeclaredType, query: ParsedQuery): QueryResultBinder {
diff --git a/room/compiler/src/main/kotlin/androidx/room/solver/binderprovider/RxQueryResultBinderProvider.kt b/room/compiler/src/main/kotlin/androidx/room/solver/binderprovider/RxQueryResultBinderProvider.kt
index aab23f6..ee5253c 100644
--- a/room/compiler/src/main/kotlin/androidx/room/solver/binderprovider/RxQueryResultBinderProvider.kt
+++ b/room/compiler/src/main/kotlin/androidx/room/solver/binderprovider/RxQueryResultBinderProvider.kt
@@ -16,6 +16,7 @@
 
 package androidx.room.solver.binderprovider
 
+import androidx.room.ext.findTypeMirror
 import androidx.room.processor.Context
 import androidx.room.solver.ObservableQueryResultBinderProvider
 import androidx.room.solver.RxType
@@ -31,8 +32,7 @@
     private val rxType: RxType
 ) : ObservableQueryResultBinderProvider(context) {
     private val typeMirror: TypeMirror? by lazy {
-        context.processingEnv.elementUtils
-            .getTypeElement(rxType.className.toString())?.asType()
+        context.processingEnv.findTypeMirror(rxType.className)
     }
 
     override fun extractTypeArg(declared: DeclaredType): TypeMirror = declared.typeArguments.first()
diff --git a/room/compiler/src/main/kotlin/androidx/room/solver/prepared/binderprovider/GuavaListenableFuturePreparedQueryResultBinderProvider.kt b/room/compiler/src/main/kotlin/androidx/room/solver/prepared/binderprovider/GuavaListenableFuturePreparedQueryResultBinderProvider.kt
index 9ecc94d..9c3ee1a 100644
--- a/room/compiler/src/main/kotlin/androidx/room/solver/prepared/binderprovider/GuavaListenableFuturePreparedQueryResultBinderProvider.kt
+++ b/room/compiler/src/main/kotlin/androidx/room/solver/prepared/binderprovider/GuavaListenableFuturePreparedQueryResultBinderProvider.kt
@@ -21,6 +21,7 @@
 import androidx.room.ext.N
 import androidx.room.ext.RoomGuavaTypeNames
 import androidx.room.ext.T
+import androidx.room.ext.findTypeElement
 import androidx.room.ext.typeName
 import androidx.room.parser.ParsedQuery
 import androidx.room.processor.Context
@@ -33,8 +34,7 @@
     PreparedQueryResultBinderProvider {
 
     private val hasGuavaRoom by lazy {
-        context.processingEnv.elementUtils
-            .getTypeElement(RoomGuavaTypeNames.GUAVA_ROOM.toString()) != null
+        context.processingEnv.findTypeElement(RoomGuavaTypeNames.GUAVA_ROOM) != null
     }
 
     override fun matches(declared: DeclaredType): Boolean =
diff --git a/room/compiler/src/main/kotlin/androidx/room/solver/prepared/binderprovider/RxPreparedQueryResultBinderProvider.kt b/room/compiler/src/main/kotlin/androidx/room/solver/prepared/binderprovider/RxPreparedQueryResultBinderProvider.kt
index c106669..8b3d052 100644
--- a/room/compiler/src/main/kotlin/androidx/room/solver/prepared/binderprovider/RxPreparedQueryResultBinderProvider.kt
+++ b/room/compiler/src/main/kotlin/androidx/room/solver/prepared/binderprovider/RxPreparedQueryResultBinderProvider.kt
@@ -18,6 +18,8 @@
 
 import androidx.room.ext.L
 import androidx.room.ext.T
+import androidx.room.ext.findTypeElement
+import androidx.room.ext.findTypeMirror
 import androidx.room.ext.typeName
 import androidx.room.parser.ParsedQuery
 import androidx.room.processor.Context
@@ -34,8 +36,7 @@
 ) : PreparedQueryResultBinderProvider {
 
     private val hasRxJavaArtifact by lazy {
-        context.processingEnv.elementUtils
-            .getTypeElement(rxType.version.rxRoomClassName.toString()) != null
+        context.processingEnv.findTypeElement(rxType.version.rxRoomClassName) != null
     }
 
     override fun matches(declared: DeclaredType): Boolean =
@@ -79,8 +80,7 @@
 ) : RxPreparedQueryResultBinderProvider(context, rxType) {
 
     private val completableType: TypeMirror? by lazy {
-        context.processingEnv.elementUtils
-            .getTypeElement(rxType.className.toString())?.asType()
+        context.processingEnv.findTypeMirror(rxType.className)
     }
 
     override fun matches(declared: DeclaredType): Boolean {
diff --git a/room/compiler/src/main/kotlin/androidx/room/solver/shortcut/binderprovider/GuavaListenableFutureDeleteOrUpdateMethodBinderProvider.kt b/room/compiler/src/main/kotlin/androidx/room/solver/shortcut/binderprovider/GuavaListenableFutureDeleteOrUpdateMethodBinderProvider.kt
index 106bc14..9961244 100644
--- a/room/compiler/src/main/kotlin/androidx/room/solver/shortcut/binderprovider/GuavaListenableFutureDeleteOrUpdateMethodBinderProvider.kt
+++ b/room/compiler/src/main/kotlin/androidx/room/solver/shortcut/binderprovider/GuavaListenableFutureDeleteOrUpdateMethodBinderProvider.kt
@@ -21,6 +21,7 @@
 import androidx.room.ext.N
 import androidx.room.ext.RoomGuavaTypeNames
 import androidx.room.ext.T
+import androidx.room.ext.findTypeElement
 import androidx.room.ext.typeName
 import androidx.room.processor.Context
 import androidx.room.processor.ProcessorErrors
@@ -36,8 +37,7 @@
 ) : DeleteOrUpdateMethodBinderProvider {
 
     private val hasGuavaRoom by lazy {
-        context.processingEnv.elementUtils
-            .getTypeElement(RoomGuavaTypeNames.GUAVA_ROOM.toString()) != null
+        context.processingEnv.findTypeElement(RoomGuavaTypeNames.GUAVA_ROOM) != null
     }
 
     override fun matches(declared: DeclaredType): Boolean =
diff --git a/room/compiler/src/main/kotlin/androidx/room/solver/shortcut/binderprovider/GuavaListenableFutureInsertMethodBinderProvider.kt b/room/compiler/src/main/kotlin/androidx/room/solver/shortcut/binderprovider/GuavaListenableFutureInsertMethodBinderProvider.kt
index f603510..5f6ea59 100644
--- a/room/compiler/src/main/kotlin/androidx/room/solver/shortcut/binderprovider/GuavaListenableFutureInsertMethodBinderProvider.kt
+++ b/room/compiler/src/main/kotlin/androidx/room/solver/shortcut/binderprovider/GuavaListenableFutureInsertMethodBinderProvider.kt
@@ -21,6 +21,7 @@
 import androidx.room.ext.N
 import androidx.room.ext.RoomGuavaTypeNames
 import androidx.room.ext.T
+import androidx.room.ext.findTypeElement
 import androidx.room.ext.typeName
 import androidx.room.processor.Context
 import androidx.room.processor.ProcessorErrors
@@ -37,8 +38,7 @@
 ) : InsertMethodBinderProvider {
 
     private val hasGuavaRoom by lazy {
-        context.processingEnv.elementUtils
-            .getTypeElement(RoomGuavaTypeNames.GUAVA_ROOM.toString()) != null
+        context.processingEnv.findTypeElement(RoomGuavaTypeNames.GUAVA_ROOM) != null
     }
 
     override fun matches(declared: DeclaredType): Boolean =
diff --git a/room/compiler/src/main/kotlin/androidx/room/solver/shortcut/binderprovider/RxCallableDeleteOrUpdateMethodBinderProvider.kt b/room/compiler/src/main/kotlin/androidx/room/solver/shortcut/binderprovider/RxCallableDeleteOrUpdateMethodBinderProvider.kt
index 76518c5..23318bf 100644
--- a/room/compiler/src/main/kotlin/androidx/room/solver/shortcut/binderprovider/RxCallableDeleteOrUpdateMethodBinderProvider.kt
+++ b/room/compiler/src/main/kotlin/androidx/room/solver/shortcut/binderprovider/RxCallableDeleteOrUpdateMethodBinderProvider.kt
@@ -18,6 +18,7 @@
 
 import androidx.room.ext.L
 import androidx.room.ext.T
+import androidx.room.ext.findTypeMirror
 import androidx.room.ext.typeName
 import androidx.room.processor.Context
 import androidx.room.solver.RxType
@@ -75,8 +76,7 @@
 ) : RxCallableDeleteOrUpdateMethodBinderProvider(context, rxType) {
 
     private val completableTypeMirror: TypeMirror? by lazy {
-        context.processingEnv.elementUtils
-                .getTypeElement(rxType.className.toString())?.asType()
+        context.processingEnv.findTypeMirror(rxType.className)
     }
 
     /**
diff --git a/room/compiler/src/main/kotlin/androidx/room/solver/shortcut/binderprovider/RxCallableInsertMethodBinderProvider.kt b/room/compiler/src/main/kotlin/androidx/room/solver/shortcut/binderprovider/RxCallableInsertMethodBinderProvider.kt
index a9bd9f6..e5a4765 100644
--- a/room/compiler/src/main/kotlin/androidx/room/solver/shortcut/binderprovider/RxCallableInsertMethodBinderProvider.kt
+++ b/room/compiler/src/main/kotlin/androidx/room/solver/shortcut/binderprovider/RxCallableInsertMethodBinderProvider.kt
@@ -18,6 +18,7 @@
 
 import androidx.room.ext.L
 import androidx.room.ext.T
+import androidx.room.ext.findTypeMirror
 import androidx.room.ext.typeName
 import androidx.room.processor.Context
 import androidx.room.solver.RxType
@@ -79,8 +80,7 @@
 ) : RxCallableInsertMethodBinderProvider(context, rxType) {
 
     private val completableTypeMirror: TypeMirror? by lazy {
-        context.processingEnv.elementUtils
-                .getTypeElement(rxType.className.toString())?.asType()
+        context.processingEnv.findTypeMirror(rxType.className)
     }
 
     /**
diff --git a/room/compiler/src/main/kotlin/androidx/room/solver/types/BoxedBooleanToBoxedIntConverter.kt b/room/compiler/src/main/kotlin/androidx/room/solver/types/BoxedBooleanToBoxedIntConverter.kt
index 015ede9..fd8d789 100644
--- a/room/compiler/src/main/kotlin/androidx/room/solver/types/BoxedBooleanToBoxedIntConverter.kt
+++ b/room/compiler/src/main/kotlin/androidx/room/solver/types/BoxedBooleanToBoxedIntConverter.kt
@@ -17,6 +17,7 @@
 package androidx.room.solver.types
 
 import androidx.room.ext.L
+import androidx.room.ext.requireTypeMirror
 import androidx.room.solver.CodeGenScope
 import javax.annotation.processing.ProcessingEnvironment
 
@@ -25,10 +26,8 @@
  */
 object BoxedBooleanToBoxedIntConverter {
     fun create(processingEnvironment: ProcessingEnvironment): List<TypeConverter> {
-        val tBoolean = processingEnvironment.elementUtils.getTypeElement("java.lang.Boolean")
-                .asType()
-        val tInt = processingEnvironment.elementUtils.getTypeElement("java.lang.Integer")
-                .asType()
+        val tBoolean = processingEnvironment.requireTypeMirror("java.lang.Boolean")
+        val tInt = processingEnvironment.requireTypeMirror("java.lang.Integer")
         return listOf(
                 object : TypeConverter(tBoolean, tInt) {
                     override fun convert(
diff --git a/room/compiler/src/main/kotlin/androidx/room/solver/types/ByteBufferColumnTypeAdapter.kt b/room/compiler/src/main/kotlin/androidx/room/solver/types/ByteBufferColumnTypeAdapter.kt
index 95dfb99..1289f08 100644
--- a/room/compiler/src/main/kotlin/androidx/room/solver/types/ByteBufferColumnTypeAdapter.kt
+++ b/room/compiler/src/main/kotlin/androidx/room/solver/types/ByteBufferColumnTypeAdapter.kt
@@ -18,6 +18,7 @@
 
 import androidx.room.ext.L
 import androidx.room.ext.T
+import androidx.room.ext.requireTypeMirror
 import androidx.room.parser.SQLTypeAffinity
 import androidx.room.solver.CodeGenScope
 import com.squareup.javapoet.TypeName
@@ -25,7 +26,7 @@
 import javax.annotation.processing.ProcessingEnvironment
 
 class ByteBufferColumnTypeAdapter(env: ProcessingEnvironment) : ColumnTypeAdapter(
-    out = env.elementUtils.getTypeElement("java.nio.ByteBuffer").asType(),
+    out = env.requireTypeMirror("java.nio.ByteBuffer"),
     typeAffinity = SQLTypeAffinity.BLOB
 ) {
     override fun readFromCursor(
diff --git a/room/compiler/src/main/kotlin/androidx/room/solver/types/StringColumnTypeAdapter.kt b/room/compiler/src/main/kotlin/androidx/room/solver/types/StringColumnTypeAdapter.kt
index 2fe0e91..2a8fa53 100644
--- a/room/compiler/src/main/kotlin/androidx/room/solver/types/StringColumnTypeAdapter.kt
+++ b/room/compiler/src/main/kotlin/androidx/room/solver/types/StringColumnTypeAdapter.kt
@@ -17,13 +17,13 @@
 package androidx.room.solver.types
 
 import androidx.room.ext.L
+import androidx.room.ext.requireTypeMirror
 import androidx.room.parser.SQLTypeAffinity.TEXT
 import androidx.room.solver.CodeGenScope
 import javax.annotation.processing.ProcessingEnvironment
 
 class StringColumnTypeAdapter(processingEnvironment: ProcessingEnvironment) :
-    ColumnTypeAdapter((processingEnvironment.elementUtils.getTypeElement(
-        String::class.java.canonicalName)).asType(), TEXT) {
+    ColumnTypeAdapter((processingEnvironment.requireTypeMirror(String::class)), TEXT) {
     override fun readFromCursor(
         outVarName: String,
         cursorVarName: String,
diff --git a/room/compiler/src/main/kotlin/androidx/room/vo/RelationCollector.kt b/room/compiler/src/main/kotlin/androidx/room/vo/RelationCollector.kt
index 1b536d1..54557cc 100644
--- a/room/compiler/src/main/kotlin/androidx/room/vo/RelationCollector.kt
+++ b/room/compiler/src/main/kotlin/androidx/room/vo/RelationCollector.kt
@@ -21,6 +21,9 @@
 import androidx.room.ext.L
 import androidx.room.ext.N
 import androidx.room.ext.T
+import androidx.room.ext.findTypeElement
+import androidx.room.ext.requireTypeElement
+import androidx.room.ext.requireTypeMirror
 import androidx.room.ext.typeName
 import androidx.room.parser.ParsedQuery
 import androidx.room.parser.SQLTypeAffinity
@@ -249,8 +252,8 @@
                 val usingLongSparseArray =
                     tmpMapType.rawType == CollectionTypeNames.LONG_SPARSE_ARRAY
                 val queryParam = if (usingLongSparseArray) {
-                    val longSparseArrayElement = context.processingEnv.elementUtils
-                            .getTypeElement(CollectionTypeNames.LONG_SPARSE_ARRAY.toString())
+                    val longSparseArrayElement = context.processingEnv
+                            .requireTypeElement(CollectionTypeNames.LONG_SPARSE_ARRAY)
                     QueryParameter(
                             name = RelationCollectorMethodWriter.PARAM_MAP_VARIABLE,
                             sqlName = RelationCollectorMethodWriter.PARAM_MAP_VARIABLE,
@@ -259,7 +262,7 @@
                     )
                 } else {
                     val keyTypeMirror = keyTypeMirrorFor(context, affinity)
-                    val set = context.processingEnv.elementUtils.getTypeElement("java.util.Set")
+                    val set = context.processingEnv.requireTypeElement("java.util.Set")
                     val keySet = context.processingEnv.typeUtils.getDeclaredType(set, keyTypeMirror)
                     QueryParameter(
                             name = RelationCollectorMethodWriter.KEY_SET_VARIABLE,
@@ -389,10 +392,10 @@
             keyType: TypeName,
             relationTypeName: TypeName
         ): ParameterizedTypeName {
-            val canUseLongSparseArray = context.processingEnv.elementUtils
-                .getTypeElement(CollectionTypeNames.LONG_SPARSE_ARRAY.toString()) != null
-            val canUseArrayMap = context.processingEnv.elementUtils
-                .getTypeElement(CollectionTypeNames.ARRAY_MAP.toString()) != null
+            val canUseLongSparseArray = context.processingEnv
+                .findTypeElement(CollectionTypeNames.LONG_SPARSE_ARRAY) != null
+            val canUseArrayMap = context.processingEnv
+                .findTypeElement(CollectionTypeNames.ARRAY_MAP) != null
             return when {
                 canUseLongSparseArray && affinity == SQLTypeAffinity.INTEGER -> {
                     ParameterizedTypeName.get(CollectionTypeNames.LONG_SPARSE_ARRAY,
@@ -411,12 +414,12 @@
 
         // Gets the type mirror of the relationship key.
         private fun keyTypeMirrorFor(context: Context, affinity: SQLTypeAffinity): TypeMirror {
-            val elements = context.processingEnv.elementUtils
+            val processingEnv = context.processingEnv
             return when (affinity) {
-                SQLTypeAffinity.INTEGER -> elements.getTypeElement("java.lang.Long").asType()
-                SQLTypeAffinity.REAL -> elements.getTypeElement("java.lang.Double").asType()
+                SQLTypeAffinity.INTEGER -> processingEnv.requireTypeMirror("java.lang.Long")
+                SQLTypeAffinity.REAL -> processingEnv.requireTypeMirror("java.lang.Double")
                 SQLTypeAffinity.TEXT -> context.COMMON_TYPES.STRING
-                SQLTypeAffinity.BLOB -> elements.getTypeElement("java.nio.ByteBuffer").asType()
+                SQLTypeAffinity.BLOB -> processingEnv.requireTypeMirror("java.nio.ByteBuffer")
                 else -> {
                     context.COMMON_TYPES.STRING
                 }
diff --git a/room/compiler/src/test/kotlin/androidx/room/kotlin/KotlinMetadataElementTest.kt b/room/compiler/src/test/kotlin/androidx/room/kotlin/KotlinMetadataElementTest.kt
index 5d89402..5118848 100644
--- a/room/compiler/src/test/kotlin/androidx/room/kotlin/KotlinMetadataElementTest.kt
+++ b/room/compiler/src/test/kotlin/androidx/room/kotlin/KotlinMetadataElementTest.kt
@@ -16,6 +16,7 @@
 
 package androidx.room.kotlin
 
+import androidx.room.ext.requireTypeElement
 import androidx.room.processor.Context
 import androidx.room.testing.TestInvocation
 import com.google.auto.common.MoreElements
@@ -97,7 +98,7 @@
     }
 
     private fun getMetadataElement(invocation: TestInvocation, klass: KClass<*>) =
-        invocation.typeElement(klass.java.canonicalName!!).let {
+        invocation.processingEnv.requireTypeElement(klass).let {
             it to KotlinMetadataElement.createFor(Context(invocation.processingEnv), it)!!
         }
 
diff --git a/room/compiler/src/test/kotlin/androidx/room/processor/BaseDaoTest.kt b/room/compiler/src/test/kotlin/androidx/room/processor/BaseDaoTest.kt
index 7226809..5e13336 100644
--- a/room/compiler/src/test/kotlin/androidx/room/processor/BaseDaoTest.kt
+++ b/room/compiler/src/test/kotlin/androidx/room/processor/BaseDaoTest.kt
@@ -2,6 +2,7 @@
 
 import COMMON
 import androidx.room.ext.RoomTypeNames
+import androidx.room.ext.requireTypeElement
 import androidx.room.vo.Dao
 import androidx.room.writer.DaoWriter
 import com.google.auto.common.MoreTypes
@@ -158,9 +159,9 @@
             }
         """.toJFO("foo.bar.MyDao")
         simpleRun(baseClass, extension, COMMON.USER) { invocation ->
-            val daoElm = invocation.processingEnv.elementUtils.getTypeElement("foo.bar.MyDao")
-            val dbElm = invocation.context.processingEnv.elementUtils
-                .getTypeElement(RoomTypeNames.ROOM_DB.toString())
+            val daoElm = invocation.processingEnv.requireTypeElement("foo.bar.MyDao")
+            val dbElm = invocation.context.processingEnv
+                .requireTypeElement(RoomTypeNames.ROOM_DB)
             val dbType = MoreTypes.asDeclared(dbElm.asType())
             val processedDao = DaoProcessor(
                 invocation.context, daoElm, dbType, null
diff --git a/room/compiler/src/test/kotlin/androidx/room/processor/CustomConverterProcessorTest.kt b/room/compiler/src/test/kotlin/androidx/room/processor/CustomConverterProcessorTest.kt
index fe9474e..c2e98ff 100644
--- a/room/compiler/src/test/kotlin/androidx/room/processor/CustomConverterProcessorTest.kt
+++ b/room/compiler/src/test/kotlin/androidx/room/processor/CustomConverterProcessorTest.kt
@@ -17,6 +17,7 @@
 package androidx.room.processor
 
 import androidx.room.TypeConverter
+import androidx.room.ext.requireTypeElement
 import androidx.room.ext.typeName
 import androidx.room.processor.ProcessorErrors.TYPE_CONVERTER_EMPTY_CLASS
 import androidx.room.processor.ProcessorErrors
@@ -207,7 +208,7 @@
                         }.build().toString())
 
         simpleRun(baseConverter, extendingClass) { invocation ->
-            val element = invocation.processingEnv.elementUtils.getTypeElement(extendingQName)
+            val element = invocation.processingEnv.requireTypeElement(extendingQName)
             val converter = CustomConverterProcessor(invocation.context, element)
                     .process().firstOrNull()
             assertThat(converter?.fromTypeName, `is`(ParameterizedTypeName.get(
@@ -264,7 +265,7 @@
     ): CompileTester {
         return simpleRun(*((jfo.toList() + CONTAINER).toTypedArray())) { invocation ->
             val processed = CustomConverterProcessor.findConverters(invocation.context,
-                    invocation.processingEnv.elementUtils.getTypeElement("foo.bar.Container"))
+                    invocation.processingEnv.requireTypeElement("foo.bar.Container"))
             handler(processed.converters.firstOrNull()?.custom, invocation)
         }
     }
diff --git a/room/compiler/src/test/kotlin/androidx/room/processor/DaoProcessorTest.kt b/room/compiler/src/test/kotlin/androidx/room/processor/DaoProcessorTest.kt
index 8e90716..1204399 100644
--- a/room/compiler/src/test/kotlin/androidx/room/processor/DaoProcessorTest.kt
+++ b/room/compiler/src/test/kotlin/androidx/room/processor/DaoProcessorTest.kt
@@ -18,6 +18,7 @@
 
 import COMMON
 import androidx.room.ext.RoomTypeNames
+import androidx.room.ext.requireTypeMirror
 import androidx.room.testing.TestInvocation
 import androidx.room.testing.TestProcessor
 import androidx.room.vo.Dao
@@ -168,8 +169,8 @@
                 abstract User users();
             }
             """) { dao, invocation ->
-            val dbType = MoreTypes.asDeclared(invocation.context.processingEnv.elementUtils
-                    .getTypeElement(RoomTypeNames.ROOM_DB.toString()).asType())
+            val dbType = MoreTypes.asDeclared(invocation.context.processingEnv
+                    .requireTypeMirror(RoomTypeNames.ROOM_DB.toString()))
             val daoProcessor =
                 DaoProcessor(invocation.context, dao.element, dbType, null)
 
@@ -197,8 +198,8 @@
                 abstract User users();
             }
             """) { dao, invocation ->
-            val dbType = MoreTypes.asDeclared(invocation.context.processingEnv.elementUtils
-                    .getTypeElement(RoomTypeNames.ROOM_DB.toString()).asType())
+            val dbType = MoreTypes.asDeclared(invocation.context.processingEnv
+                    .requireTypeMirror(RoomTypeNames.ROOM_DB))
             val daoProcessor =
                 DaoProcessor(invocation.context, dao.element, dbType, null)
             assertThat(daoProcessor.context.logger
@@ -328,9 +329,8 @@
                                 null
                             }
                             val dbType = MoreTypes.asDeclared(
-                                    invocation.context.processingEnv.elementUtils
-                                            .getTypeElement(RoomTypeNames.ROOM_DB.toString())
-                                            .asType())
+                                    invocation.context.processingEnv
+                                            .requireTypeMirror(RoomTypeNames.ROOM_DB))
                             val parser = DaoProcessor(invocation.context,
                                     MoreElements.asType(dao), dbType, dbVerifier)
 
diff --git a/room/compiler/src/test/kotlin/androidx/room/processor/FieldProcessorTest.kt b/room/compiler/src/test/kotlin/androidx/room/processor/FieldProcessorTest.kt
index 71d2a72f..0ddeee5 100644
--- a/room/compiler/src/test/kotlin/androidx/room/processor/FieldProcessorTest.kt
+++ b/room/compiler/src/test/kotlin/androidx/room/processor/FieldProcessorTest.kt
@@ -17,6 +17,7 @@
 package androidx.room.processor
 
 import androidx.room.Entity
+import androidx.room.ext.requireTypeMirror
 import androidx.room.parser.Collate
 import androidx.room.parser.SQLTypeAffinity
 import androidx.room.solver.types.ColumnTypeAdapter
@@ -106,7 +107,7 @@
         }
 
         private fun TypeKind.box(invocation: TestInvocation): TypeMirror {
-            return invocation.processingEnv.elementUtils.getTypeElement(box()).asType()
+            return invocation.processingEnv.requireTypeMirror(box())
         }
     }
 
diff --git a/room/compiler/src/test/kotlin/androidx/room/processor/PojoProcessorTargetMethodTest.kt b/room/compiler/src/test/kotlin/androidx/room/processor/PojoProcessorTargetMethodTest.kt
index a4f4afc..dda7928 100644
--- a/room/compiler/src/test/kotlin/androidx/room/processor/PojoProcessorTargetMethodTest.kt
+++ b/room/compiler/src/test/kotlin/androidx/room/processor/PojoProcessorTargetMethodTest.kt
@@ -16,6 +16,7 @@
 
 package androidx.room.processor
 
+import androidx.room.ext.requireTypeElement
 import com.google.testing.compile.CompileTester
 import com.squareup.javapoet.ClassName
 import org.junit.Test
@@ -404,7 +405,7 @@
 
     private fun singleRun(vararg jfos: JavaFileObject) = simpleRun(*jfos) { invocation ->
         PojoProcessor.createFor(context = invocation.context,
-            element = invocation.typeElement(MY_POJO.toString()),
+            element = invocation.processingEnv.requireTypeElement(MY_POJO),
             bindingScope = FieldProcessor.BindingScope.READ_FROM_CURSOR,
             parent = null).process()
     }
@@ -439,7 +440,7 @@
         val all = (jfos.toList() + pojoJFO + autoValuePojoJFO).toTypedArray()
         return simpleRun(*all) { invocation ->
             PojoProcessor.createFor(context = invocation.context,
-                element = invocation.typeElement(MY_POJO.toString()),
+                element = invocation.processingEnv.requireTypeElement(MY_POJO),
                 bindingScope = FieldProcessor.BindingScope.READ_FROM_CURSOR,
                 parent = null).process()
         }
diff --git a/room/compiler/src/test/kotlin/androidx/room/processor/PojoProcessorTest.kt b/room/compiler/src/test/kotlin/androidx/room/processor/PojoProcessorTest.kt
index 2afa385..26f7d81 100644
--- a/room/compiler/src/test/kotlin/androidx/room/processor/PojoProcessorTest.kt
+++ b/room/compiler/src/test/kotlin/androidx/room/processor/PojoProcessorTest.kt
@@ -18,6 +18,7 @@
 
 import COMMON
 import androidx.room.Embedded
+import androidx.room.ext.requireTypeElement
 import androidx.room.parser.SQLTypeAffinity
 import androidx.room.processor.ProcessorErrors.CANNOT_FIND_GETTER_FOR_FIELD
 import androidx.room.processor.ProcessorErrors.CANNOT_FIND_TYPE
@@ -93,7 +94,7 @@
                 """.toJFO(MY_POJO.toString()),
                 parent.toJFO("foo.bar.x.BaseClass")) { invocation ->
             val pojo = PojoProcessor.createFor(context = invocation.context,
-                    element = invocation.typeElement(MY_POJO.toString()),
+                    element = invocation.processingEnv.requireTypeElement(MY_POJO),
                     bindingScope = FieldProcessor.BindingScope.READ_FROM_CURSOR,
                     parent = null).process()
             assertThat(pojo.fields.find { it.name == "myField" }, notNullValue())
@@ -847,7 +848,7 @@
             $FOOTER
             """.toJFO(MY_POJO.toString())
         simpleRun(pojo) { invocation ->
-            val element = invocation.typeElement(MY_POJO.toString())
+            val element = invocation.processingEnv.requireTypeElement(MY_POJO)
             val pojo1 = PojoProcessor.createFor(invocation.context, element,
                     FieldProcessor.BindingScope.BIND_TO_STMT, null).process()
             assertThat(pojo1, notNullValue())
@@ -1029,7 +1030,7 @@
             String mLastName;
         """) { _, invocation ->
             val process2 = PojoProcessor.createFor(context = invocation.context,
-                    element = invocation.typeElement(MY_POJO.toString()),
+                    element = invocation.processingEnv.requireTypeElement(MY_POJO),
                     bindingScope = FieldProcessor.BindingScope.BIND_TO_STMT,
                     parent = null).process()
             assertThat(process2.constructor, nullValue())
@@ -1043,7 +1044,7 @@
             String mLastName;
         """) { _, invocation ->
             val process2 = PojoProcessor.createFor(context = invocation.context,
-                    element = invocation.typeElement(MY_POJO.toString()),
+                    element = invocation.processingEnv.requireTypeElement(MY_POJO),
                     bindingScope = FieldProcessor.BindingScope.TWO_WAY,
                     parent = null).process()
             assertThat(process2.constructor, notNullValue())
@@ -1336,7 +1337,7 @@
             simpleRun { invocation ->
                 PojoProcessor.createFor(
                         context = invocation.context,
-                        element = invocation.typeElement(it),
+                        element = invocation.processingEnv.requireTypeElement(it),
                         bindingScope = FieldProcessor.BindingScope.READ_FROM_CURSOR,
                         parent = null
                 ).process()
@@ -1349,8 +1350,8 @@
         simpleRun { invocation ->
             PojoProcessor.createFor(
                     context = invocation.context,
-                    element = invocation.typeElement(
-                            TestData.WithJvmOverloads::class.java.canonicalName!!
+                    element = invocation.processingEnv.requireTypeElement(
+                            TestData.WithJvmOverloads::class
                     ),
                     bindingScope = FieldProcessor.BindingScope.READ_FROM_CURSOR,
                     parent = null
@@ -1372,7 +1373,7 @@
                 }
                 """.toJFO(MY_POJO.toString())) { invocation ->
             val pojo = PojoProcessor.createFor(context = invocation.context,
-                    element = invocation.typeElement(MY_POJO.toString()),
+                    element = invocation.processingEnv.requireTypeElement(MY_POJO),
                     bindingScope = FieldProcessor.BindingScope.READ_FROM_CURSOR,
                     parent = null).process()
             assertThat(pojo.fields.find { it.name == "foo" }, notNullValue())
@@ -1403,7 +1404,7 @@
                 }
                 """.toJFO(MY_POJO.toString())) { invocation ->
             val pojo = PojoProcessor.createFor(context = invocation.context,
-                element = invocation.typeElement(MY_POJO.toString()),
+                element = invocation.processingEnv.requireTypeElement(MY_POJO),
                 bindingScope = FieldProcessor.BindingScope.READ_FROM_CURSOR,
                     parent = null).process()
             assertThat(pojo.fields.find { it.name == "foo" }, notNullValue())
@@ -1433,7 +1434,7 @@
                 }
                 """.toJFO(MY_POJO.toString())) { invocation ->
             val pojo = PojoProcessor.createFor(context = invocation.context,
-                element = invocation.typeElement(MY_POJO.toString()),
+                element = invocation.processingEnv.requireTypeElement(MY_POJO),
                 bindingScope = FieldProcessor.BindingScope.READ_FROM_CURSOR,
                 parent = null).process()
             assertThat(pojo.fields.find { it.name == "foo" }, notNullValue())
@@ -1456,7 +1457,7 @@
                 }
                 """.toJFO(MY_POJO.toString())) { invocation ->
             val pojo = PojoProcessor.createFor(context = invocation.context,
-                    element = invocation.typeElement(MY_POJO.toString()),
+                    element = invocation.processingEnv.requireTypeElement(MY_POJO),
                     bindingScope = FieldProcessor.BindingScope.READ_FROM_CURSOR,
                     parent = null).process()
             assertThat(pojo.fields.find { it.name == "foo" }, notNullValue())
@@ -1478,7 +1479,7 @@
                 }
                 """.toJFO(MY_POJO.toString())) { invocation ->
             val pojo = PojoProcessor.createFor(context = invocation.context,
-                    element = invocation.typeElement(MY_POJO.toString()),
+                    element = invocation.processingEnv.requireTypeElement(MY_POJO),
                     bindingScope = FieldProcessor.BindingScope.READ_FROM_CURSOR,
                     parent = null).process()
             assertThat(pojo.fields.find { it.name == "foo" }, notNullValue())
@@ -1502,7 +1503,7 @@
                 }
                 """.toJFO(MY_POJO.toString())) { invocation ->
             PojoProcessor.createFor(context = invocation.context,
-                element = invocation.typeElement(MY_POJO.toString()),
+                element = invocation.processingEnv.requireTypeElement(MY_POJO),
                 bindingScope = FieldProcessor.BindingScope.BIND_TO_STMT,
                 parent = null).process()
         }.compilesWithoutError()
@@ -1523,7 +1524,7 @@
                 }
                 """.toJFO(MY_POJO.toString())) { invocation ->
             PojoProcessor.createFor(context = invocation.context,
-                element = invocation.typeElement(MY_POJO.toString()),
+                element = invocation.processingEnv.requireTypeElement(MY_POJO),
                 bindingScope = FieldProcessor.BindingScope.TWO_WAY,
                 parent = null).process()
         }.failsToCompile().withErrorContaining("Cannot find setter for field.")
@@ -1544,7 +1545,7 @@
                 }
                 """.toJFO(MY_POJO.toString())) { invocation ->
             PojoProcessor.createFor(context = invocation.context,
-                element = invocation.typeElement(MY_POJO.toString()),
+                element = invocation.processingEnv.requireTypeElement(MY_POJO),
                 bindingScope = FieldProcessor.BindingScope.READ_FROM_CURSOR,
                 parent = null).process()
         }.failsToCompile().withErrorContaining("Cannot find setter for field.")
@@ -1565,7 +1566,7 @@
                 }
                 """.toJFO(MY_POJO.toString())) { invocation ->
             PojoProcessor.createFor(context = invocation.context,
-                element = invocation.typeElement(MY_POJO.toString()),
+                element = invocation.processingEnv.requireTypeElement(MY_POJO),
                 bindingScope = FieldProcessor.BindingScope.BIND_TO_STMT,
                 parent = null).process()
         }.failsToCompile().withErrorContaining("Cannot find getter for field.")
@@ -1586,7 +1587,7 @@
                 }
                 """.toJFO(MY_POJO.toString())) { invocation ->
             PojoProcessor.createFor(context = invocation.context,
-                element = invocation.typeElement(MY_POJO.toString()),
+                element = invocation.processingEnv.requireTypeElement(MY_POJO),
                 bindingScope = FieldProcessor.BindingScope.TWO_WAY,
                 parent = null).process()
         }.failsToCompile().withErrorContaining("Cannot find getter for field.")
@@ -1607,7 +1608,7 @@
                 }
                 """.toJFO(MY_POJO.toString())) { invocation ->
             PojoProcessor.createFor(context = invocation.context,
-                element = invocation.typeElement(MY_POJO.toString()),
+                element = invocation.processingEnv.requireTypeElement(MY_POJO),
                 bindingScope = FieldProcessor.BindingScope.READ_FROM_CURSOR,
                 parent = null).process()
         }.compilesWithoutError()
@@ -1653,7 +1654,7 @@
         return simpleRun(*all, classpathFiles = classpathFiles) { invocation ->
             handler.invoke(
                 PojoProcessor.createFor(context = invocation.context,
-                        element = invocation.typeElement(MY_POJO.toString()),
+                        element = invocation.processingEnv.requireTypeElement(MY_POJO),
                         bindingScope = FieldProcessor.BindingScope.TWO_WAY,
                         parent = null).process(),
                 invocation
diff --git a/room/compiler/src/test/kotlin/androidx/room/processor/QueryMethodProcessorTest.kt b/room/compiler/src/test/kotlin/androidx/room/processor/QueryMethodProcessorTest.kt
index dd82041..c3d1173 100644
--- a/room/compiler/src/test/kotlin/androidx/room/processor/QueryMethodProcessorTest.kt
+++ b/room/compiler/src/test/kotlin/androidx/room/processor/QueryMethodProcessorTest.kt
@@ -24,6 +24,7 @@
 import androidx.room.ext.LifecyclesTypeNames
 import androidx.room.ext.PagingTypeNames
 import androidx.room.ext.hasAnnotation
+import androidx.room.ext.requireTypeMirror
 import androidx.room.ext.typeName
 import androidx.room.parser.QueryType
 import androidx.room.parser.Table
@@ -372,8 +373,8 @@
                 }
                 """) { parsedQuery, invocation ->
             assertThat(parsedQuery.parameters.first().type,
-                    `is`(invocation.processingEnv.elementUtils
-                            .getTypeElement("java.lang.Integer").asType()))
+                    `is`(invocation.processingEnv
+                            .requireTypeMirror("java.lang.Integer")))
         }.compilesWithoutError()
     }
 
diff --git a/room/compiler/src/test/kotlin/androidx/room/processor/RawQueryMethodProcessorTest.kt b/room/compiler/src/test/kotlin/androidx/room/processor/RawQueryMethodProcessorTest.kt
index 652abe7..f6811fe 100644
--- a/room/compiler/src/test/kotlin/androidx/room/processor/RawQueryMethodProcessorTest.kt
+++ b/room/compiler/src/test/kotlin/androidx/room/processor/RawQueryMethodProcessorTest.kt
@@ -26,6 +26,7 @@
 import androidx.room.ext.PagingTypeNames
 import androidx.room.ext.SupportDbTypeNames
 import androidx.room.ext.hasAnnotation
+import androidx.room.ext.requireTypeElement
 import androidx.room.ext.typeName
 import androidx.room.processor.ProcessorErrors.RAW_QUERY_STRING_PARAMETER_REMOVED
 import androidx.room.testing.TestInvocation
@@ -197,7 +198,7 @@
     fun suspendUnit() {
         simpleRun { invocation ->
             val daoElement =
-                invocation.typeElement(RawQuerySuspendUnitDao::class.java.canonicalName!!)
+                invocation.processingEnv.requireTypeElement(RawQuerySuspendUnitDao::class)
             val daoFunctionElement = ElementFilter.methodsIn(daoElement.enclosedElements).first()
             RawQueryMethodProcessor(
                 baseContext = invocation.context,
diff --git a/room/compiler/src/test/kotlin/androidx/room/processor/autovalue/AutoValuePojoProcessorDelegateTest.kt b/room/compiler/src/test/kotlin/androidx/room/processor/autovalue/AutoValuePojoProcessorDelegateTest.kt
index d8446c5..92b97fd 100644
--- a/room/compiler/src/test/kotlin/androidx/room/processor/autovalue/AutoValuePojoProcessorDelegateTest.kt
+++ b/room/compiler/src/test/kotlin/androidx/room/processor/autovalue/AutoValuePojoProcessorDelegateTest.kt
@@ -16,6 +16,7 @@
 
 package androidx.room.processor.autovalue
 
+import androidx.room.ext.requireTypeElement
 import androidx.room.processor.FieldProcessor
 import androidx.room.processor.PojoProcessor
 import androidx.room.processor.ProcessorErrors
@@ -102,7 +103,7 @@
         )
         simpleRun(classpathFiles = libraryClasspath) { invocation ->
                 PojoProcessor.createFor(context = invocation.context,
-                    element = invocation.typeElement(MY_POJO.toString()),
+                    element = invocation.processingEnv.requireTypeElement(MY_POJO),
                     bindingScope = FieldProcessor.BindingScope.READ_FROM_CURSOR,
                     parent = null).process()
         }.compilesWithoutError().withWarningCount(0)
@@ -265,7 +266,7 @@
         return simpleRun(*all, classpathFiles = classpathFiles) { invocation ->
             handler.invoke(
                     PojoProcessor.createFor(context = invocation.context,
-                            element = invocation.typeElement(MY_POJO.toString()),
+                            element = invocation.processingEnv.requireTypeElement(MY_POJO),
                             bindingScope = FieldProcessor.BindingScope.READ_FROM_CURSOR,
                             parent = null).process(),
                     invocation
diff --git a/room/compiler/src/test/kotlin/androidx/room/solver/BasicColumnTypeAdaptersTest.kt b/room/compiler/src/test/kotlin/androidx/room/solver/BasicColumnTypeAdaptersTest.kt
index 4a14c7b..cbd45b4 100644
--- a/room/compiler/src/test/kotlin/androidx/room/solver/BasicColumnTypeAdaptersTest.kt
+++ b/room/compiler/src/test/kotlin/androidx/room/solver/BasicColumnTypeAdaptersTest.kt
@@ -16,6 +16,7 @@
 
 package androidx.room.solver
 
+import androidx.room.ext.requireTypeMirror
 import androidx.room.processor.Context
 import androidx.room.testing.TestInvocation
 import com.squareup.javapoet.ClassName
@@ -176,7 +177,7 @@
             return if (typeKind.isPrimitive) {
                 processingEnv.typeUtils.getPrimitiveType(typeKind)
             } else {
-                processingEnv.elementUtils.getTypeElement(qName).asType()
+                processingEnv.requireTypeMirror(qName!!)
             }
         }
 
diff --git a/room/compiler/src/test/kotlin/androidx/room/solver/TypeAdapterStoreTest.kt b/room/compiler/src/test/kotlin/androidx/room/solver/TypeAdapterStoreTest.kt
index d7145df..b5e1854 100644
--- a/room/compiler/src/test/kotlin/androidx/room/solver/TypeAdapterStoreTest.kt
+++ b/room/compiler/src/test/kotlin/androidx/room/solver/TypeAdapterStoreTest.kt
@@ -29,6 +29,8 @@
 import androidx.room.ext.RxJava2TypeNames
 import androidx.room.ext.RxJava3TypeNames
 import androidx.room.ext.T
+import androidx.room.ext.requireTypeElement
+import androidx.room.ext.requireTypeMirror
 import androidx.room.ext.typeName
 import androidx.room.parser.SQLTypeAffinity
 import androidx.room.processor.Context
@@ -88,9 +90,7 @@
             val store = TypeAdapterStore.create(Context(invocation.processingEnv))
             val boolean = invocation
                     .processingEnv
-                    .elementUtils
-                    .getTypeElement("java.lang.Boolean")
-                    .asType()
+                    .requireTypeMirror("java.lang.Boolean")
             val adapter = store.findColumnTypeAdapter(boolean, null)
             assertThat(adapter, notNullValue())
             assertThat(adapter, instanceOf(CompositeAdapter::class.java))
@@ -134,8 +134,7 @@
         singleRun { invocation ->
             val store = TypeAdapterStore.create(Context(invocation.processingEnv),
                     pointTypeConverters(invocation.processingEnv))
-            val pointType = invocation.processingEnv.elementUtils
-                    .getTypeElement("foo.bar.Point").asType()
+            val pointType = invocation.processingEnv.requireTypeMirror("foo.bar.Point")
             val adapter = store.findColumnTypeAdapter(pointType, null)
             assertThat(adapter, notNullValue())
             assertThat(adapter, instanceOf(CompositeAdapter::class.java))
@@ -167,7 +166,7 @@
         singleRun { (processingEnv) ->
             val store = TypeAdapterStore.create(Context(processingEnv),
                     dateTypeConverters(processingEnv))
-            val tDate = processingEnv.elementUtils.getTypeElement("java.util.Date").asType()
+            val tDate = processingEnv.requireTypeMirror("java.util.Date")
             val adapter = store.findCursorValueReader(tDate, SQLTypeAffinity.INTEGER)
             assertThat(adapter, notNullValue())
             assertThat(adapter?.typeMirror(), `is`(tDate))
@@ -235,8 +234,8 @@
     @Test
     fun testMissingRx2Room() {
         simpleRun(jfos = *arrayOf(COMMON.PUBLISHER, COMMON.RX2_FLOWABLE)) { invocation ->
-            val publisherElement = invocation.processingEnv.elementUtils
-                    .getTypeElement(ReactiveStreamsTypeNames.PUBLISHER.toString())
+            val publisherElement = invocation.processingEnv
+                    .requireTypeElement(ReactiveStreamsTypeNames.PUBLISHER)
             assertThat(publisherElement, notNullValue())
             assertThat(
                 RxQueryResultBinderProvider.getAll(invocation.context).any {
@@ -248,8 +247,8 @@
     @Test
     fun testMissingRx3Room() {
         simpleRun(jfos = *arrayOf(COMMON.PUBLISHER, COMMON.RX3_FLOWABLE)) { invocation ->
-            val publisherElement = invocation.processingEnv.elementUtils
-                .getTypeElement(ReactiveStreamsTypeNames.PUBLISHER.toString())
+            val publisherElement = invocation.processingEnv
+                .requireTypeElement(ReactiveStreamsTypeNames.PUBLISHER)
             assertThat(publisherElement, notNullValue())
             assertThat(
                 RxQueryResultBinderProvider.getAll(invocation.context).any {
@@ -266,8 +265,8 @@
         ).forEach { (rxTypeSrc, rxRoomSrc) ->
             simpleRun(jfos = *arrayOf(COMMON.PUBLISHER, rxTypeSrc, rxRoomSrc)) {
                     invocation ->
-                val publisher = invocation.processingEnv.elementUtils
-                    .getTypeElement(ReactiveStreamsTypeNames.PUBLISHER.toString())
+                val publisher = invocation.processingEnv
+                    .requireTypeElement(ReactiveStreamsTypeNames.PUBLISHER)
                 assertThat(publisher, notNullValue())
                 assertThat(
                     RxQueryResultBinderProvider.getAll(invocation.context).any {
@@ -285,9 +284,7 @@
         ).forEach { (rxTypeSrc, rxRoomSrc, rxTypeClassName) ->
             simpleRun(jfos = *arrayOf(COMMON.PUBLISHER, rxTypeSrc, rxRoomSrc)) {
                 invocation ->
-                val flowable = invocation.processingEnv.elementUtils
-                        .getTypeElement(rxTypeClassName.toString())
-                assertThat(flowable, notNullValue())
+                val flowable = invocation.processingEnv.requireTypeElement(rxTypeClassName)
                 assertThat(
                     RxQueryResultBinderProvider.getAll(invocation.context).any {
                         it.matches(MoreTypes.asDeclared(flowable.asType()))
@@ -304,8 +301,7 @@
         ).forEach { (rxTypeSrc, rxRoomSrc, rxTypeClassName) ->
             simpleRun(jfos = *arrayOf(rxTypeSrc, rxRoomSrc)) {
                 invocation ->
-                val observable = invocation.processingEnv.elementUtils
-                        .getTypeElement(rxTypeClassName.toString())
+                val observable = invocation.processingEnv.requireTypeElement(rxTypeClassName)
                 assertThat(observable, notNullValue())
                 assertThat(
                     RxQueryResultBinderProvider.getAll(invocation.context).any {
@@ -323,8 +319,7 @@
         ).forEach { (rxTypeSrc, _, rxTypeClassName) ->
             simpleRun(jfos = *arrayOf(rxTypeSrc)) {
                 invocation ->
-                val single = invocation.processingEnv.elementUtils
-                        .getTypeElement(rxTypeClassName.toString())
+                val single = invocation.processingEnv.requireTypeElement(rxTypeClassName)
                 assertThat(single, notNullValue())
                 assertThat(
                     RxCallableInsertMethodBinderProvider.getAll(invocation.context).any {
@@ -342,9 +337,7 @@
         ).forEach { (rxTypeSrc, _, rxTypeClassName) ->
             simpleRun(jfos = *arrayOf(rxTypeSrc)) {
                 invocation ->
-                val maybe = invocation.processingEnv.elementUtils
-                        .getTypeElement(rxTypeClassName.toString())
-                assertThat(maybe, notNullValue())
+                val maybe = invocation.processingEnv.requireTypeElement(rxTypeClassName)
                 assertThat(
                     RxCallableInsertMethodBinderProvider.getAll(invocation.context).any {
                         it.matches(MoreTypes.asDeclared(maybe.asType()))
@@ -361,9 +354,7 @@
         ).forEach { (rxTypeSrc, _, rxTypeClassName) ->
             simpleRun(jfos = *arrayOf(rxTypeSrc)) {
                 invocation ->
-                val completable = invocation.processingEnv.elementUtils
-                        .getTypeElement(rxTypeClassName.toString())
-                assertThat(completable, notNullValue())
+                val completable = invocation.processingEnv.requireTypeElement(rxTypeClassName)
                 assertThat(
                     RxCallableInsertMethodBinderProvider.getAll(invocation.context).any {
                         it.matches(MoreTypes.asDeclared(completable.asType()))
@@ -376,9 +367,8 @@
     fun testFindInsertListenableFuture() {
         simpleRun(jfos = *arrayOf(COMMON.LISTENABLE_FUTURE)) {
                 invocation ->
-            val future = invocation.processingEnv.elementUtils
-                .getTypeElement(GuavaUtilConcurrentTypeNames.LISTENABLE_FUTURE.toString())
-            assertThat(future, notNullValue())
+            val future = invocation.processingEnv
+                .requireTypeElement(GuavaUtilConcurrentTypeNames.LISTENABLE_FUTURE)
             assertThat(GuavaListenableFutureInsertMethodBinderProvider(invocation.context).matches(
                 MoreTypes.asDeclared(future.asType())), `is`(true))
         }.compilesWithoutError()
@@ -388,8 +378,7 @@
     fun testFindDeleteOrUpdateSingle() {
         simpleRun(jfos = *arrayOf(COMMON.RX2_SINGLE)) {
             invocation ->
-            val single = invocation.processingEnv.elementUtils
-                    .getTypeElement(RxJava2TypeNames.SINGLE.toString())
+            val single = invocation.processingEnv.requireTypeElement(RxJava2TypeNames.SINGLE)
             assertThat(single, notNullValue())
             assertThat(
                 RxCallableDeleteOrUpdateMethodBinderProvider.getAll(invocation.context).any {
@@ -402,8 +391,7 @@
     fun testFindDeleteOrUpdateMaybe() {
         simpleRun(jfos = *arrayOf(COMMON.RX2_MAYBE)) {
             invocation ->
-            val maybe = invocation.processingEnv.elementUtils
-                    .getTypeElement(RxJava2TypeNames.MAYBE.toString())
+            val maybe = invocation.processingEnv.requireTypeElement(RxJava2TypeNames.MAYBE)
             assertThat(maybe, notNullValue())
             assertThat(
                 RxCallableDeleteOrUpdateMethodBinderProvider.getAll(invocation.context).any {
@@ -416,8 +404,8 @@
     fun testFindDeleteOrUpdateCompletable() {
         simpleRun(jfos = *arrayOf(COMMON.RX2_COMPLETABLE)) {
             invocation ->
-            val completable = invocation.processingEnv.elementUtils
-                    .getTypeElement(RxJava2TypeNames.COMPLETABLE.toString())
+            val completable = invocation.processingEnv
+                    .requireTypeElement(RxJava2TypeNames.COMPLETABLE)
             assertThat(completable, notNullValue())
             assertThat(
                 RxCallableDeleteOrUpdateMethodBinderProvider.getAll(invocation.context).any {
@@ -430,8 +418,8 @@
     fun testFindDeleteOrUpdateListenableFuture() {
         simpleRun(jfos = *arrayOf(COMMON.LISTENABLE_FUTURE)) {
                 invocation ->
-            val future = invocation.processingEnv.elementUtils
-                .getTypeElement(GuavaUtilConcurrentTypeNames.LISTENABLE_FUTURE.toString())
+            val future = invocation.processingEnv
+                .requireTypeElement(GuavaUtilConcurrentTypeNames.LISTENABLE_FUTURE)
             assertThat(future, notNullValue())
             assertThat(GuavaListenableFutureDeleteOrUpdateMethodBinderProvider(invocation.context)
                 .matches(MoreTypes.asDeclared(future.asType())), `is`(true))
@@ -442,8 +430,8 @@
     fun testFindLiveData() {
         simpleRun(jfos = *arrayOf(COMMON.COMPUTABLE_LIVE_DATA, COMMON.LIVE_DATA)) {
             invocation ->
-            val liveData = invocation.processingEnv.elementUtils
-                    .getTypeElement(LifecyclesTypeNames.LIVE_DATA.toString())
+            val liveData = invocation.processingEnv
+                    .requireTypeElement(LifecyclesTypeNames.LIVE_DATA)
             assertThat(liveData, notNullValue())
             assertThat(LiveDataQueryResultBinderProvider(invocation.context).matches(
                     MoreTypes.asDeclared(liveData.asType())), `is`(true))
@@ -453,11 +441,9 @@
     @Test
     fun findPagingSourceIntKey() {
         simpleRun { invocation ->
-            val pagingSourceElement = invocation.processingEnv.elementUtils
-                .getTypeElement(PagingSource::class.java.canonicalName)
-            val intType = invocation.processingEnv.elementUtils
-                .getTypeElement(Integer::class.java.canonicalName)
-                .asType()
+            val pagingSourceElement = invocation.processingEnv
+                .requireTypeElement(PagingSource::class)
+            val intType = invocation.processingEnv.requireTypeMirror(Integer::class)
             val pagingSourceIntIntType = invocation.processingEnv.typeUtils
                 .getDeclaredType(pagingSourceElement, intType, intType)
 
@@ -470,11 +456,9 @@
     @Test
     fun findPagingSourceStringKey() {
         simpleRun { invocation ->
-            val pagingSourceElement = invocation.processingEnv.elementUtils
-                .getTypeElement(PagingSource::class.java.canonicalName)
-            val stringType = invocation.processingEnv.elementUtils
-                .getTypeElement(String::class.java.canonicalName)
-                .asType()
+            val pagingSourceElement = invocation.processingEnv
+                .requireTypeElement(PagingSource::class)
+            val stringType = invocation.processingEnv.requireTypeMirror(String::class)
             val pagingSourceIntIntType = invocation.processingEnv.typeUtils
                 .getDeclaredType(pagingSourceElement, stringType, stringType)
 
@@ -488,8 +472,7 @@
     fun findDataSource() {
         simpleRun {
             invocation ->
-            val dataSource = invocation.processingEnv.elementUtils
-                    .getTypeElement(DataSource::class.java.canonicalName)
+            val dataSource = invocation.processingEnv.requireTypeElement(DataSource::class)
             assertThat(dataSource, notNullValue())
             assertThat(DataSourceQueryResultBinderProvider(invocation.context).matches(
                     MoreTypes.asDeclared(dataSource.asType())), `is`(true))
@@ -501,8 +484,8 @@
         simpleRun {
             invocation ->
             @Suppress("DEPRECATION")
-            val dataSource = invocation.processingEnv.elementUtils
-                    .getTypeElement(androidx.paging.PositionalDataSource::class.java.canonicalName)
+            val dataSource = invocation.processingEnv
+                .requireTypeElement(androidx.paging.PositionalDataSource::class)
             assertThat(dataSource, notNullValue())
             assertThat(DataSourceQueryResultBinderProvider(invocation.context).matches(
                     MoreTypes.asDeclared(dataSource.asType())), `is`(true))
@@ -513,8 +496,8 @@
     fun findDataSourceFactory() {
         simpleRun(jfos = *arrayOf(COMMON.DATA_SOURCE_FACTORY)) {
             invocation ->
-            val pagedListProvider = invocation.processingEnv.elementUtils
-                    .getTypeElement(PagingTypeNames.DATA_SOURCE_FACTORY.toString())
+            val pagedListProvider = invocation.processingEnv
+                    .requireTypeElement(PagingTypeNames.DATA_SOURCE_FACTORY)
             assertThat(pagedListProvider, notNullValue())
             assertThat(DataSourceFactoryQueryResultBinderProvider(invocation.context).matches(
                     MoreTypes.asDeclared(pagedListProvider.asType())), `is`(true))
@@ -522,12 +505,9 @@
     }
 
     private fun createIntListToStringBinders(invocation: TestInvocation): List<TypeConverter> {
-        val intType = invocation.processingEnv.elementUtils
-                .getTypeElement(Integer::class.java.canonicalName)
-                .asType()
-        val listType = invocation.processingEnv.elementUtils
-                .getTypeElement(java.util.List::class.java.canonicalName)
-        val listOfInts = invocation.processingEnv.typeUtils.getDeclaredType(listType, intType)
+        val intType = invocation.processingEnv.requireTypeMirror(Integer::class)
+        val listElement = invocation.processingEnv.requireTypeElement(java.util.List::class)
+        val listOfInts = invocation.processingEnv.typeUtils.getDeclaredType(listElement, intType)
 
         val intListConverter = object : TypeConverter(listOfInts,
                 invocation.context.COMMON_TYPES.STRING) {
@@ -598,7 +578,7 @@
     }
 
     fun pointTypeConverters(env: ProcessingEnvironment): List<TypeConverter> {
-        val tPoint = env.elementUtils.getTypeElement("foo.bar.Point").asType()
+        val tPoint = env.requireTypeMirror("foo.bar.Point")
         val tBoolean = env.typeUtils.getPrimitiveType(TypeKind.BOOLEAN)
         return listOf(
                 object : TypeConverter(tPoint, tBoolean) {
@@ -628,8 +608,8 @@
     }
 
     fun dateTypeConverters(env: ProcessingEnvironment): List<TypeConverter> {
-        val tDate = env.elementUtils.getTypeElement("java.util.Date").asType()
-        val tLong = env.elementUtils.getTypeElement("java.lang.Long").asType()
+        val tDate = env.requireTypeMirror("java.util.Date")
+        val tLong = env.requireTypeMirror("java.lang.Long")
         return listOf(
                 object : TypeConverter(tDate, tLong) {
                     override fun convert(
diff --git a/room/compiler/src/test/kotlin/androidx/room/solver/TypeAssignmentTest.kt b/room/compiler/src/test/kotlin/androidx/room/solver/TypeAssignmentTest.kt
index 720c751..8596df1 100644
--- a/room/compiler/src/test/kotlin/androidx/room/solver/TypeAssignmentTest.kt
+++ b/room/compiler/src/test/kotlin/androidx/room/solver/TypeAssignmentTest.kt
@@ -18,6 +18,7 @@
 
 import androidx.room.ext.getAllFieldsIncludingPrivateSupers
 import androidx.room.ext.isAssignableFromWithoutVariance
+import androidx.room.ext.requireTypeElement
 import androidx.room.testing.TestInvocation
 import com.google.testing.compile.JavaFileObjects
 import org.hamcrest.CoreMatchers.`is`
@@ -51,7 +52,7 @@
     @Test
     fun basic() {
         runTest {
-            val testObject = typeElement("foo.bar.MyObject")
+            val testObject = processingEnv.requireTypeElement("foo.bar.MyObject")
             val string = testObject.getField(processingEnv, "mString")
             val integer = testObject.getField(processingEnv, "mInteger")
             assertThat( integer.asType()
@@ -63,7 +64,7 @@
     @Test
     fun generics() {
         runTest {
-            val testObject = typeElement("foo.bar.MyObject")
+            val testObject = processingEnv.requireTypeElement("foo.bar.MyObject")
             val set = testObject.getField(processingEnv, "mSet").asType()
             val hashSet = testObject.getField(processingEnv, "mHashSet").asType()
             assertThat(hashSet.isAssignableFromWithoutVariance(typeUtils, set), `is`(false))
@@ -80,7 +81,7 @@
          *                       // to accept it
          */
         runTest {
-            val testObject = typeElement("foo.bar.MyObject")
+            val testObject = processingEnv.requireTypeElement("foo.bar.MyObject")
             val set = testObject.getField(processingEnv, "mSet").asType()
             val varianceSet = testObject.getField(processingEnv, "mVarianceSet").asType()
             assertThat(varianceSet.isAssignableFromWithoutVariance(typeUtils, set), `is`(true))
@@ -91,7 +92,7 @@
     @Test
     fun unboundedVariance() {
         runTest {
-            val testObject = typeElement("foo.bar.MyObject")
+            val testObject = processingEnv.requireTypeElement("foo.bar.MyObject")
             val unbounded = testObject.getField(processingEnv, "mUnboundedMap").asType()
             val objectMap = testObject.getField(processingEnv, "mStringMap").asType()
             assertThat(objectMap.isAssignableFromWithoutVariance(typeUtils, unbounded), `is`(false))
diff --git a/room/compiler/src/test/kotlin/androidx/room/testing/TestInvocation.kt b/room/compiler/src/test/kotlin/androidx/room/testing/TestInvocation.kt
index aeb9346..79acd1d 100644
--- a/room/compiler/src/test/kotlin/androidx/room/testing/TestInvocation.kt
+++ b/room/compiler/src/test/kotlin/androidx/room/testing/TestInvocation.kt
@@ -28,9 +28,5 @@
 ) {
     val context = Context(processingEnv)
 
-    fun typeElement(qName: String): TypeElement {
-        return processingEnv.elementUtils.getTypeElement(qName)
-    }
-
     val typeUtils by lazy { processingEnv.typeUtils }
 }
diff --git a/room/compiler/src/test/kotlin/androidx/room/verifier/DatabaseVerifierTest.kt b/room/compiler/src/test/kotlin/androidx/room/verifier/DatabaseVerifierTest.kt
index 0c5ab32..5a2fb8b 100644
--- a/room/compiler/src/test/kotlin/androidx/room/verifier/DatabaseVerifierTest.kt
+++ b/room/compiler/src/test/kotlin/androidx/room/verifier/DatabaseVerifierTest.kt
@@ -16,6 +16,7 @@
 
 package androidx.room.verifier
 
+import androidx.room.ext.requireTypeElement
 import androidx.room.parser.Collate
 import androidx.room.parser.SQLTypeAffinity
 import androidx.room.parser.SqlParser
@@ -289,7 +290,7 @@
         tableName: String,
         vararg fields: Field
     ): Entity {
-        val element = invocation.typeElement("Dummy")
+        val element = invocation.processingEnv.requireTypeElement("Dummy")
         return Entity(
             element = element,
             tableName = tableName,
diff --git a/room/compiler/src/test/kotlin/androidx/room/writer/DaoWriterTest.kt b/room/compiler/src/test/kotlin/androidx/room/writer/DaoWriterTest.kt
index 7d247b5..8071098 100644
--- a/room/compiler/src/test/kotlin/androidx/room/writer/DaoWriterTest.kt
+++ b/room/compiler/src/test/kotlin/androidx/room/writer/DaoWriterTest.kt
@@ -18,6 +18,7 @@
 
 import COMMON
 import androidx.room.ext.RoomTypeNames
+import androidx.room.ext.requireTypeElement
 import androidx.room.processor.DaoProcessor
 import androidx.room.testing.TestProcessor
 import com.google.auto.common.MoreElements
@@ -90,14 +91,9 @@
                                     .getElementsAnnotatedWith(
                                             androidx.room.Database::class.java)
                                     .firstOrNull()
-                                    ?: invocation.context.processingEnv.elementUtils
-                                        .getTypeElement(RoomTypeNames.ROOM_DB.toString())
-                            val dbType = if (db != null) {
-                                db.asType()
-                            } else {
-                                invocation.context.processingEnv.elementUtils
-                                    .getTypeElement(RoomTypeNames.ROOM_DB.toString()).asType()
-                            }.let { MoreTypes.asDeclared(it) }
+                                    ?: invocation.context.processingEnv
+                                        .requireTypeElement(RoomTypeNames.ROOM_DB)
+                            val dbType = MoreTypes.asDeclared(db.asType())
                             val parser = DaoProcessor(
                                     baseContext = invocation.context,
                                     element = MoreElements.asType(dao),