blob: 2c14a26785a3888d122bf176a3501cca54f52b5b [file] [log] [blame]
limpbizkit8d620752009-03-31 22:37:26 +00001/**
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
17package com.google.inject;
18
19import com.google.inject.internal.Errors;
20import com.google.inject.internal.Lists;
21import com.google.inject.spi.Element;
22import com.google.inject.spi.MembersInjectorLookup;
23import com.google.inject.spi.ProviderLookup;
24import 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 */
limpbizkit050d1f82009-05-16 18:02:27 +000032class DeferredLookups implements Lookups {
limpbizkit8d620752009-03-31 22:37:26 +000033 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}