• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.Binding;
20 import com.google.inject.Provider;
21 import com.google.inject.Scope;
22 
23 import java.util.List;
24 
25 /**
26  * Listens for provisioning of objects. Useful for gathering timing information
27  * about provisioning, post-provision initialization, and more.
28  *
29  * @author sameb@google.com (Sam Berlin)
30  * @since 4.0
31  */
32 public interface ProvisionListener {
33 
34   /**
35    * Invoked by Guice when an object requires provisioning. Provisioning occurs
36    * when Guice locates and injects the dependencies for a binding. For types
37    * bound to a Provider, provisioning encapsulates the {@link Provider#get}
38    * method. For toInstance or constant bindings, provisioning encapsulates
39    * the injecting of {@literal @}{@code Inject}ed fields or methods.
40    * For other types, provisioning encapsulates the construction of the
41    * object. If a type is bound within a {@link Scope}, provisioning depends on
42    * the scope. Types bound in Singleton scope will only be provisioned once.
43    * Types bound in no scope will be provisioned every time they are injected.
44    * Other scopes define their own behavior for provisioning.
45    * <p>
46    * To perform the provision, call {@link ProvisionInvocation#provision()}.
47    * If you do not explicitly call provision, it will be automatically done after
48    * this method returns.  It is an error to call provision more than once.
49    */
onProvision(ProvisionInvocation<T> provision)50   <T> void onProvision(ProvisionInvocation<T> provision);
51 
52   /**
53    * Encapsulates a single act of provisioning.
54    *
55    * @since 4.0
56    */
57   public abstract static class ProvisionInvocation<T> {
58 
59     /**
60      * Returns the Binding this is provisioning.
61      * <p>
62      * You must not call {@link Provider#get()} on the provider returned by
63      * {@link Binding#getProvider}, otherwise you will get confusing error messages.
64      */
getBinding()65     public abstract Binding<T> getBinding();
66 
67     /** Performs the provision, returning the object provisioned. */
provision()68     public abstract T provision();
69 
70     /** Returns the dependency chain that led to this object being provisioned. */
getDependencyChain()71     public abstract List<DependencyAndSource> getDependencyChain();
72 
73   }
74 }
75