blob: ab73b4929bf5c0d77d8e3cbfa7464bc8925a8635 [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
limpbizkit5ae41eb2009-06-06 17:51:27 +000017package com.google.inject.internal;
limpbizkit3d58d6b2008-03-08 16:11:47 +000018
limpbizkit5ae41eb2009-06-06 17:51:27 +000019import com.google.inject.Key;
sberlin888a2642010-02-11 22:07:07 +000020import com.google.inject.internal.InjectorImpl.JitLimitation;
limpbizkita98bc7a2008-08-29 16:52:44 +000021import com.google.inject.spi.Dependency;
limpbizkit3d58d6b2008-03-08 16:11:47 +000022
23/**
limpbizkitfcbdf992008-11-26 02:37:35 +000024 * A placeholder which enables us to swap in the real factory once the injector is created.
sberlin888a2642010-02-11 22:07:07 +000025 * Used for a linked binding, so that getting the linked binding returns the link's factory.
limpbizkit3d58d6b2008-03-08 16:11:47 +000026 */
limpbizkit5ae41eb2009-06-06 17:51:27 +000027final class FactoryProxy<T> implements InternalFactory<T>, BindingProcessor.CreationListener {
limpbizkit3d58d6b2008-03-08 16:11:47 +000028
limpbizkitfcbdf992008-11-26 02:37:35 +000029 private final InjectorImpl injector;
limpbizkit3d58d6b2008-03-08 16:11:47 +000030 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
limpbizkitfcbdf992008-11-26 02:37:35 +000036 FactoryProxy(InjectorImpl injector, Key<T> key, Key<? extends T> targetKey, Object source) {
37 this.injector = injector;
limpbizkit3d58d6b2008-03-08 16:11:47 +000038 this.key = key;
39 this.targetKey = targetKey;
40 this.source = source;
41 }
42
limpbizkitc3f92842008-12-30 19:43:47 +000043 public void notify(final Errors errors) {
limpbizkit9dc32d42008-06-15 11:29:10 +000044 try {
sberlin888a2642010-02-11 22:07:07 +000045 targetFactory = injector.getInternalFactory(targetKey, errors.withSource(source), JitLimitation.NEW_OR_EXISTING_JIT);
limpbizkit163c48a2008-06-16 02:58:08 +000046 } catch (ErrorsException e) {
limpbizkit9dc32d42008-06-15 11:29:10 +000047 errors.merge(e.getErrors());
limpbizkit9dc32d42008-06-15 11:29:10 +000048 }
limpbizkit3d58d6b2008-03-08 16:11:47 +000049 }
50
sberlin888a2642010-02-11 22:07:07 +000051 public T get(Errors errors, InternalContext context, Dependency<?> dependency, boolean linked)
limpbizkit163c48a2008-06-16 02:58:08 +000052 throws ErrorsException {
sberlin888a2642010-02-11 22:07:07 +000053 return targetFactory.get(errors.withSource(targetKey), context, dependency, true);
limpbizkit3d58d6b2008-03-08 16:11:47 +000054 }
55
limpbizkit477f9f92008-07-28 07:05:14 +000056 @Override public String toString() {
limpbizkit3d58d6b2008-03-08 16:11:47 +000057 return new ToStringBuilder(FactoryProxy.class)
58 .add("key", key)
59 .add("provider", targetFactory)
60 .toString();
61 }
62}