Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor BindingGraphConverter. #4087

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading