1 /* 2 * Copyright (C) 2011 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17 package com.google.inject.spi; 18 19 import com.google.inject.Binder; 20 import com.google.inject.Binding; 21 import com.google.inject.Injector; 22 import com.google.inject.internal.util.StackTraceElements; 23 import java.lang.reflect.Member; 24 25 /** 26 * A combination of a {@link Dependency} and the {@link Binding#getSource() source} where the 27 * dependency was bound. 28 * 29 * @author sameb@google.com (Sam Berlin) 30 * @since 4.0 31 * @deprecated The only use of this object is for {@link 32 * ProvisionListener.ProvisionInvocation#getDependencyChain()} which is also deprecated. This 33 * object will also be removed in Guice 4.4. 34 */ 35 @Deprecated 36 public final class DependencyAndSource { 37 private final Dependency<?> dependency; 38 private final Object source; 39 DependencyAndSource(Dependency<?> dependency, Object source)40 public DependencyAndSource(Dependency<?> dependency, Object source) { 41 this.dependency = dependency; 42 this.source = source; 43 } 44 45 /** 46 * Returns the Dependency, if one exists. For anything that can be referenced by {@link 47 * Injector#getBinding}, a dependency exists. A dependency will not exist (and this will return 48 * null) for types initialized with {@link Binder#requestInjection} or {@link 49 * Injector#injectMembers(Object)}, nor will it exist for objects injected into Providers bound 50 * with LinkedBindingBuilder#toProvider(Provider). 51 */ getDependency()52 public Dependency<?> getDependency() { 53 return dependency; 54 } 55 56 /** 57 * Returns a string describing where this dependency was bound. If the binding was just-in-time, 58 * there is no valid binding source, so this describes the class in question. 59 */ getBindingSource()60 public String getBindingSource() { 61 if (source instanceof Class) { 62 return StackTraceElements.forType((Class) source).toString(); 63 } else if (source instanceof Member) { 64 return StackTraceElements.forMember((Member) source).toString(); 65 } else { 66 return source.toString(); 67 } 68 } 69 70 @Override toString()71 public String toString() { 72 Dependency<?> dep = getDependency(); 73 Object source = getBindingSource(); 74 if (dep != null) { 75 return "Dependency: " + dep + ", source: " + source; 76 } else { 77 return "Source: " + source; 78 } 79 } 80 } 81