limpbizkit | 8d62075 | 2009-03-31 22:37:26 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2009 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.Errors; |
| 20 | import com.google.inject.internal.Lists; |
| 21 | import com.google.inject.spi.Element; |
| 22 | import com.google.inject.spi.MembersInjectorLookup; |
| 23 | import com.google.inject.spi.ProviderLookup; |
| 24 | import java.util.List; |
| 25 | |
| 26 | /** |
| 27 | * Returns providers and members injectors that haven't yet been initialized. As a part of injector |
| 28 | * creation it's necessary to {@link #initialize initialize} these lookups. |
| 29 | * |
| 30 | * @author jessewilson@google.com (Jesse Wilson) |
| 31 | */ |
limpbizkit | 050d1f8 | 2009-05-16 18:02:27 +0000 | [diff] [blame] | 32 | class DeferredLookups implements Lookups { |
limpbizkit | 8d62075 | 2009-03-31 22:37:26 +0000 | [diff] [blame] | 33 | private final InjectorImpl injector; |
| 34 | private final List<Element> lookups = Lists.newArrayList(); |
| 35 | |
| 36 | public DeferredLookups(InjectorImpl injector) { |
| 37 | this.injector = injector; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Initialize the specified lookups, either immediately or when the injector is created. |
| 42 | */ |
| 43 | public void initialize(Errors errors) { |
| 44 | injector.lookups = injector; |
| 45 | new LookupProcessor(errors).process(injector, lookups); |
| 46 | } |
| 47 | |
| 48 | public <T> Provider<T> getProvider(Key<T> key) { |
| 49 | ProviderLookup<T> lookup = new ProviderLookup<T>(key, key); |
| 50 | lookups.add(lookup); |
| 51 | return lookup.getProvider(); |
| 52 | } |
| 53 | |
| 54 | public <T> MembersInjector<T> getMembersInjector(TypeLiteral<T> type) { |
| 55 | MembersInjectorLookup<T> lookup = new MembersInjectorLookup<T>(type, type); |
| 56 | lookups.add(lookup); |
| 57 | return lookup.getMembersInjector(); |
| 58 | } |
| 59 | } |