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 34 import java.util.Collection; 35 import java.util.Set; 36 37 /** 38 * {@link com.google.inject.spi.BindingTargetVisitor} that returns a 39 * {@link Collection} of the {@link Key}s of each {@link Binding}'s 40 * dependencies. Used by {@link InjectorGrapher} to walk the dependency graph 41 * from a starting set of {@link Binding}s. 42 * 43 * @author phopkins@gmail.com (Pete Hopkins) 44 */ 45 public class TransitiveDependencyVisitor 46 extends DefaultBindingTargetVisitor<Object, Collection<Key<?>>> { 47 visitHasDependencies(HasDependencies hasDependencies)48 private Collection<Key<?>> visitHasDependencies(HasDependencies hasDependencies) { 49 Set<Key<?>> dependencies = Sets.newHashSet(); 50 51 for (Dependency<?> dependency : hasDependencies.getDependencies()) { 52 dependencies.add(dependency.getKey()); 53 } 54 55 return dependencies; 56 } 57 visit(ConstructorBinding<?> binding)58 @Override public Collection<Key<?>> visit(ConstructorBinding<?> binding) { 59 return visitHasDependencies(binding); 60 } 61 visit(ConvertedConstantBinding<?> binding)62 @Override public Collection<Key<?>> visit(ConvertedConstantBinding<?> binding) { 63 return visitHasDependencies(binding); 64 } 65 visit(InstanceBinding<?> binding)66 @Override public Collection<Key<?>> visit(InstanceBinding<?> binding) { 67 return visitHasDependencies(binding); 68 } 69 visit(LinkedKeyBinding<?> binding)70 @Override public Collection<Key<?>> visit(LinkedKeyBinding<?> binding) { 71 return ImmutableSet.<Key<?>>of(binding.getLinkedKey()); 72 } 73 visit(ProviderBinding<?> binding)74 @Override public Collection<Key<?>> visit(ProviderBinding<?> binding) { 75 return ImmutableSet.<Key<?>>of(binding.getProvidedKey()); 76 } 77 visit(ProviderInstanceBinding<?> binding)78 @Override public Collection<Key<?>> visit(ProviderInstanceBinding<?> binding) { 79 return visitHasDependencies(binding); 80 } 81 visit(ProviderKeyBinding<?> binding)82 @Override public Collection<Key<?>> visit(ProviderKeyBinding<?> binding) { 83 return ImmutableSet.<Key<?>>of(binding.getProviderKey()); 84 } 85 86 /** @since 4.0 */ visitOther(Binding<?> binding)87 @Override public Collection<Key<?>> visitOther(Binding<?> binding) { 88 return ImmutableSet.of(); 89 } 90 } 91