Migrate DaggerElements.checkTypePresent() to XProcessing.

RELNOTES=N/A
PiperOrigin-RevId: 426949369
diff --git a/java/dagger/internal/codegen/binding/MapKeys.java b/java/dagger/internal/codegen/binding/MapKeys.java
index 1afd914..87bdcb9 100644
--- a/java/dagger/internal/codegen/binding/MapKeys.java
+++ b/java/dagger/internal/codegen/binding/MapKeys.java
@@ -27,6 +27,7 @@
 import static com.squareup.javapoet.MethodSpec.methodBuilder;
 import static dagger.internal.codegen.base.MapKeyAccessibility.isMapKeyPubliclyAccessible;
 import static dagger.internal.codegen.binding.SourceFiles.elementBasedClassName;
+import static dagger.internal.codegen.langmodel.DaggerElements.checkTypePresent;
 import static dagger.internal.codegen.xprocessing.XElements.getSimpleName;
 import static dagger.internal.codegen.xprocessing.XTypes.isDeclared;
 import static dagger.internal.codegen.xprocessing.XTypes.isPrimitive;
@@ -38,6 +39,7 @@
 import androidx.room.compiler.processing.XMethodElement;
 import androidx.room.compiler.processing.XProcessingEnv;
 import androidx.room.compiler.processing.XType;
+import androidx.room.compiler.processing.XTypeElement;
 import com.google.auto.common.MoreTypes;
 import com.google.common.collect.ImmutableSet;
 import com.squareup.javapoet.ClassName;
@@ -55,7 +57,6 @@
 import javax.lang.model.element.AnnotationMirror;
 import javax.lang.model.element.AnnotationValue;
 import javax.lang.model.element.Element;
-import javax.lang.model.element.TypeElement;
 import javax.lang.model.type.TypeMirror;
 
 /** Methods for extracting {@link MapKey} annotations and key code blocks from binding elements. */
@@ -154,11 +155,11 @@
    *     map} contribution.
    */
   public static CodeBlock getMapKeyExpression(
-      ContributionBinding binding, ClassName requestingClass, DaggerElements elements) {
+      ContributionBinding binding, ClassName requestingClass, XProcessingEnv processingEnv) {
     AnnotationMirror mapKeyAnnotation = binding.mapKeyAnnotation().get();
     return MapKeyAccessibility.isMapKeyAccessibleFrom(
             mapKeyAnnotation, requestingClass.packageName())
-        ? directMapKeyExpression(mapKeyAnnotation, elements)
+        ? directMapKeyExpression(mapKeyAnnotation, processingEnv)
         : CodeBlock.of("$T.create()", mapKeyProxyClassName(binding));
   }
 
@@ -175,19 +176,19 @@
    *     annotation
    */
   private static CodeBlock directMapKeyExpression(
-      AnnotationMirror mapKey, DaggerElements elements) {
+      AnnotationMirror mapKey, XProcessingEnv processingEnv) {
     Optional<? extends AnnotationValue> unwrappedValue = unwrapValue(mapKey);
     AnnotationExpression annotationExpression = new AnnotationExpression(mapKey);
 
     if (MoreTypes.asTypeElement(mapKey.getAnnotationType())
         .getQualifiedName()
         .contentEquals("dagger.android.AndroidInjectionKey")) {
-      TypeElement unwrappedType =
-          elements.checkTypePresent((String) unwrappedValue.get().getValue());
+      XTypeElement unwrappedType =
+          checkTypePresent(processingEnv, (String) unwrappedValue.get().getValue());
       return CodeBlock.of(
           "$T.of($S)",
           ClassName.get("dagger.android.internal", "AndroidInjectionKeys"),
-          ClassName.get(unwrappedType).reflectionName());
+          unwrappedType.getClassName().reflectionName());
     }
 
     if (unwrappedValue.isPresent()) {
@@ -213,7 +214,7 @@
    * accessible.
    */
   public static Optional<MethodSpec> mapKeyFactoryMethod(
-      ContributionBinding binding, XProcessingEnv processingEnv, DaggerElements elements) {
+      ContributionBinding binding, XProcessingEnv processingEnv) {
     return binding
         .mapKeyAnnotation()
         .filter(mapKey -> !isMapKeyPubliclyAccessible(mapKey))
@@ -222,7 +223,7 @@
                 methodBuilder("create")
                     .addModifiers(PUBLIC, STATIC)
                     .returns(TypeName.get(mapKeyType(toXProcessing(mapKey, processingEnv))))
-                    .addStatement("return $L", directMapKeyExpression(mapKey, elements))
+                    .addStatement("return $L", directMapKeyExpression(mapKey, processingEnv))
                     .build());
   }
 
diff --git a/java/dagger/internal/codegen/langmodel/DaggerElements.java b/java/dagger/internal/codegen/langmodel/DaggerElements.java
index 7e19ba9..4d479f9 100644
--- a/java/dagger/internal/codegen/langmodel/DaggerElements.java
+++ b/java/dagger/internal/codegen/langmodel/DaggerElements.java
@@ -353,19 +353,12 @@
 
   /** Returns the type element or throws {@link TypeNotPresentException} if it is null. */
   public static XTypeElement checkTypePresent(XProcessingEnv processingEnv, ClassName className) {
-    XTypeElement type = processingEnv.findTypeElement(className);
-    if (type == null) {
-      throw new TypeNotPresentException(className.canonicalName(), null);
-    }
-    return type;
+    return checkTypePresent(processingEnv, className.canonicalName());
   }
 
-  /**
-   * Invokes {@link Elements#getTypeElement(CharSequence)}, throwing {@link TypeNotPresentException}
-   * if it is not accessible in the current compilation.
-   */
-  public TypeElement checkTypePresent(String typeName) {
-    TypeElement type = elements.getTypeElement(typeName);
+  /** Returns the type element or throws {@link TypeNotPresentException} if it is null. */
+  public static XTypeElement checkTypePresent(XProcessingEnv processingEnv, String typeName) {
+    XTypeElement type = processingEnv.findTypeElement(typeName);
     if (type == null) {
       throw new TypeNotPresentException(typeName, null);
     }
diff --git a/java/dagger/internal/codegen/writing/InaccessibleMapKeyProxyGenerator.java b/java/dagger/internal/codegen/writing/InaccessibleMapKeyProxyGenerator.java
index 5687acd..f48fd36 100644
--- a/java/dagger/internal/codegen/writing/InaccessibleMapKeyProxyGenerator.java
+++ b/java/dagger/internal/codegen/writing/InaccessibleMapKeyProxyGenerator.java
@@ -41,7 +41,6 @@
 public final class InaccessibleMapKeyProxyGenerator
     extends SourceFileGenerator<ContributionBinding> {
   private final XProcessingEnv processingEnv;
-  private final DaggerElements elements;
 
   @Inject
   InaccessibleMapKeyProxyGenerator(
@@ -51,7 +50,6 @@
       SourceVersion sourceVersion) {
     super(filer, elements, sourceVersion);
     this.processingEnv = processingEnv;
-    this.elements = elements;
   }
 
   @Override
@@ -62,7 +60,7 @@
 
   @Override
   public ImmutableList<TypeSpec.Builder> topLevelTypes(ContributionBinding binding) {
-    return MapKeys.mapKeyFactoryMethod(binding, processingEnv, elements)
+    return MapKeys.mapKeyFactoryMethod(binding, processingEnv)
         .map(
             method ->
                 classBuilder(MapKeys.mapKeyProxyClassName(binding))
diff --git a/java/dagger/internal/codegen/writing/MapFactoryCreationExpression.java b/java/dagger/internal/codegen/writing/MapFactoryCreationExpression.java
index 38c9d07..a326e0a 100644
--- a/java/dagger/internal/codegen/writing/MapFactoryCreationExpression.java
+++ b/java/dagger/internal/codegen/writing/MapFactoryCreationExpression.java
@@ -21,6 +21,7 @@
 import static dagger.internal.codegen.binding.SourceFiles.mapFactoryClassName;
 import static dagger.internal.codegen.extension.DaggerCollectors.toOptional;
 
+import androidx.room.compiler.processing.XProcessingEnv;
 import androidx.room.compiler.processing.XType;
 import com.squareup.javapoet.CodeBlock;
 import dagger.assisted.Assisted;
@@ -30,30 +31,29 @@
 import dagger.internal.codegen.binding.BindingGraph;
 import dagger.internal.codegen.binding.ContributionBinding;
 import dagger.internal.codegen.javapoet.TypeNames;
-import dagger.internal.codegen.langmodel.DaggerElements;
 import dagger.spi.model.DependencyRequest;
 import java.util.stream.Stream;
 
 /** A factory creation expression for a multibound map. */
 final class MapFactoryCreationExpression extends MultibindingFactoryCreationExpression {
 
+  private final XProcessingEnv processingEnv;
   private final ComponentImplementation componentImplementation;
   private final BindingGraph graph;
   private final ContributionBinding binding;
-  private final DaggerElements elements;
 
   @AssistedInject
   MapFactoryCreationExpression(
       @Assisted ContributionBinding binding,
+      XProcessingEnv processingEnv,
       ComponentImplementation componentImplementation,
       ComponentRequestRepresentations componentRequestRepresentations,
-      BindingGraph graph,
-      DaggerElements elements) {
+      BindingGraph graph) {
     super(binding, componentImplementation, componentRequestRepresentations);
+    this.processingEnv = processingEnv;
     this.binding = checkNotNull(binding);
     this.componentImplementation = componentImplementation;
     this.graph = graph;
-    this.elements = elements;
   }
 
   @Override
@@ -78,7 +78,7 @@
       ContributionBinding contributionBinding = graph.contributionBinding(dependency.key());
       builder.add(
           ".put($L, $L)",
-          getMapKeyExpression(contributionBinding, componentImplementation.name(), elements),
+          getMapKeyExpression(contributionBinding, componentImplementation.name(), processingEnv),
           multibindingDependencyExpression(dependency));
     }
     builder.add(".build()");
diff --git a/java/dagger/internal/codegen/writing/MapRequestRepresentation.java b/java/dagger/internal/codegen/writing/MapRequestRepresentation.java
index 92ad920..876c248 100644
--- a/java/dagger/internal/codegen/writing/MapRequestRepresentation.java
+++ b/java/dagger/internal/codegen/writing/MapRequestRepresentation.java
@@ -16,16 +16,17 @@
 
 package dagger.internal.codegen.writing;
 
-import static androidx.room.compiler.processing.compat.XConverters.toJavac;
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.collect.Iterables.getOnlyElement;
 import static dagger.internal.codegen.binding.BindingRequest.bindingRequest;
 import static dagger.internal.codegen.binding.MapKeys.getMapKeyExpression;
 import static dagger.internal.codegen.javapoet.CodeBlocks.toParametersCodeBlock;
 import static dagger.internal.codegen.langmodel.Accessibility.isTypeAccessibleFrom;
+import static dagger.internal.codegen.xprocessing.XElements.getSimpleName;
 import static dagger.spi.model.BindingKind.MULTIBOUND_MAP;
-import static javax.lang.model.util.ElementFilter.methodsIn;
 
+import androidx.room.compiler.processing.XProcessingEnv;
+import androidx.room.compiler.processing.XType;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Maps;
 import com.squareup.javapoet.ClassName;
@@ -40,12 +41,9 @@
 import dagger.internal.codegen.binding.ProvisionBinding;
 import dagger.internal.codegen.javapoet.Expression;
 import dagger.internal.codegen.javapoet.TypeNames;
-import dagger.internal.codegen.langmodel.DaggerElements;
-import dagger.internal.codegen.langmodel.DaggerTypes;
 import dagger.spi.model.BindingKind;
 import dagger.spi.model.DependencyRequest;
 import java.util.Collections;
-import javax.lang.model.type.DeclaredType;
 import javax.lang.model.type.TypeMirror;
 
 /** A {@link RequestRepresentation} for multibound maps. */
@@ -53,27 +51,24 @@
   /** Maximum number of key-value pairs that can be passed to ImmutableMap.of(K, V, K, V, ...). */
   private static final int MAX_IMMUTABLE_MAP_OF_KEY_VALUE_PAIRS = 5;
 
+  private final XProcessingEnv processingEnv;
   private final ProvisionBinding binding;
   private final ImmutableMap<DependencyRequest, ContributionBinding> dependencies;
   private final ComponentRequestRepresentations componentRequestRepresentations;
-  private final DaggerTypes types;
-  private final DaggerElements elements;
   private final boolean isExperimentalMergedMode;
 
   @AssistedInject
   MapRequestRepresentation(
       @Assisted ProvisionBinding binding,
+      XProcessingEnv processingEnv,
       BindingGraph graph,
       ComponentImplementation componentImplementation,
-      ComponentRequestRepresentations componentRequestRepresentations,
-      DaggerTypes types,
-      DaggerElements elements) {
+      ComponentRequestRepresentations componentRequestRepresentations) {
     this.binding = binding;
+    this.processingEnv = processingEnv;
     BindingKind bindingKind = this.binding.kind();
     checkArgument(bindingKind.equals(MULTIBOUND_MAP), bindingKind);
     this.componentRequestRepresentations = componentRequestRepresentations;
-    this.types = types;
-    this.elements = elements;
     this.dependencies =
         Maps.toMap(binding.dependencies(), dep -> graph.contributionBinding(dep.key()));
     this.isExperimentalMergedMode =
@@ -125,23 +120,23 @@
           instantiation.add(".put($L)", keyAndValueExpression(dependency, requestingClass));
         }
         return Expression.create(
-            isImmutableMapAvailable ? immutableMapType() : binding.key().type().java(),
+            isImmutableMapAvailable ? immutableMapType() : binding.key().type().xprocessing(),
             instantiation.add(".build()").build());
     }
   }
 
-  private DeclaredType immutableMapType() {
+  private XType immutableMapType() {
     MapType mapType = MapType.from(binding.key());
-    return types.getDeclaredType(
-        elements.getTypeElement(TypeNames.IMMUTABLE_MAP),
-        toJavac(mapType.keyType()),
-        toJavac(mapType.valueType()));
+    return processingEnv.getDeclaredType(
+        processingEnv.requireTypeElement(TypeNames.IMMUTABLE_MAP),
+        mapType.keyType(),
+        mapType.valueType());
   }
 
   private CodeBlock keyAndValueExpression(DependencyRequest dependency, ClassName requestingClass) {
     return CodeBlock.of(
         "$L, $L",
-        getMapKeyExpression(dependencies.get(dependency), requestingClass, elements),
+        getMapKeyExpression(dependencies.get(dependency), requestingClass, processingEnv),
         isExperimentalMergedMode
             ? componentRequestRepresentations
                 .getExperimentalSwitchingProviderDependencyRepresentation(
@@ -174,16 +169,13 @@
   }
 
   private boolean isImmutableMapBuilderWithExpectedSizeAvailable() {
-    if (isImmutableMapAvailable()) {
-      return methodsIn(elements.getTypeElement(TypeNames.IMMUTABLE_MAP).getEnclosedElements())
-          .stream()
-          .anyMatch(method -> method.getSimpleName().contentEquals("builderWithExpectedSize"));
-    }
-    return false;
+    return isImmutableMapAvailable()
+        && processingEnv.requireTypeElement(TypeNames.IMMUTABLE_MAP).getDeclaredMethods().stream()
+            .anyMatch(method -> getSimpleName(method).contentEquals("builderWithExpectedSize"));
   }
 
   private boolean isImmutableMapAvailable() {
-    return elements.getTypeElement(TypeNames.IMMUTABLE_MAP) != null;
+    return processingEnv.findTypeElement(TypeNames.IMMUTABLE_MAP) != null;
   }
 
   @AssistedFactory