15d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin/*
25d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin * Copyright (C) 2015 Google, Inc.
35d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin *
45d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin * Licensed under the Apache License, Version 2.0 (the "License");
55d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin * you may not use this file except in compliance with the License.
65d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin * You may obtain a copy of the License at
75d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin *
85d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin * http://www.apache.org/licenses/LICENSE-2.0
95d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin *
105d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin * Unless required by applicable law or agreed to in writing, software
115d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin * distributed under the License is distributed on an "AS IS" BASIS,
125d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin * See the License for the specific language governing permissions and
145d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin * limitations under the License.
155d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin */
165d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffinpackage dagger.internal.codegen;
175d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin
185d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffinimport com.google.common.collect.ImmutableList;
195d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffinimport java.util.ArrayDeque;
205d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffinimport java.util.Deque;
215d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin
225d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin/**
235d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin * Utility code that looks for bindings matching a key in all subcomponents in a binding graph so
245d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin * that a user is advised that a binding exists elsewhere when it is not found in the current
255d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin * subgraph. If a binding matching a key exists in a sub- or sibling component, that is often what
265d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin * the user actually wants to use.
275d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin */
285d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffinclass MissingBindingSuggestions {
295d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin  /**
305d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin   * Searches the entire binding graph from the top-level graph for a binding matching
315d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin   * {@code key}.
325d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin   */
335d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin  static ImmutableList<String> forKey(BindingGraph topLevelGraph, BindingKey key) {
345d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin    ImmutableList.Builder<String> resolutions = new ImmutableList.Builder<>();
355d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin    Deque<BindingGraph> graphsToTry = new ArrayDeque<>();
365d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin
375d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin    graphsToTry.add(topLevelGraph);
385d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin    do {
395d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin      BindingGraph graph = graphsToTry.removeLast();
405d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin      ResolvedBindings bindings = graph.resolvedBindings().get(key);
415d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin      if ((bindings == null) || bindings.bindings().isEmpty()) {
425d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin        graphsToTry.addAll(graph.subgraphs().values());
435d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin      } else {
445d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin        resolutions.add("A binding with matching key exists in component: "
455d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin            + graph.componentDescriptor().componentDefinitionType().getQualifiedName());
465d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin      }
475d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin    } while (!graphsToTry.isEmpty());
485d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin
495d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin    return resolutions.build();
505d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin  }
515d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin
525d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin  private MissingBindingSuggestions() {}
535d3207ac2713386ed61c6ca8f0356e8f093a62e1Paul Duffin}
54