Migrate Hilt's `Components` utility class to XProcessing.
RELNOTES=n/a
PiperOrigin-RevId: 517135313
diff --git a/java/dagger/hilt/processor/internal/Components.java b/java/dagger/hilt/processor/internal/Components.java
index e8f718d..a5496be 100644
--- a/java/dagger/hilt/processor/internal/Components.java
+++ b/java/dagger/hilt/processor/internal/Components.java
@@ -16,55 +16,51 @@
package dagger.hilt.processor.internal;
-import static androidx.room.compiler.processing.compat.XConverters.getProcessingEnv;
-import static androidx.room.compiler.processing.compat.XConverters.toJavac;
+import static androidx.room.compiler.processing.XElementKt.isTypeElement;
+import static dagger.hilt.processor.internal.kotlin.KotlinMetadataUtils.getMetadataUtil;
+import static dagger.internal.codegen.extension.DaggerStreams.toImmutableList;
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet;
+import static dagger.internal.codegen.xprocessing.XElements.asTypeElement;
import androidx.room.compiler.processing.XElement;
-import com.google.auto.common.MoreElements;
+import androidx.room.compiler.processing.XTypeElement;
import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.squareup.javapoet.AnnotationSpec;
import com.squareup.javapoet.ClassName;
-import dagger.hilt.processor.internal.kotlin.KotlinMetadataUtils;
-import javax.lang.model.element.Element;
-import javax.lang.model.element.TypeElement;
-import javax.lang.model.util.Elements;
+import dagger.internal.codegen.xprocessing.XElements;
/** Helper methods for defining components and the component hierarchy. */
public final class Components {
/** Returns the {@link dagger.hilt.InstallIn} components for a given element. */
public static ImmutableSet<ClassName> getComponents(XElement element) {
- return getComponents(toJavac(getProcessingEnv(element)).getElementUtils(), toJavac(element));
- }
-
- /** Returns the {@link dagger.hilt.InstallIn} components for a given element. */
- public static ImmutableSet<ClassName> getComponents(Elements elements, Element element) {
ImmutableSet<ClassName> components;
- if (Processors.hasAnnotation(element, ClassNames.INSTALL_IN)
- || Processors.hasAnnotation(element, ClassNames.TEST_INSTALL_IN)) {
- components = getHiltInstallInComponents(elements, element);
+ if (element.hasAnnotation(ClassNames.INSTALL_IN)
+ || element.hasAnnotation(ClassNames.TEST_INSTALL_IN)) {
+ components = getHiltInstallInComponents(element);
} else {
// Check the enclosing element in case it passed in module is a companion object. This helps
// in cases where the element was arrived at by checking a binding method and moving outward.
- Element enclosing = element.getEnclosingElement();
+ XElement enclosing = element.getEnclosingElement();
if (enclosing != null
- && MoreElements.isType(enclosing)
- && MoreElements.isType(element)
- && Processors.hasAnnotation(enclosing, ClassNames.MODULE)
- && KotlinMetadataUtils.getMetadataUtil().isCompanionObjectClass(
- MoreElements.asType(element))) {
- return getComponents(elements, enclosing);
+ && isTypeElement(enclosing)
+ && isTypeElement(element)
+ && enclosing.hasAnnotation(ClassNames.MODULE)
+ && getMetadataUtil().isCompanionObjectClass(asTypeElement(element))) {
+ return getComponents(enclosing);
}
if (Processors.hasErrorTypeAnnotation(element)) {
throw new BadInputException(
- "Error annotation found on element " + element + ". Look above for compilation errors",
+ String.format(
+ "Error annotation found on element %s. Look above for compilation errors",
+ XElements.toStableString(element)),
element);
} else {
throw new BadInputException(
String.format(
"An @InstallIn annotation is required for: %s." ,
- element),
+ XElements.toStableString(element)),
element);
}
}
@@ -72,10 +68,6 @@
return components;
}
- public static AnnotationSpec getInstallInAnnotationSpec(ClassName... components) {
- return getInstallInAnnotationSpec(ImmutableSet.copyOf(components));
- }
-
public static AnnotationSpec getInstallInAnnotationSpec(ImmutableSet<ClassName> components) {
Preconditions.checkArgument(!components.isEmpty());
AnnotationSpec.Builder builder = AnnotationSpec.builder(ClassNames.INSTALL_IN);
@@ -83,36 +75,30 @@
return builder.build();
}
- private static ImmutableSet<ClassName> getHiltInstallInComponents(
- Elements elements, Element element) {
+ private static ImmutableSet<ClassName> getHiltInstallInComponents(XElement element) {
Preconditions.checkArgument(
- Processors.hasAnnotation(element, ClassNames.INSTALL_IN)
- || Processors.hasAnnotation(element, ClassNames.TEST_INSTALL_IN));
+ element.hasAnnotation(ClassNames.INSTALL_IN)
+ || element.hasAnnotation(ClassNames.TEST_INSTALL_IN));
- ImmutableSet<TypeElement> components =
- ImmutableSet.copyOf(
- Processors.hasAnnotation(element, ClassNames.INSTALL_IN)
- ? Processors.getAnnotationClassValues(
- elements,
- Processors.getAnnotationMirror(element, ClassNames.INSTALL_IN),
- "value")
- : Processors.getAnnotationClassValues(
- elements,
- Processors.getAnnotationMirror(element, ClassNames.TEST_INSTALL_IN),
- "components"));
+ ImmutableList<XTypeElement> components =
+ element.hasAnnotation(ClassNames.INSTALL_IN)
+ ? Processors.getAnnotationClassValues(
+ element.getAnnotation(ClassNames.INSTALL_IN), "value")
+ : Processors.getAnnotationClassValues(
+ element.getAnnotation(ClassNames.TEST_INSTALL_IN), "components");
- ImmutableSet<TypeElement> undefinedComponents =
+ ImmutableSet<XTypeElement> undefinedComponents =
components.stream()
- .filter(component -> !Processors.hasAnnotation(component, ClassNames.DEFINE_COMPONENT))
+ .filter(component -> !component.hasAnnotation(ClassNames.DEFINE_COMPONENT))
.collect(toImmutableSet());
ProcessorErrors.checkState(
undefinedComponents.isEmpty(),
element,
"@InstallIn, can only be used with @DefineComponent-annotated classes, but found: %s",
- undefinedComponents);
+ undefinedComponents.stream().map(XElements::toStableString).collect(toImmutableList()));
- return components.stream().map(ClassName::get).collect(toImmutableSet());
+ return components.stream().map(XTypeElement::getClassName).collect(toImmutableSet());
}
private Components() {}
diff --git a/java/dagger/hilt/processor/internal/Processors.java b/java/dagger/hilt/processor/internal/Processors.java
index 26786c8..cc7cdfe 100644
--- a/java/dagger/hilt/processor/internal/Processors.java
+++ b/java/dagger/hilt/processor/internal/Processors.java
@@ -54,6 +54,7 @@
import com.squareup.javapoet.ParameterizedTypeName;
import com.squareup.javapoet.TypeName;
import com.squareup.javapoet.TypeSpec;
+import dagger.internal.codegen.xprocessing.XAnnotations;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.util.LinkedHashSet;
@@ -185,6 +186,22 @@
return Iterables.getOnlyElement(getAnnotationClassValues(elements, annotation, key));
}
+ /** Returns a list of {@link XTypeElement}s for a class attribute on an annotation. */
+ public static ImmutableList<XTypeElement> getAnnotationClassValues(
+ XAnnotation annotation, String key) {
+ ImmutableList<XTypeElement> values = XAnnotations.getAsTypeElementList(annotation, key);
+
+ ProcessorErrors.checkState(
+ values.size() >= 1,
+ annotation.getTypeElement(),
+ "@%s, '%s' class is invalid or missing: %s",
+ annotation.getName(),
+ key,
+ XAnnotations.toStableString(annotation));
+
+ return values;
+ }
+
/** Returns a list of {@link TypeElement}s for a class attribute on an annotation. */
public static ImmutableList<TypeElement> getAnnotationClassValues(
Elements elements, AnnotationMirror annotation, String key) {
@@ -366,6 +383,11 @@
}
/** Returns true if the given element has an annotation that is an error kind. */
+ public static boolean hasErrorTypeAnnotation(XElement element) {
+ return hasErrorTypeAnnotation(toJavac(element));
+ }
+
+ /** Returns true if the given element has an annotation that is an error kind. */
public static boolean hasErrorTypeAnnotation(Element element) {
for (AnnotationMirror annotationMirror : element.getAnnotationMirrors()) {
if (annotationMirror.getAnnotationType().getKind() == TypeKind.ERROR) {
diff --git a/java/dagger/hilt/processor/internal/aggregateddeps/AggregatedDepsProcessor.java b/java/dagger/hilt/processor/internal/aggregateddeps/AggregatedDepsProcessor.java
index ff74b08..768f09c 100644
--- a/java/dagger/hilt/processor/internal/aggregateddeps/AggregatedDepsProcessor.java
+++ b/java/dagger/hilt/processor/internal/aggregateddeps/AggregatedDepsProcessor.java
@@ -17,6 +17,7 @@
package dagger.hilt.processor.internal.aggregateddeps;
import static androidx.room.compiler.processing.compat.XConverters.toJavac;
+import static androidx.room.compiler.processing.compat.XConverters.toXProcessing;
import static com.google.auto.common.AnnotationMirrors.getAnnotationValue;
import static com.google.auto.common.MoreElements.asType;
import static com.google.auto.common.MoreElements.getPackage;
@@ -317,7 +318,8 @@
TypeElement entryPoint = asType(element);
if (entryPointAnnotation.equals(ClassNames.EARLY_ENTRY_POINT)) {
- ImmutableSet<ClassName> components = Components.getComponents(getElementUtils(), element);
+ ImmutableSet<ClassName> components =
+ Components.getComponents(toXProcessing(element, processingEnv()));
ProcessorErrors.checkState(
components.equals(ImmutableSet.of(ClassNames.SINGLETON_COMPONENT)),
element,
@@ -354,7 +356,8 @@
ImmutableSet<ClassName> replacedModules)
throws Exception {
// Get @InstallIn components here to catch errors before skipping user's pkg-private element.
- ImmutableSet<ClassName> components = Components.getComponents(getElementUtils(), element);
+ ImmutableSet<ClassName> components =
+ Components.getComponents(toXProcessing(element, processingEnv()));
if (isValidKind(element)) {
Optional<PkgPrivateMetadata> pkgPrivateMetadata =
diff --git a/java/dagger/hilt/processor/internal/kotlin/KotlinMetadataUtil.java b/java/dagger/hilt/processor/internal/kotlin/KotlinMetadataUtil.java
index 6fc0761..3d47274 100644
--- a/java/dagger/hilt/processor/internal/kotlin/KotlinMetadataUtil.java
+++ b/java/dagger/hilt/processor/internal/kotlin/KotlinMetadataUtil.java
@@ -151,6 +151,11 @@
}
/* Returns {@code true} if this type element is a Kotlin Companion Object. */
+ public boolean isCompanionObjectClass(XTypeElement typeElement) {
+ return isCompanionObjectClass(toJavac(typeElement));
+ }
+
+ /* Returns {@code true} if this type element is a Kotlin Companion Object. */
public boolean isCompanionObjectClass(TypeElement typeElement) {
return hasMetadata(typeElement)
&& metadataFactory.create(typeElement).classMetadata().flags(IS_COMPANION_OBJECT);
diff --git a/javatests/dagger/hilt/android/processor/internal/aggregateddeps/TestInstallInTest.java b/javatests/dagger/hilt/android/processor/internal/aggregateddeps/TestInstallInTest.java
index e4e9f67..c109564 100644
--- a/javatests/dagger/hilt/android/processor/internal/aggregateddeps/TestInstallInTest.java
+++ b/javatests/dagger/hilt/android/processor/internal/aggregateddeps/TestInstallInTest.java
@@ -86,7 +86,7 @@
.hadErrorContaining(
"@TestInstallIn, 'components' class is invalid or missing: "
+ "@dagger.hilt.testing.TestInstallIn("
- + "components={}, replaces={test.InstallInModule.class})");
+ + "components={}, replaces={test.InstallInModule})");
}
@Test