Skip to content

Commit

Permalink
Migrate AggregatedDepsProcessor to XProcessing.
Browse files Browse the repository at this point in the history
RELNOTES=N/A
PiperOrigin-RevId: 513564693
  • Loading branch information
kuanyingchou authored and Dagger Team committed Mar 10, 2023
1 parent a8e9178 commit 4f6027d
Show file tree
Hide file tree
Showing 14 changed files with 337 additions and 321 deletions.
14 changes: 14 additions & 0 deletions java/dagger/hilt/processor/internal/AggregatedElements.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@

package dagger.hilt.processor.internal;

import static androidx.room.compiler.processing.compat.XConverters.toJavac;
import static androidx.room.compiler.processing.compat.XConverters.toXProcessing;
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet;
import static javax.lang.model.element.Modifier.PUBLIC;

import androidx.room.compiler.processing.XProcessingEnv;
import androidx.room.compiler.processing.XTypeElement;
import com.google.auto.common.MoreElements;
import com.google.common.collect.ImmutableSet;
import com.squareup.javapoet.ClassName;
import dagger.internal.codegen.extension.DaggerStreams;
import java.util.Optional;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
Expand Down Expand Up @@ -58,6 +63,7 @@ private static TypeElement unwrapProxy(TypeElement element, Elements elements) {
: element;
}

// TODO(kuanyingchou): Remove this method once all usages are migrated to XProcessing.
/** Returns all aggregated elements in the aggregating package after validating them. */
public static ImmutableSet<TypeElement> from(
String aggregatingPackage, ClassName aggregatingAnnotation, Elements elements) {
Expand Down Expand Up @@ -95,5 +101,13 @@ public static ImmutableSet<TypeElement> from(
return aggregatedElements;
}

/** Returns all aggregated elements in the aggregating package after validating them. */
public static ImmutableSet<XTypeElement> from(
String aggregatingPackage, ClassName aggregatingAnnotation, XProcessingEnv env) {
return from(aggregatingPackage, aggregatingAnnotation, toJavac(env).getElementUtils()).stream()
.map(it -> toXProcessing(it, env))
.collect(DaggerStreams.toImmutableSet());
}

private AggregatedElements() {}
}
1 change: 1 addition & 0 deletions java/dagger/hilt/processor/internal/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ java_library(
":processor_errors",
":processors",
"//java/dagger/internal/codegen/extension",
"//java/dagger/internal/codegen/xprocessing",
"//third_party/java/auto:common",
"//third_party/java/guava/collect",
"//third_party/java/javapoet",
Expand Down
41 changes: 39 additions & 2 deletions java/dagger/hilt/processor/internal/Processors.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
import static javax.lang.model.element.Modifier.STATIC;

import androidx.room.compiler.processing.XAnnotation;
import androidx.room.compiler.processing.XConstructorElement;
import androidx.room.compiler.processing.XElement;
import androidx.room.compiler.processing.XExecutableElement;
import androidx.room.compiler.processing.XProcessingEnv;
import androidx.room.compiler.processing.XTypeElement;
import com.google.auto.common.GeneratedAnnotations;
Expand Down Expand Up @@ -330,6 +332,7 @@ public String visitString(String value, Void unused) {
}, /* unused accumulator */ null);
}

// TODO(kuanyingchou): Remove this method once all usages are migrated to XProcessing.
public static TypeElement getTopLevelType(Element originalElement) {
checkNotNull(originalElement);
for (Element e = originalElement; e != null; e = e.getEnclosingElement()) {
Expand All @@ -340,6 +343,11 @@ public static TypeElement getTopLevelType(Element originalElement) {
throw new IllegalStateException("Cannot find a top-level type for " + originalElement);
}

public static XTypeElement getTopLevelType(XElement originalElement) {
return toXProcessing(
getTopLevelType(toJavac(originalElement)), getProcessingEnv(originalElement));
}

// TODO(kuanyingchou): Remove this method once all usages are migrated to XProcessing.
/** Returns true if the given element is a top-level element. */
public static boolean isTopLevel(Element element) {
Expand Down Expand Up @@ -714,9 +722,10 @@ public static MethodSpec.Builder copyMethodSpecWithoutBody(MethodSpec methodSpec
.addTypeVariables(methodSpec.typeVariables);
}

// TODO(kuanyingchou): Remove this method once all usages are migrated to XProcessing.
/**
* Returns true if the given method is annotated with one of the annotations Dagger recognizes
* for abstract methods (e.g. @Binds).
* Returns true if the given method is annotated with one of the annotations Dagger recognizes for
* abstract methods (e.g. @Binds).
*/
public static boolean hasDaggerAbstractMethodAnnotation(ExecutableElement method) {
return hasAnnotation(method, ClassNames.BINDS)
Expand All @@ -725,10 +734,22 @@ public static boolean hasDaggerAbstractMethodAnnotation(ExecutableElement method
|| hasAnnotation(method, ClassNames.CONTRIBUTES_ANDROID_INJECTOR);
}

/**
* Returns true if the given method is annotated with one of the annotations Dagger recognizes for
* abstract methods (e.g. @Binds).
*/
public static boolean hasDaggerAbstractMethodAnnotation(XExecutableElement method) {
return method.hasAnnotation(ClassNames.BINDS)
|| method.hasAnnotation(ClassNames.BINDS_OPTIONAL_OF)
|| method.hasAnnotation(ClassNames.MULTIBINDS)
|| method.hasAnnotation(ClassNames.CONTRIBUTES_ANDROID_INJECTOR);
}

public static ImmutableSet<ClassName> toClassNames(Iterable<TypeElement> elements) {
return FluentIterable.from(elements).transform(ClassName::get).toSet();
}

// TODO(kuanyingchou): Remove this method once all usages are migrated to XProcessing.
public static boolean requiresModuleInstance(Elements elements, TypeElement module) {
// Binding methods that lack ABSTRACT or STATIC require module instantiation.
// Required by Dagger. See b/31489617.
Expand All @@ -741,6 +762,11 @@ public static boolean requiresModuleInstance(Elements elements, TypeElement modu
&& !getMetadataUtil().isObjectOrCompanionObjectClass(module);
}

public static boolean requiresModuleInstance(XTypeElement module) {
return requiresModuleInstance(
toJavac(getProcessingEnv(module)).getElementUtils(), toJavac(module));
}

public static boolean hasVisibleEmptyConstructor(TypeElement type) {
List<ExecutableElement> constructors = ElementFilter.constructorsIn(type.getEnclosedElements());
return constructors.isEmpty()
Expand All @@ -752,6 +778,17 @@ public static boolean hasVisibleEmptyConstructor(TypeElement type) {
);
}

public static boolean hasVisibleEmptyConstructor(XTypeElement type) {
List<XConstructorElement> constructors = type.getConstructors();
return constructors.isEmpty()
|| constructors.stream()
.filter(constructor -> constructor.getParameters().isEmpty())
.anyMatch(
constructor ->
!constructor.isPrivate()
);
}

private static boolean isBindingMethod(ExecutableElement method) {
return hasAnnotation(method, ClassNames.PROVIDES)
|| hasAnnotation(method, ClassNames.BINDS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@

package dagger.hilt.processor.internal.aggregateddeps;

import androidx.room.compiler.processing.XTypeElement;
import com.google.common.collect.ImmutableSet;
import com.squareup.javapoet.AnnotationSpec;
import com.squareup.javapoet.ClassName;
import dagger.hilt.processor.internal.Processors;
import java.io.IOException;
import java.util.Optional;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.TypeElement;

/**
* Generates the @AggregatedDeps annotated class used to pass information
Expand All @@ -35,30 +34,27 @@ final class AggregatedDepsGenerator {
ClassName.get("dagger.hilt.processor.internal.aggregateddeps", "AggregatedDeps");

private final String dependencyType;
private final TypeElement dependency;
private final XTypeElement dependency;
private final Optional<ClassName> testName;
private final ImmutableSet<ClassName> components;
private final ImmutableSet<ClassName> replacedDependencies;
private final ProcessingEnvironment processingEnv;

AggregatedDepsGenerator(
String dependencyType,
TypeElement dependency,
XTypeElement dependency,
Optional<ClassName> testName,
ImmutableSet<ClassName> components,
ImmutableSet<ClassName> replacedDependencies,
ProcessingEnvironment processingEnv) {
ImmutableSet<ClassName> replacedDependencies) {
this.dependencyType = dependencyType;
this.dependency = dependency;
this.testName = testName;
this.components = components;
this.replacedDependencies = replacedDependencies;
this.processingEnv = processingEnv;
}

void generate() throws IOException {
Processors.generateAggregatingClass(
AGGREGATING_PACKAGE, aggregatedDepsAnnotation(), dependency, getClass(), processingEnv);
AGGREGATING_PACKAGE, aggregatedDepsAnnotation(), dependency, getClass());
}

private AnnotationSpec aggregatedDepsAnnotation() {
Expand Down

0 comments on commit 4f6027d

Please sign in to comment.