blob: b771caa1f216fe199a2cb3627c7c93470306cdf1 [file] [log] [blame]
limpbizkit76c24b12008-12-25 04:32:41 +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;
limpbizkit76c24b12008-12-25 04:32:41 +000018
limpbizkit5ae41eb2009-06-06 17:51:27 +000019import com.google.inject.Key;
limpbizkit76c24b12008-12-25 04:32:41 +000020import com.google.inject.spi.Dependency;
limpbizkitc3f92842008-12-30 19:43:47 +000021import com.google.inject.spi.PrivateElements;
limpbizkit76c24b12008-12-25 04:32:41 +000022
limpbizkitc3f92842008-12-30 19:43:47 +000023/**
24 * This factory exists in a parent injector. When invoked, it retrieves its value from a child
25 * injector.
26 */
limpbizkit5ae41eb2009-06-06 17:51:27 +000027final class ExposedKeyFactory<T> implements InternalFactory<T>, BindingProcessor.CreationListener {
limpbizkit76c24b12008-12-25 04:32:41 +000028 private final Key<T> key;
limpbizkitc3f92842008-12-30 19:43:47 +000029 private final PrivateElements privateElements;
limpbizkit76c24b12008-12-25 04:32:41 +000030 private BindingImpl<T> delegate;
31
limpbizkit5ae41eb2009-06-06 17:51:27 +000032 ExposedKeyFactory(Key<T> key, PrivateElements privateElements) {
limpbizkit76c24b12008-12-25 04:32:41 +000033 this.key = key;
limpbizkitc3f92842008-12-30 19:43:47 +000034 this.privateElements = privateElements;
limpbizkit76c24b12008-12-25 04:32:41 +000035 }
36
limpbizkitc3f92842008-12-30 19:43:47 +000037 public void notify(Errors errors) {
38 InjectorImpl privateInjector = (InjectorImpl) privateElements.getInjector();
limpbizkit76c24b12008-12-25 04:32:41 +000039 BindingImpl<T> explicitBinding = privateInjector.state.getExplicitBinding(key);
40
limpbizkitc3f92842008-12-30 19:43:47 +000041 // validate that the child injector has its own factory. If the getInternalFactory() returns
42 // this, then that child injector doesn't have a factory (and getExplicitBinding has returned
43 // its parent's binding instead
limpbizkit76c24b12008-12-25 04:32:41 +000044 if (explicitBinding.getInternalFactory() == this) {
45 errors.withSource(explicitBinding.getSource()).exposedButNotBound(key);
46 return;
47 }
48
49 this.delegate = explicitBinding;
50 }
51
52 public T get(Errors errors, InternalContext context, Dependency<?> dependency)
53 throws ErrorsException {
54 return delegate.getInternalFactory().get(errors, context, dependency);
55 }
56}