blob: 7b0387fd1e01c29ed09816d01a60d9fe3d374bd4 [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;
limpbizkit@gmail.com9a227be2010-07-03 15:51:31 +000021import com.google.inject.internal.util.ToStringBuilder;
limpbizkita98bc7a2008-08-29 16:52:44 +000022import com.google.inject.spi.Dependency;
limpbizkit3d58d6b2008-03-08 16:11:47 +000023
24/**
limpbizkitfcbdf992008-11-26 02:37:35 +000025 * A placeholder which enables us to swap in the real factory once the injector is created.
sberlin888a2642010-02-11 22:07:07 +000026 * Used for a linked binding, so that getting the linked binding returns the link's factory.
limpbizkit3d58d6b2008-03-08 16:11:47 +000027 */
sberlin07170cc2011-03-11 03:02:39 +000028final class FactoryProxy<T> implements InternalFactory<T>, CreationListener {
limpbizkit3d58d6b2008-03-08 16:11:47 +000029
limpbizkitfcbdf992008-11-26 02:37:35 +000030 private final InjectorImpl injector;
limpbizkit3d58d6b2008-03-08 16:11:47 +000031 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
limpbizkitfcbdf992008-11-26 02:37:35 +000037 FactoryProxy(InjectorImpl injector, Key<T> key, Key<? extends T> targetKey, Object source) {
38 this.injector = injector;
limpbizkit3d58d6b2008-03-08 16:11:47 +000039 this.key = key;
40 this.targetKey = targetKey;
41 this.source = source;
42 }
43
limpbizkitc3f92842008-12-30 19:43:47 +000044 public void notify(final Errors errors) {
limpbizkit9dc32d42008-06-15 11:29:10 +000045 try {
sberlin888a2642010-02-11 22:07:07 +000046 targetFactory = injector.getInternalFactory(targetKey, errors.withSource(source), JitLimitation.NEW_OR_EXISTING_JIT);
limpbizkit163c48a2008-06-16 02:58:08 +000047 } catch (ErrorsException e) {
limpbizkit9dc32d42008-06-15 11:29:10 +000048 errors.merge(e.getErrors());
limpbizkit9dc32d42008-06-15 11:29:10 +000049 }
limpbizkit3d58d6b2008-03-08 16:11:47 +000050 }
51
sberlin888a2642010-02-11 22:07:07 +000052 public T get(Errors errors, InternalContext context, Dependency<?> dependency, boolean linked)
limpbizkit163c48a2008-06-16 02:58:08 +000053 throws ErrorsException {
sberlinba75f352011-06-12 21:54:43 +000054 context.pushState(targetKey, source);
55 try {
56 return targetFactory.get(errors.withSource(targetKey), context, dependency, true);
57 } finally {
58 context.popState();
59 }
limpbizkit3d58d6b2008-03-08 16:11:47 +000060 }
61
limpbizkit477f9f92008-07-28 07:05:14 +000062 @Override public String toString() {
limpbizkit3d58d6b2008-03-08 16:11:47 +000063 return new ToStringBuilder(FactoryProxy.class)
64 .add("key", key)
65 .add("provider", targetFactory)
66 .toString();
67 }
68}