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