blob: afcafb5ecdc0aef48543d1c7fdd04d2b0cd1b852 [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
limpbizkit9dc32d42008-06-15 11:29:10 +000020import com.google.inject.internal.Errors;
limpbizkit163c48a2008-06-16 02:58:08 +000021import com.google.inject.internal.ErrorsException;
limpbizkit3d58d6b2008-03-08 16:11:47 +000022import com.google.inject.internal.ToStringBuilder;
limpbizkita98bc7a2008-08-29 16:52:44 +000023import com.google.inject.spi.Dependency;
limpbizkitfcbdf992008-11-26 02:37:35 +000024import com.google.inject.spi.PrivateEnvironment;
25import java.util.Map;
limpbizkit3d58d6b2008-03-08 16:11:47 +000026
27/**
limpbizkitfcbdf992008-11-26 02:37:35 +000028 * A placeholder which enables us to swap in the real factory once the injector is created.
limpbizkit3d58d6b2008-03-08 16:11:47 +000029 */
limpbizkit00ca9f72008-08-02 17:56:17 +000030class FactoryProxy<T> implements InternalFactory<T>, BindingProcessor.CreationListener {
limpbizkit3d58d6b2008-03-08 16:11:47 +000031
limpbizkitfcbdf992008-11-26 02:37:35 +000032 private final InjectorImpl injector;
limpbizkit3d58d6b2008-03-08 16:11:47 +000033 private final Key<T> key;
34 private final Key<? extends T> targetKey;
35 private final Object source;
36
limpbizkite4647a62008-05-25 18:03:35 +000037 private InternalFactory<? extends T> targetFactory;
limpbizkit3d58d6b2008-03-08 16:11:47 +000038
limpbizkitfcbdf992008-11-26 02:37:35 +000039 FactoryProxy(InjectorImpl injector, Key<T> key, Key<? extends T> targetKey, Object source) {
40 this.injector = injector;
limpbizkit3d58d6b2008-03-08 16:11:47 +000041 this.key = key;
42 this.targetKey = targetKey;
43 this.source = source;
44 }
45
limpbizkitfcbdf992008-11-26 02:37:35 +000046 public void notify(Map<PrivateEnvironment, InjectorImpl> privateInjectors, final Errors errors) {
limpbizkit9dc32d42008-06-15 11:29:10 +000047 try {
limpbizkit2c2c6102008-06-20 13:34:36 +000048 targetFactory = injector.getInternalFactory(targetKey, errors.withSource(source));
limpbizkit163c48a2008-06-16 02:58:08 +000049 } catch (ErrorsException e) {
limpbizkit9dc32d42008-06-15 11:29:10 +000050 errors.merge(e.getErrors());
limpbizkit9dc32d42008-06-15 11:29:10 +000051 }
limpbizkit3d58d6b2008-03-08 16:11:47 +000052 }
53
limpbizkita98bc7a2008-08-29 16:52:44 +000054 public T get(Errors errors, InternalContext context, Dependency<?> dependency)
limpbizkit163c48a2008-06-16 02:58:08 +000055 throws ErrorsException {
limpbizkit06898062008-11-02 05:14:55 +000056 return targetFactory.get(errors.withSource(targetKey), context, dependency);
limpbizkit3d58d6b2008-03-08 16:11:47 +000057 }
58
limpbizkit477f9f92008-07-28 07:05:14 +000059 @Override public String toString() {
limpbizkit3d58d6b2008-03-08 16:11:47 +000060 return new ToStringBuilder(FactoryProxy.class)
61 .add("key", key)
62 .add("provider", targetFactory)
63 .toString();
64 }
65}