1919import static com .google .common .base .Verify .verify ;
2020import static dagger .internal .codegen .binding .BindingRequest .bindingRequest ;
2121import static dagger .internal .codegen .extension .DaggerGraphs .unreachableNodes ;
22- import static dagger .internal .codegen .extension .DaggerStreams .toImmutableList ;
2322import static dagger .internal .codegen .model .BindingKind .SUBCOMPONENT_CREATOR ;
2423
2524import androidx .room .compiler .processing .XMethodElement ;
2928import com .google .auto .value .extension .memoized .Memoized ;
3029import com .google .common .collect .ImmutableList ;
3130import com .google .common .collect .ImmutableSet ;
32- import com .google .common .collect .Iterables ;
3331import com .google .common .collect .Iterators ;
3432import com .google .common .graph .ImmutableNetwork ;
3533import com .google .common .graph .MutableNetwork ;
36- import com .google .common .graph .Network ;
3734import com .google .common .graph .NetworkBuilder ;
3835import dagger .internal .codegen .binding .BindingGraph .TopLevelBindingGraph ;
3936import dagger .internal .codegen .binding .ComponentDescriptor .ComponentMethodDescriptor ;
@@ -70,7 +67,7 @@ final class BindingGraphConverter {
7067 */
7168 BindingGraph convert (LegacyBindingGraph legacyBindingGraph , boolean isFullBindingGraph ) {
7269 MutableNetwork <Node , Edge > network = asNetwork (legacyBindingGraph );
73- ComponentNode rootNode = rootComponentNode ( network );
70+ ComponentNode rootNode = legacyBindingGraph . componentNode ( );
7471
7572 // When bindings are copied down into child graphs because they transitively depend on local
7673 // multibindings or optional bindings, the parent-owned binding is still there. If that
@@ -92,47 +89,19 @@ private MutableNetwork<Node, Edge> asNetwork(LegacyBindingGraph graph) {
9289 return converter .network ;
9390 }
9491
95- // TODO(dpb): Example of BindingGraph logic applied to derived networks.
96- private ComponentNode rootComponentNode (Network <Node , Edge > network ) {
97- return (ComponentNode )
98- Iterables .find (
99- network .nodes (),
100- node -> node instanceof ComponentNode && node .componentPath ().atRoot ());
101- }
102-
103- /**
104- * Used as a cache key to make sure resolved bindings are cached per component path.
105- * This is required so that binding nodes are not reused across different branches of the
106- * graph since the ResolvedBindings class only contains the component and not the path.
107- */
108- @ AutoValue
109- abstract static class ResolvedBindingsWithPath {
110- abstract ResolvedBindings resolvedBindings ();
111- abstract ComponentPath componentPath ();
112-
113- static ResolvedBindingsWithPath create (
114- ResolvedBindings resolvedBindings , ComponentPath componentPath ) {
115- return new AutoValue_BindingGraphConverter_ResolvedBindingsWithPath (
116- resolvedBindings , componentPath );
117- }
118- }
119-
12092 private final class Converter {
12193 /** The path from the root graph to the currently visited graph. */
12294 private final Deque <LegacyBindingGraph > bindingGraphPath = new ArrayDeque <>();
12395
124- /** The {@link ComponentPath} for each component in {@link #bindingGraphPath}. */
125- private final Deque <ComponentPath > componentPaths = new ArrayDeque <>();
126-
12796 private final MutableNetwork <Node , Edge > network =
12897 NetworkBuilder .directed ().allowsParallelEdges (true ).allowsSelfLoops (true ).build ();
12998 private final Set <BindingNode > bindings = new HashSet <>();
13099
131- private final Map <ResolvedBindingsWithPath , ImmutableSet <BindingNode >> resolvedBindingsMap =
100+ private final Map <ResolvedBindings , ImmutableSet <BindingNode >> resolvedBindingsMap =
132101 new HashMap <>();
133102
134103 private void visitRootComponent (LegacyBindingGraph graph ) {
135- visitComponent (graph , null );
104+ visitComponent (graph );
136105 }
137106
138107 /**
@@ -145,30 +114,20 @@ private void visitRootComponent(LegacyBindingGraph graph) {
145114 * {@link #visitSubcomponentFactoryMethod(ComponentNode, ComponentNode, XMethodElement)}.
146115 * <li>For each entry point in the component, calls {@link #visitEntryPoint(ComponentNode,
147116 * DependencyRequest)}.
148- * <li>For each child component, calls {@link #visitComponent(LegacyBindingGraph,
149- * ComponentNode)}, updating the traversal state.
117+ * <li>For each child component, calls {@link #visitComponent(LegacyBindingGraph)} ,
118+ * updating the traversal state.
150119 * </ol>
151120 *
152121 * @param graph the currently visited graph
153122 */
154- private void visitComponent (LegacyBindingGraph graph , ComponentNode parentComponent ) {
123+ private void visitComponent (LegacyBindingGraph graph ) {
155124 bindingGraphPath .addLast (graph );
156- ComponentPath graphPath =
157- ComponentPath .create (
158- bindingGraphPath .stream ()
159- .map (LegacyBindingGraph ::componentDescriptor )
160- .map (ComponentDescriptor ::typeElement )
161- .map (DaggerTypeElement ::from )
162- .collect (toImmutableList ()));
163- componentPaths .addLast (graphPath );
164- ComponentNode currentComponent =
165- ComponentNodeImpl .create (componentPath (), graph .componentDescriptor ());
166-
167- network .addNode (currentComponent );
125+
126+ network .addNode (graph .componentNode ());
168127
169128 for (ComponentMethodDescriptor entryPointMethod :
170129 graph .componentDescriptor ().entryPointMethods ()) {
171- visitEntryPoint (currentComponent , entryPointMethod .dependencyRequest ().get ());
130+ visitEntryPoint (graph . componentNode () , entryPointMethod .dependencyRequest ().get ());
172131 }
173132
174133 for (ResolvedBindings resolvedBindings : graph .resolvedBindings ()) {
@@ -180,7 +139,7 @@ private void visitComponent(LegacyBindingGraph graph, ComponentNode parentCompon
180139 }
181140 }
182141 if (binding .kind ().equals (SUBCOMPONENT_CREATOR )
183- && binding .componentPath ().equals (currentComponent .componentPath ())) {
142+ && binding .componentPath ().equals (graph .componentPath ())) {
184143 network .addEdge (
185144 binding ,
186145 subcomponentNode (binding .key ().type ().xprocessing (), graph ),
@@ -191,22 +150,23 @@ private void visitComponent(LegacyBindingGraph graph, ComponentNode parentCompon
191150 }
192151
193152 if (bindingGraphPath .size () > 1 ) {
194- LegacyBindingGraph parent = Iterators .get (bindingGraphPath .descendingIterator (), 1 );
195- parent
153+ LegacyBindingGraph parentGraph = Iterators .get (bindingGraphPath .descendingIterator (), 1 );
154+ parentGraph
196155 .componentDescriptor ()
197156 .getFactoryMethodForChildComponent (graph .componentDescriptor ())
198157 .ifPresent (
199158 childFactoryMethod ->
200159 visitSubcomponentFactoryMethod (
201- parentComponent , currentComponent , childFactoryMethod .methodElement ()));
160+ parentGraph .componentNode (),
161+ graph .componentNode (),
162+ childFactoryMethod .methodElement ()));
202163 }
203164
204165 for (LegacyBindingGraph child : graph .subgraphs ()) {
205- visitComponent (child , currentComponent );
166+ visitComponent (child );
206167 }
207168
208169 verify (bindingGraphPath .removeLast ().equals (graph ));
209- verify (componentPaths .removeLast ().equals (graphPath ));
210170 }
211171
212172 /**
@@ -242,17 +202,17 @@ private void visitSubcomponentFactoryMethod(
242202 * component.
243203 */
244204 private ComponentPath componentPath () {
245- return componentPaths .getLast ();
205+ return bindingGraphPath .getLast (). componentPath ();
246206 }
247207
248208 /**
249209 * Returns the subpath from the root component to the matching {@code ancestor} of the current
250210 * component.
251211 */
252212 private ComponentPath pathFromRootToAncestor (XTypeElement ancestor ) {
253- for (ComponentPath componentPath : componentPaths ) {
254- if (componentPath . currentComponent ().xprocessing ().equals (ancestor )) {
255- return componentPath ;
213+ for (LegacyBindingGraph graph : bindingGraphPath ) {
214+ if (graph . componentDescriptor ().typeElement ().equals (ancestor )) {
215+ return graph . componentPath () ;
256216 }
257217 }
258218 throw new IllegalArgumentException (
@@ -325,23 +285,18 @@ private ResolvedBindings resolvedDependencies(
325285 }
326286
327287 private ImmutableSet <BindingNode > bindingNodes (ResolvedBindings resolvedBindings ) {
328- ResolvedBindingsWithPath resolvedBindingsWithPath =
329- ResolvedBindingsWithPath .create (resolvedBindings , componentPath ());
330- return resolvedBindingsMap .computeIfAbsent (
331- resolvedBindingsWithPath , this ::uncachedBindingNodes );
288+ return resolvedBindingsMap .computeIfAbsent (resolvedBindings , this ::uncachedBindingNodes );
332289 }
333290
334- private ImmutableSet <BindingNode > uncachedBindingNodes (
335- ResolvedBindingsWithPath resolvedBindingsWithPath ) {
291+ private ImmutableSet <BindingNode > uncachedBindingNodes (ResolvedBindings resolvedBindings ) {
336292 ImmutableSet .Builder <BindingNode > bindingNodes = ImmutableSet .builder ();
337- resolvedBindingsWithPath . resolvedBindings ()
293+ resolvedBindings
338294 .allBindings ()
339295 .asMap ()
340296 .forEach (
341297 (component , bindings ) -> {
342298 for (Binding binding : bindings ) {
343- bindingNodes .add (
344- bindingNode (resolvedBindingsWithPath .resolvedBindings (), binding , component ));
299+ bindingNodes .add (bindingNode (resolvedBindings , binding , component ));
345300 }
346301 });
347302 return bindingNodes .build ();
0 commit comments