limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 1 | /** |
| 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 | package com.google.inject; |
| 18 | |
| 19 | import com.google.inject.internal.BindingImpl; |
| 20 | import com.google.inject.internal.Errors; |
| 21 | import com.google.inject.internal.ErrorsException; |
| 22 | import com.google.inject.internal.InternalContext; |
| 23 | import com.google.inject.internal.InternalFactory; |
| 24 | import com.google.inject.spi.Dependency; |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 25 | import com.google.inject.spi.PrivateElements; |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 26 | |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 27 | /** |
| 28 | * This factory exists in a parent injector. When invoked, it retrieves its value from a child |
| 29 | * injector. |
| 30 | */ |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 31 | class ExposedKeyFactory<T> implements InternalFactory<T>, BindingProcessor.CreationListener { |
| 32 | private final Key<T> key; |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 33 | private final PrivateElements privateElements; |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 34 | private BindingImpl<T> delegate; |
| 35 | |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 36 | public ExposedKeyFactory(Key<T> key, PrivateElements privateElements) { |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 37 | this.key = key; |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 38 | this.privateElements = privateElements; |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 39 | } |
| 40 | |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 41 | public void notify(Errors errors) { |
| 42 | InjectorImpl privateInjector = (InjectorImpl) privateElements.getInjector(); |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 43 | BindingImpl<T> explicitBinding = privateInjector.state.getExplicitBinding(key); |
| 44 | |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 45 | // validate that the child injector has its own factory. If the getInternalFactory() returns |
| 46 | // this, then that child injector doesn't have a factory (and getExplicitBinding has returned |
| 47 | // its parent's binding instead |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 48 | if (explicitBinding.getInternalFactory() == this) { |
| 49 | errors.withSource(explicitBinding.getSource()).exposedButNotBound(key); |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | this.delegate = explicitBinding; |
| 54 | } |
| 55 | |
| 56 | public T get(Errors errors, InternalContext context, Dependency<?> dependency) |
| 57 | throws ErrorsException { |
| 58 | return delegate.getInternalFactory().get(errors, context, dependency); |
| 59 | } |
| 60 | } |