Skip to content

Commit

Permalink
Migrate ComponentDescriptor#typeElement() to XProcessing.
Browse files Browse the repository at this point in the history
For now, this means that a lot of usages need to be wrapped in toJavac(), but those will be cleaned up in future CLs.

RELNOTES=N/A
PiperOrigin-RevId: 405435970
  • Loading branch information
bcorso authored and Dagger Team committed Oct 25, 2021
1 parent c926820 commit d3e0cbb
Show file tree
Hide file tree
Showing 16 changed files with 123 additions and 78 deletions.
11 changes: 2 additions & 9 deletions java/dagger/internal/codegen/ComponentProcessingStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
import static java.util.Collections.disjoint;

import androidx.room.compiler.processing.XMessager;
import androidx.room.compiler.processing.XProcessingEnv;
import androidx.room.compiler.processing.XTypeElement;
import androidx.room.compiler.processing.compat.XConverters;
import com.google.auto.common.BasicAnnotationProcessor.ProcessingStep;
import com.google.common.collect.ImmutableSet;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
Expand All @@ -44,7 +42,6 @@
import dagger.internal.codegen.validation.ValidationReport;
import java.util.Set;
import javax.inject.Inject;
import javax.lang.model.element.TypeElement;

/**
* A {@link ProcessingStep} that is responsible for dealing with a component or production component
Expand All @@ -59,7 +56,6 @@ final class ComponentProcessingStep extends TypeCheckingProcessingStep<XTypeElem
private final BindingGraphFactory bindingGraphFactory;
private final SourceFileGenerator<BindingGraph> componentGenerator;
private final BindingGraphValidator bindingGraphValidator;
private final XProcessingEnv processingEnv;

@Inject
ComponentProcessingStep(
Expand All @@ -70,8 +66,7 @@ final class ComponentProcessingStep extends TypeCheckingProcessingStep<XTypeElem
ComponentDescriptorFactory componentDescriptorFactory,
BindingGraphFactory bindingGraphFactory,
SourceFileGenerator<BindingGraph> componentGenerator,
BindingGraphValidator bindingGraphValidator,
XProcessingEnv processingEnv) {
BindingGraphValidator bindingGraphValidator) {
this.messager = messager;
this.componentValidator = componentValidator;
this.creatorValidator = creatorValidator;
Expand All @@ -80,7 +75,6 @@ final class ComponentProcessingStep extends TypeCheckingProcessingStep<XTypeElem
this.bindingGraphFactory = bindingGraphFactory;
this.componentGenerator = componentGenerator;
this.bindingGraphValidator = bindingGraphValidator;
this.processingEnv = processingEnv;
}

@Override
Expand Down Expand Up @@ -145,9 +139,8 @@ private boolean isComponentValid(XTypeElement component) {

@CanIgnoreReturnValue
private boolean validateFullBindingGraph(ComponentDescriptor componentDescriptor) {
TypeElement component = componentDescriptor.typeElement();
if (!bindingGraphValidator.shouldDoFullBindingGraphValidation(
XConverters.toXProcessing(component, processingEnv))) {
componentDescriptor.typeElement())) {
return true;
}
BindingGraph fullBindingGraph = bindingGraphFactory.create(componentDescriptor, true);
Expand Down
6 changes: 3 additions & 3 deletions java/dagger/internal/codegen/binding/BindingFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -363,12 +363,12 @@ private boolean multibindingRequiresProduction(
}

/** Returns a {@link dagger.spi.model.BindingKind#COMPONENT} binding for the component. */
public ProvisionBinding componentBinding(TypeElement componentDefinitionType) {
public ProvisionBinding componentBinding(XTypeElement componentDefinitionType) {
checkNotNull(componentDefinitionType);
return ProvisionBinding.builder()
.contributionType(ContributionType.UNIQUE)
.bindingElement(componentDefinitionType)
.key(keyFactory.forType(componentDefinitionType.asType()))
.bindingElement(toJavac(componentDefinitionType))
.key(keyFactory.forType(toJavac(componentDefinitionType.getType())))
.kind(COMPONENT)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@

package dagger.internal.codegen.binding;

import static androidx.room.compiler.processing.compat.XConverters.toJavac;
import static com.google.auto.common.MoreTypes.asTypeElement;
import static com.google.common.base.Verify.verify;
import static dagger.internal.codegen.binding.BindingRequest.bindingRequest;
import static dagger.internal.codegen.extension.DaggerGraphs.unreachableNodes;
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableList;
import static dagger.spi.model.BindingKind.SUBCOMPONENT_CREATOR;

import androidx.room.compiler.processing.compat.XConverters;
import com.google.auto.value.AutoValue;
import com.google.auto.value.extension.memoized.Memoized;
import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -166,6 +168,7 @@ private void visitComponent(LegacyBindingGraph graph, ComponentNode parentCompon
bindingGraphPath.stream()
.map(LegacyBindingGraph::componentDescriptor)
.map(ComponentDescriptor::typeElement)
.map(XConverters::toJavac)
.map(DaggerTypeElement::fromJava)
.collect(toImmutableList()));
componentPaths.addLast(graphPath);
Expand Down Expand Up @@ -274,7 +277,7 @@ private ComponentPath pathFromRootToAncestor(TypeElement ancestor) {
*/
private LegacyBindingGraph graphForAncestor(TypeElement ancestor) {
for (LegacyBindingGraph graph : bindingGraphPath) {
if (graph.componentDescriptor().typeElement().equals(ancestor)) {
if (toJavac(graph.componentDescriptor().typeElement()).equals(ancestor)) {
return graph;
}
}
Expand Down Expand Up @@ -380,7 +383,8 @@ private ComponentNode subcomponentNode(
ComponentDescriptor subcomponent =
graph.componentDescriptor().getChildComponentWithBuilderType(subcomponentBuilderElement);
return ComponentNodeImpl.create(
componentPath().childPath(DaggerTypeElement.fromJava(subcomponent.typeElement())),
componentPath()
.childPath(DaggerTypeElement.fromJava(toJavac(subcomponent.typeElement()))),
subcomponent);
}
}
Expand Down
9 changes: 6 additions & 3 deletions java/dagger/internal/codegen/binding/BindingGraphFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package dagger.internal.codegen.binding;

import static androidx.room.compiler.processing.compat.XConverters.toJavac;
import static com.google.auto.common.MoreTypes.asTypeElement;
import static com.google.auto.common.MoreTypes.isType;
import static com.google.auto.common.MoreTypes.isTypeOf;
Expand All @@ -35,6 +36,7 @@
import static java.util.function.Predicate.isEqual;
import static javax.lang.model.util.ElementFilter.methodsIn;

import androidx.room.compiler.processing.XTypeElement;
import com.google.auto.common.MoreTypes;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -180,7 +182,8 @@ private LegacyBindingGraph createLegacyBindingGraph(
.contains(childComponent)) {
explicitBindingsBuilder.add(
bindingFactory.subcomponentCreatorBinding(
builderEntryPoint.methodElement(), componentDescriptor.typeElement()));
builderEntryPoint.methodElement(),
toJavac(componentDescriptor.typeElement())));
}
});

Expand Down Expand Up @@ -282,7 +285,7 @@ private boolean shouldIncludeImplicitProductionModules(
* @throws TypeNotPresentException if the module has not been generated yet. This will cause the
* processor to retry in a later processing round.
*/
private ModuleDescriptor descriptorForMonitoringModule(TypeElement componentDefinitionType) {
private ModuleDescriptor descriptorForMonitoringModule(XTypeElement componentDefinitionType) {
return moduleDescriptorFactory.create(
elements.checkTypePresent(
generatedMonitoringModuleName(componentDefinitionType).toString()));
Expand Down Expand Up @@ -590,7 +593,7 @@ private TypeElement getOwningComponent(Key requestKey, ContributionBinding bindi
parentResolver.get().resolvedContributionBindings.get(requestKey);
return parentResolvedBindings.owningComponent(binding);
} else {
return componentDescriptor.typeElement();
return toJavac(componentDescriptor.typeElement());
}
}

Expand Down
70 changes: 47 additions & 23 deletions java/dagger/internal/codegen/binding/ComponentDescriptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

package dagger.internal.codegen.binding;

import static androidx.room.compiler.processing.XElementKt.isMethod;
import static androidx.room.compiler.processing.compat.XConverters.toJavac;
import static androidx.room.compiler.processing.compat.XConverters.toXProcessing;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
Expand All @@ -27,7 +29,10 @@
import static javax.lang.model.element.Modifier.ABSTRACT;
import static javax.lang.model.type.TypeKind.VOID;

import androidx.room.compiler.processing.XElement;
import androidx.room.compiler.processing.XMethodElement;
import androidx.room.compiler.processing.XProcessingEnv;
import androidx.room.compiler.processing.XTypeElement;
import com.google.auto.value.AutoValue;
import com.google.auto.value.extension.memoized.Memoized;
import com.google.common.base.Supplier;
Expand Down Expand Up @@ -71,31 +76,38 @@
public abstract class ComponentDescriptor {
/** Creates a {@link ComponentDescriptor}. */
static ComponentDescriptor create(
XProcessingEnv processingEnv,
ComponentAnnotation componentAnnotation,
TypeElement component,
XTypeElement component,
ImmutableSet<ComponentRequirement> componentDependencies,
ImmutableSet<ModuleDescriptor> transitiveModules,
ImmutableMap<ExecutableElement, ComponentRequirement> dependenciesByDependencyMethod,
ImmutableMap<XMethodElement, ComponentRequirement> dependenciesByDependencyMethod,
ImmutableSet<Scope> scopes,
ImmutableSet<ComponentDescriptor> subcomponentsFromModules,
ImmutableBiMap<ComponentMethodDescriptor, ComponentDescriptor> subcomponentsByFactoryMethod,
ImmutableBiMap<ComponentMethodDescriptor, ComponentDescriptor> subcomponentsByBuilderMethod,
ImmutableSet<ComponentMethodDescriptor> componentMethods,
Optional<ComponentCreatorDescriptor> creator) {
return new AutoValue_ComponentDescriptor(
componentAnnotation,
component,
componentDependencies,
transitiveModules,
dependenciesByDependencyMethod,
scopes,
subcomponentsFromModules,
subcomponentsByFactoryMethod,
subcomponentsByBuilderMethod,
componentMethods,
creator);
ComponentDescriptor descriptor =
new AutoValue_ComponentDescriptor(
componentAnnotation,
component,
componentDependencies,
transitiveModules,
dependenciesByDependencyMethod,
scopes,
subcomponentsFromModules,
subcomponentsByFactoryMethod,
subcomponentsByBuilderMethod,
componentMethods,
creator);
descriptor.processingEnv = processingEnv;
return descriptor;
}

// This is required temporarily during the XProcessing migration to use toXProcessing().
private XProcessingEnv processingEnv;

/** The annotation that specifies that {@link #typeElement()} is a component. */
public abstract ComponentAnnotation annotation();

Expand Down Expand Up @@ -124,7 +136,7 @@ public final boolean isRealComponent() {
* The element that defines the component. This is the element to which the {@link #annotation()}
* was applied.
*/
public abstract TypeElement typeElement();
public abstract XTypeElement typeElement();

/**
* The set of component dependencies listed in {@link Component#dependencies} or {@link
Expand Down Expand Up @@ -187,15 +199,18 @@ ImmutableSet<ComponentRequirement> requirements() {
* the enclosing type of the method; a method may be declared by a supertype of the actual
* dependency.
*/
public abstract ImmutableMap<ExecutableElement, ComponentRequirement>
public abstract ImmutableMap<XMethodElement, ComponentRequirement>
dependenciesByDependencyMethod();

/** The {@linkplain #dependencies() component dependency} that defines a method. */
public final ComponentRequirement getDependencyThatDefinesMethod(Element method) {
checkArgument(
method instanceof ExecutableElement, "method must be an executable element: %s", method);
return checkNotNull(
dependenciesByDependencyMethod().get(method), "no dependency implements %s", method);
public final ComponentRequirement getDependencyThatDefinesMethod(Element javaMethod) {
XElement method = toXProcessing(javaMethod, processingEnv);
checkArgument(isMethod(method), "method must be an executable element: %s", method);
checkState(
dependenciesByDependencyMethod().containsKey(method),
"no dependency implements %s",
method);
return dependenciesByDependencyMethod().get(method);
}

/** The scopes of the component. */
Expand Down Expand Up @@ -230,7 +245,7 @@ public final ImmutableSet<ComponentDescriptor> childComponents() {

/** Returns a map of {@link #childComponents()} indexed by {@link #typeElement()}. */
@Memoized
public ImmutableMap<TypeElement, ComponentDescriptor> childComponentsByElement() {
public ImmutableMap<XTypeElement, ComponentDescriptor> childComponentsByElement() {
return Maps.uniqueIndex(childComponents(), ComponentDescriptor::typeElement);
}

Expand Down Expand Up @@ -312,7 +327,8 @@ public final boolean hasCreator() {
*/
public final Optional<CancellationPolicy> cancellationPolicy() {
return isProduction()
? Optional.ofNullable(typeElement().getAnnotation(CancellationPolicy.class))
// TODO(bcorso): Get values from XAnnotation instead of using CancellationPolicy directly.
? Optional.ofNullable(toJavac(typeElement()).getAnnotation(CancellationPolicy.class))
: Optional.empty();
}

Expand Down Expand Up @@ -387,6 +403,14 @@ public interface Builder {
private static final ImmutableSet<String> NON_CONTRIBUTING_OBJECT_METHOD_NAMES =
ImmutableSet.of("toString", "hashCode", "clone", "getClass");

/**
* Returns {@code true} if a method could be a component entry point but not a members-injection
* method.
*/
static boolean isComponentContributionMethod(XMethodElement method) {
return isComponentContributionMethod(toJavac(method));
}

/**
* Returns {@code true} if a method could be a component entry point but not a members-injection
* method.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,14 @@ private ComponentDescriptor create(
.map(ComponentRequirement::forDependency)
.collect(toImmutableSet());

ImmutableMap.Builder<ExecutableElement, ComponentRequirement> dependenciesByDependencyMethod =
ImmutableMap.Builder<XMethodElement, ComponentRequirement> dependenciesByDependencyMethod =
ImmutableMap.builder();

for (ComponentRequirement componentDependency : componentDependencies) {
for (ExecutableElement dependencyMethod :
methodsIn(elements.getAllMembers(componentDependency.typeElement()))) {
if (isComponentContributionMethod(dependencyMethod)) {
dependenciesByDependencyMethod.put(dependencyMethod, componentDependency);
dependenciesByDependencyMethod.put(
(XMethodElement) toXProcessing(dependencyMethod, processingEnv), componentDependency);
}
}
}
Expand Down Expand Up @@ -185,8 +185,9 @@ private ComponentDescriptor create(
}

return ComponentDescriptor.create(
processingEnv,
componentAnnotation,
toJavac(typeElement),
typeElement,
componentDependencies,
transitiveModules,
dependenciesByDependencyMethod.build(),
Expand Down
4 changes: 2 additions & 2 deletions java/dagger/internal/codegen/binding/LegacyBindingGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package dagger.internal.codegen.binding;

import androidx.room.compiler.processing.XTypeElement;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
Expand All @@ -25,7 +26,6 @@
import dagger.spi.model.RequestKind;
import java.util.Collection;
import java.util.Map;
import javax.lang.model.element.TypeElement;

// TODO(bcorso): Remove the LegacyBindingGraph after we've migrated to the new BindingGraph.
/** The canonical representation of a full-resolved graph. */
Expand Down Expand Up @@ -69,7 +69,7 @@ ImmutableList<LegacyBindingGraph> subgraphs() {

private static ImmutableList<LegacyBindingGraph> checkForDuplicates(
ImmutableList<LegacyBindingGraph> graphs) {
Map<TypeElement, Collection<LegacyBindingGraph>> duplicateGraphs =
Map<XTypeElement, Collection<LegacyBindingGraph>> duplicateGraphs =
Maps.filterValues(
Multimaps.index(graphs, graph -> graph.componentDescriptor().typeElement()).asMap(),
overlapping -> overlapping.size() > 1);
Expand Down
5 changes: 3 additions & 2 deletions java/dagger/internal/codegen/binding/ResolvedBindings.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package dagger.internal.codegen.binding;

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;

Expand Down Expand Up @@ -99,7 +100,7 @@ && optionalBindingDeclarations().isEmpty()

/** All bindings for {@link #key()} that are owned by a component. */
ImmutableSet<? extends Binding> bindingsOwnedBy(ComponentDescriptor component) {
return allBindings().get(component.typeElement());
return allBindings().get(toJavac(component.typeElement()));
}

/**
Expand Down Expand Up @@ -151,7 +152,7 @@ static ResolvedBindings forMembersInjectionBinding(
return new AutoValue_ResolvedBindings(
key,
ImmutableSetMultimap.of(),
ImmutableMap.of(owningComponent.typeElement(), ownedMembersInjectionBinding),
ImmutableMap.of(toJavac(owningComponent.typeElement()), ownedMembersInjectionBinding),
ImmutableSet.of(),
ImmutableSet.of(),
ImmutableSet.of());
Expand Down
Loading

0 comments on commit d3e0cbb

Please sign in to comment.