• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006 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.internal;
18 
19 import com.google.common.base.MoreObjects;
20 import com.google.inject.Binding;
21 import com.google.inject.Key;
22 import com.google.inject.Provider;
23 import com.google.inject.spi.BindingScopingVisitor;
24 import com.google.inject.spi.ElementVisitor;
25 import com.google.inject.spi.InstanceBinding;
26 
27 /** @author crazybob@google.com (Bob Lee) */
28 public abstract class BindingImpl<T> implements Binding<T> {
29 
30   private final InjectorImpl injector;
31   private final Key<T> key;
32   private final Object source;
33   private final Scoping scoping;
34   private final InternalFactory<? extends T> internalFactory;
35 
BindingImpl( InjectorImpl injector, Key<T> key, Object source, InternalFactory<? extends T> internalFactory, Scoping scoping)36   public BindingImpl(
37       InjectorImpl injector,
38       Key<T> key,
39       Object source,
40       InternalFactory<? extends T> internalFactory,
41       Scoping scoping) {
42     this.injector = injector;
43     this.key = key;
44     this.source = source;
45     this.internalFactory = internalFactory;
46     this.scoping = scoping;
47   }
48 
BindingImpl(Object source, Key<T> key, Scoping scoping)49   protected BindingImpl(Object source, Key<T> key, Scoping scoping) {
50     this.internalFactory = null;
51     this.injector = null;
52     this.source = source;
53     this.key = key;
54     this.scoping = scoping;
55   }
56 
57   @Override
getKey()58   public Key<T> getKey() {
59     return key;
60   }
61 
62   @Override
getSource()63   public Object getSource() {
64     return source;
65   }
66 
67   private volatile Provider<T> provider;
68 
69   @Override
getProvider()70   public Provider<T> getProvider() {
71     if (provider == null) {
72       if (injector == null) {
73         throw new UnsupportedOperationException("getProvider() not supported for module bindings");
74       }
75 
76       provider = injector.getProvider(key);
77     }
78     return provider;
79   }
80 
getInternalFactory()81   public InternalFactory<? extends T> getInternalFactory() {
82     return internalFactory;
83   }
84 
getScoping()85   public Scoping getScoping() {
86     return scoping;
87   }
88 
89   /**
90    * Is this a constant binding? This returns true for constant bindings as well as toInstance()
91    * bindings.
92    */
isConstant()93   public boolean isConstant() {
94     return this instanceof InstanceBinding;
95   }
96 
97   @Override
acceptVisitor(ElementVisitor<V> visitor)98   public <V> V acceptVisitor(ElementVisitor<V> visitor) {
99     return visitor.visit(this);
100   }
101 
102   @Override
acceptScopingVisitor(BindingScopingVisitor<V> visitor)103   public <V> V acceptScopingVisitor(BindingScopingVisitor<V> visitor) {
104     return scoping.acceptVisitor(visitor);
105   }
106 
withScoping(Scoping scoping)107   protected BindingImpl<T> withScoping(Scoping scoping) {
108     throw new AssertionError();
109   }
110 
withKey(Key<T> key)111   protected BindingImpl<T> withKey(Key<T> key) {
112     throw new AssertionError();
113   }
114 
115   @Override
toString()116   public String toString() {
117     return MoreObjects.toStringHelper(Binding.class)
118         .add("key", key)
119         .add("scope", scoping)
120         .add("source", source)
121         .toString();
122   }
123 
getInjector()124   public InjectorImpl getInjector() {
125     return injector;
126   }
127 }
128