Skip to content

Commit

Permalink
Refactor BindingGraphConverter.
Browse files Browse the repository at this point in the history
This CL inlines some functions that are only used at a single call-site and are essentially 1-liners.

RELNOTES=N/A
PiperOrigin-RevId: 569594969
  • Loading branch information
bcorso authored and Dagger Team committed Oct 4, 2023
1 parent 7173b40 commit 0d03bc7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 49 deletions.
58 changes: 11 additions & 47 deletions java/dagger/internal/codegen/binding/BindingGraphConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@
import static dagger.internal.codegen.extension.DaggerGraphs.unreachableNodes;
import static dagger.internal.codegen.model.BindingKind.SUBCOMPONENT_CREATOR;

import androidx.room.compiler.processing.XMethodElement;
import androidx.room.compiler.processing.XType;
import androidx.room.compiler.processing.XTypeElement;
import com.google.auto.value.AutoValue;
import com.google.auto.value.extension.memoized.Memoized;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterators;
import com.google.common.graph.ImmutableNetwork;
import com.google.common.graph.MutableNetwork;
import com.google.common.graph.NetworkBuilder;
Expand All @@ -40,7 +38,6 @@
import dagger.internal.codegen.model.BindingGraph.MissingBinding;
import dagger.internal.codegen.model.BindingGraph.Node;
import dagger.internal.codegen.model.ComponentPath;
import dagger.internal.codegen.model.DaggerExecutableElement;
import dagger.internal.codegen.model.DaggerTypeElement;
import dagger.internal.codegen.model.DependencyRequest;
import dagger.internal.codegen.model.Key;
Expand Down Expand Up @@ -110,10 +107,9 @@ private void visitRootComponent(LegacyBindingGraph graph) {
* <p>This implementation does the following:
*
* <ol>
* <li>If this component is installed in its parent by a subcomponent factory method, calls
* {@link #visitSubcomponentFactoryMethod(ComponentNode, ComponentNode, XMethodElement)}.
* <li>For each entry point in the component, calls {@link #visitEntryPoint(ComponentNode,
* DependencyRequest)}.
* <li>If this component is installed in its parent by a subcomponent factory method, adds
* an edge between the parent and child components.
* <li>For each entry point, adds an edge between the component and the entry point.
* <li>For each child component, calls {@link #visitComponent(LegacyBindingGraph)},
* updating the traversal state.
* </ol>
Expand All @@ -127,7 +123,7 @@ private void visitComponent(LegacyBindingGraph graph) {

for (ComponentMethodDescriptor entryPointMethod :
graph.componentDescriptor().entryPointMethods()) {
visitEntryPoint(graph.componentNode(), entryPointMethod.dependencyRequest().get());
addDependencyEdges(graph.componentNode(), entryPointMethod.dependencyRequest().get());
}

for (ResolvedBindings resolvedBindings : graph.resolvedBindings()) {
Expand All @@ -149,54 +145,22 @@ private void visitComponent(LegacyBindingGraph graph) {
}
}

if (bindingGraphPath.size() > 1) {
LegacyBindingGraph parentGraph = Iterators.get(bindingGraphPath.descendingIterator(), 1);
parentGraph
for (LegacyBindingGraph childGraph : graph.subgraphs()) {
visitComponent(childGraph);
graph
.componentDescriptor()
.getFactoryMethodForChildComponent(graph.componentDescriptor())
.getFactoryMethodForChildComponent(childGraph.componentDescriptor())
.ifPresent(
childFactoryMethod ->
visitSubcomponentFactoryMethod(
parentGraph.componentNode(),
network.addEdge(
graph.componentNode(),
childFactoryMethod.methodElement()));
}

for (LegacyBindingGraph child : graph.subgraphs()) {
visitComponent(child);
childGraph.componentNode(),
new ChildFactoryMethodEdgeImpl(childFactoryMethod.methodElement())));
}

verify(bindingGraphPath.removeLast().equals(graph));
}

/**
* Called once for each entry point in a component.
*
* @param componentNode the component that contains the entry point
* @param entryPoint the entry point to visit
*/
private void visitEntryPoint(ComponentNode componentNode, DependencyRequest entryPoint) {
addDependencyEdges(componentNode, entryPoint);
}

/**
* Called if this component was installed in its parent by a subcomponent factory method.
*
* @param parentComponent the parent graph
* @param currentComponent the currently visited graph
* @param factoryMethod the factory method in the parent component that declares that the
* current component is a child
*/
private void visitSubcomponentFactoryMethod(
ComponentNode parentComponent,
ComponentNode currentComponent,
XMethodElement factoryMethod) {
network.addEdge(
parentComponent,
currentComponent,
new ChildFactoryMethodEdgeImpl(DaggerExecutableElement.from(factoryMethod)));
}

/**
* Returns an immutable snapshot of the path from the root component to the currently visited
* component.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static dagger.internal.codegen.base.ElementFormatter.elementToString;

import androidx.room.compiler.processing.XMethodElement;
import dagger.internal.codegen.model.BindingGraph.ChildFactoryMethodEdge;
import dagger.internal.codegen.model.DaggerExecutableElement;

Expand All @@ -26,8 +27,8 @@ public final class ChildFactoryMethodEdgeImpl implements ChildFactoryMethodEdge

private final DaggerExecutableElement factoryMethod;

ChildFactoryMethodEdgeImpl(DaggerExecutableElement factoryMethod) {
this.factoryMethod = factoryMethod;
ChildFactoryMethodEdgeImpl(XMethodElement factoryMethod) {
this.factoryMethod = DaggerExecutableElement.from(factoryMethod);
}

@Override
Expand Down

0 comments on commit 0d03bc7

Please sign in to comment.