1 /*
2  * Copyright (C) 2008 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.inject.grapher;
18 
19 import com.google.common.collect.ImmutableSet;
20 import com.google.common.collect.Sets;
21 import com.google.inject.Binding;
22 import com.google.inject.Key;
23 import com.google.inject.spi.ConstructorBinding;
24 import com.google.inject.spi.ConvertedConstantBinding;
25 import com.google.inject.spi.DefaultBindingTargetVisitor;
26 import com.google.inject.spi.Dependency;
27 import com.google.inject.spi.HasDependencies;
28 import com.google.inject.spi.InstanceBinding;
29 import com.google.inject.spi.LinkedKeyBinding;
30 import com.google.inject.spi.ProviderBinding;
31 import com.google.inject.spi.ProviderInstanceBinding;
32 import com.google.inject.spi.ProviderKeyBinding;
33 import java.util.Collection;
34 import java.util.Set;
35 
36 /**
37  * {@link com.google.inject.spi.BindingTargetVisitor} that returns a {@link Collection} of the
38  * {@link Key}s of each {@link Binding}'s dependencies. Used by {@link InjectorGrapher} to walk the
39  * dependency graph from a starting set of {@link Binding}s.
40  *
41  * @author phopkins@gmail.com (Pete Hopkins)
42  */
43 public class TransitiveDependencyVisitor
44     extends DefaultBindingTargetVisitor<Object, Collection<Key<?>>> {
45 
visitHasDependencies(HasDependencies hasDependencies)46   private Collection<Key<?>> visitHasDependencies(HasDependencies hasDependencies) {
47     Set<Key<?>> dependencies = Sets.newHashSet();
48 
49     for (Dependency<?> dependency : hasDependencies.getDependencies()) {
50       dependencies.add(dependency.getKey());
51     }
52 
53     return dependencies;
54   }
55 
56   @Override
visit(ConstructorBinding<?> binding)57   public Collection<Key<?>> visit(ConstructorBinding<?> binding) {
58     return visitHasDependencies(binding);
59   }
60 
61   @Override
visit(ConvertedConstantBinding<?> binding)62   public Collection<Key<?>> visit(ConvertedConstantBinding<?> binding) {
63     return visitHasDependencies(binding);
64   }
65 
66   @Override
visit(InstanceBinding<?> binding)67   public Collection<Key<?>> visit(InstanceBinding<?> binding) {
68     return visitHasDependencies(binding);
69   }
70 
71   @Override
visit(LinkedKeyBinding<?> binding)72   public Collection<Key<?>> visit(LinkedKeyBinding<?> binding) {
73     return ImmutableSet.<Key<?>>of(binding.getLinkedKey());
74   }
75 
76   @Override
visit(ProviderBinding<?> binding)77   public Collection<Key<?>> visit(ProviderBinding<?> binding) {
78     return ImmutableSet.<Key<?>>of(binding.getProvidedKey());
79   }
80 
81   @Override
visit(ProviderInstanceBinding<?> binding)82   public Collection<Key<?>> visit(ProviderInstanceBinding<?> binding) {
83     return visitHasDependencies(binding);
84   }
85 
86   @Override
visit(ProviderKeyBinding<?> binding)87   public Collection<Key<?>> visit(ProviderKeyBinding<?> binding) {
88     return ImmutableSet.<Key<?>>of(binding.getProviderKey());
89   }
90 
91   /** @since 4.0 */
92   @Override
visitOther(Binding<?> binding)93   public Collection<Key<?>> visitOther(Binding<?> binding) {
94     return ImmutableSet.of();
95   }
96 }
97