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;
18 
19 import com.google.inject.spi.BindingScopingVisitor;
20 import com.google.inject.spi.BindingTargetVisitor;
21 import com.google.inject.spi.Element;
22 
23 /**
24  * A mapping from a key (type and optional annotation) to the strategy for getting instances of the
25  * type. This interface is part of the introspection API and is intended primarily for use by tools.
26  *
27  * <p>Bindings are created in several ways:
28  *
29  * <ul>
30  * <li>Explicitly in a module, via {@code bind()} and {@code bindConstant()} statements:
31  *     <pre>
32  *     bind(Service.class).annotatedWith(Red.class).to(ServiceImpl.class);
33  *     bindConstant().annotatedWith(ServerHost.class).to(args[0]);</pre>
34  *
35  * <li>Implicitly by the Injector by following a type's {@link ImplementedBy pointer} {@link
36  *     ProvidedBy annotations} or by using its {@link Inject annotated} or default constructor.
37  * <li>By converting a bound instance to a different type.
38  * <li>For {@link Provider providers}, by delegating to the binding for the provided type.
39  * </ul>
40  *
41  * <p>They exist on both modules and on injectors, and their behaviour is different for each:
42  *
43  * <ul>
44  * <li><strong>Module bindings</strong> are incomplete and cannot be used to provide instances. This
45  *     is because the applicable scopes and interceptors may not be known until an injector is
46  *     created. From a tool's perspective, module bindings are like the injector's source code. They
47  *     can be inspected or rewritten, but this analysis must be done statically.
48  * <li><strong>Injector bindings</strong> are complete and valid and can be used to provide
49  *     instances. From a tools' perspective, injector bindings are like reflection for an injector.
50  *     They have full runtime information, including the complete graph of injections necessary to
51  *     satisfy a binding.
52  * </ul>
53  *
54  * @param <T> the bound type. The injected is always assignable to this type.
55  * @author crazybob@google.com (Bob Lee)
56  * @author jessewilson@google.com (Jesse Wilson)
57  */
58 public interface Binding<T> extends Element {
59 
60   /** Returns the key for this binding. */
getKey()61   Key<T> getKey();
62 
63   /**
64    * Returns the scoped provider guice uses to fulfill requests for this binding.
65    *
66    * @throws UnsupportedOperationException when invoked on a {@link Binding} created via {@link
67    *     com.google.inject.spi.Elements#getElements}. This method is only supported on {@link
68    *     Binding}s returned from an injector.
69    */
getProvider()70   Provider<T> getProvider();
71 
72   /**
73    * Accepts a target visitor. Invokes the visitor method specific to this binding's target.
74    *
75    * @param visitor to call back on
76    * @since 2.0
77    */
acceptTargetVisitor(BindingTargetVisitor<? super T, V> visitor)78   <V> V acceptTargetVisitor(BindingTargetVisitor<? super T, V> visitor);
79 
80   /**
81    * Accepts a scoping visitor. Invokes the visitor method specific to this binding's scoping.
82    *
83    * @param visitor to call back on
84    * @since 2.0
85    */
acceptScopingVisitor(BindingScopingVisitor<V> visitor)86   <V> V acceptScopingVisitor(BindingScopingVisitor<V> visitor);
87 }
88