blob: 67f89e6de48d4137f69970e9806fde8a49f60c11 [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;
limpbizkit163c48a2008-06-16 02:58:08 +000023import com.google.inject.spi.InjectionPoint;
limpbizkit3d58d6b2008-03-08 16:11:47 +000024
25/**
26 * A placeholder which enables us to swap in the real factory once the
27 * container is created.
28 */
limpbizkit9dc32d42008-06-15 11:29:10 +000029class FactoryProxy<T> implements InternalFactory<T>, BindCommandProcessor.CreationListener {
limpbizkit3d58d6b2008-03-08 16:11:47 +000030
31 private final Key<T> key;
32 private final Key<? extends T> targetKey;
33 private final Object source;
34
limpbizkite4647a62008-05-25 18:03:35 +000035 private InternalFactory<? extends T> targetFactory;
limpbizkit3d58d6b2008-03-08 16:11:47 +000036
37 FactoryProxy(Key<T> key, Key<? extends T> targetKey, Object source) {
38 this.key = key;
39 this.targetKey = targetKey;
40 this.source = source;
41 }
42
limpbizkit9dc32d42008-06-15 11:29:10 +000043 public void notify(final InjectorImpl injector, final Errors errors) {
limpbizkit163c48a2008-06-16 02:58:08 +000044 errors.pushSource(source);
limpbizkit9dc32d42008-06-15 11:29:10 +000045 try {
limpbizkit163c48a2008-06-16 02:58:08 +000046 targetFactory = injector.getInternalFactory(targetKey, errors);
47 } catch (ErrorsException e) {
limpbizkit9dc32d42008-06-15 11:29:10 +000048 errors.merge(e.getErrors());
limpbizkit163c48a2008-06-16 02:58:08 +000049 } finally {
50 errors.popSource(source);
limpbizkit9dc32d42008-06-15 11:29:10 +000051 }
limpbizkit3d58d6b2008-03-08 16:11:47 +000052 }
53
limpbizkit9dc32d42008-06-15 11:29:10 +000054 public T get(Errors errors, InternalContext context, InjectionPoint<?> injectionPoint)
limpbizkit163c48a2008-06-16 02:58:08 +000055 throws ErrorsException {
limpbizkit9dc32d42008-06-15 11:29:10 +000056 return targetFactory.get(errors, context, injectionPoint);
limpbizkit3d58d6b2008-03-08 16:11:47 +000057 }
58
59 public String toString() {
60 return new ToStringBuilder(FactoryProxy.class)
61 .add("key", key)
62 .add("provider", targetFactory)
63 .toString();
64 }
65}