blob: 1e470918cb28a538240e45a0bde7f4f7b2ba449c [file] [log] [blame]
limpbizkit3d58d6b2008-03-08 16:11:47 +00001/**
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
18package com.google.inject;
19
limpbizkit3b1cd582008-04-28 00:06:01 +000020import com.google.inject.internal.ResolveFailedException;
limpbizkit3d58d6b2008-03-08 16:11:47 +000021import com.google.inject.internal.ToStringBuilder;
22
23/**
24 * A placeholder which enables us to swap in the real factory once the
25 * container is created.
26 */
27class FactoryProxy<T> implements InternalFactory<T>,
28 BindCommandProcessor.CreationListener {
29
30 private final Key<T> key;
31 private final Key<? extends T> targetKey;
32 private final Object source;
33
limpbizkite4647a62008-05-25 18:03:35 +000034 private InternalFactory<? extends T> targetFactory;
limpbizkit3d58d6b2008-03-08 16:11:47 +000035
36 FactoryProxy(Key<T> key, Key<? extends T> targetKey, Object source) {
37 this.key = key;
38 this.targetKey = targetKey;
39 this.source = source;
40 }
41
42 public void notify(final InjectorImpl injector) {
43 injector.withDefaultSource(source, new Runnable() {
44 public void run() {
limpbizkit3b1cd582008-04-28 00:06:01 +000045 try {
46 targetFactory = injector.getInternalFactory(targetKey);
47 } catch (ResolveFailedException e) {
48 injector.errorHandler.handle(source, e.getMessage());
49 }
limpbizkit3d58d6b2008-03-08 16:11:47 +000050 }
51 });
52 }
53
54 public T get(InternalContext context, InjectionPoint<?> injectionPoint) {
55 return targetFactory.get(context, injectionPoint);
56 }
57
58 public String toString() {
59 return new ToStringBuilder(FactoryProxy.class)
60 .add("key", key)
61 .add("provider", targetFactory)
62 .toString();
63 }
64}