aboutsummaryrefslogtreecommitdiff
path: root/core/src/com/google/inject/spi/ProvisionListener.java
blob: 812f4e1ef800ad05094922a8a23448621ae32a9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
 * Copyright (C) 2011 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.inject.spi;

import com.google.inject.Key;
import com.google.inject.Provider;
import com.google.inject.Scope;

import java.util.List;

/**
 * Listens for provisioning of objects. Useful for gathering timing information
 * about provisioning, post-provision initialization, and more.
 * 
 * @author sameb@google.com (Sam Berlin)
 * @since 4.0
 */
public interface ProvisionListener {

  /**
   * Invoked by Guice when an object requires provisioning. Provisioning occurs
   * when Guice locates and injects the dependencies for a binding. For types
   * bound to a Provider, provisioning encapsulates the {@link Provider#get}
   * method. For other types, provisioning encapsulates the construction of the
   * object. If a type is bound within a {@link Scope}, provisioning depends on
   * the scope. Types bound in Singleton scope will only be provisioned once.
   * Types bound in no scope will be provisioned every time they are injected.
   * Other scopes define their own behavior for provisioning.
   * <p>
   * To perform the provision, call {@link ProvisionInvocation#provision()}.
   * If you do not explicitly call provision, it will be automatically done after
   * this method returns.  It is an error to call provision more than once.
   */
  <T> void onProvision(ProvisionInvocation<T> provision);

  /** Encapsulates a single act of provisioning.*/ 
  public abstract static class ProvisionInvocation<T> {

    /** Returns the Key which will be provisioned. */
    public abstract Key<T> getKey();

    /** Performs the provision, returning the object provisioned. */
    public abstract T provision();
    
    /** Returns the dependency chain that led to this object being provisioned. */
    public abstract List<DependencyAndSource> getDependencyChain();
  }
}