blob: 1b447f81b05c597697009d2534077f327f39b74b [file] [log] [blame]
limpbizkit00ca9f72008-08-02 17:56:17 +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
17package com.google.inject;
18
19import com.google.inject.internal.Errors;
20import com.google.inject.spi.Element;
limpbizkitafa4b5d2008-08-02 18:40:47 +000021import com.google.inject.spi.ElementVisitor;
limpbizkit00ca9f72008-08-02 17:56:17 +000022import com.google.inject.spi.InjectionRequest;
limpbizkitee792462009-04-08 23:48:49 +000023import com.google.inject.spi.MembersInjectorLookup;
limpbizkit00ca9f72008-08-02 17:56:17 +000024import com.google.inject.spi.Message;
limpbizkitc3f92842008-12-30 19:43:47 +000025import com.google.inject.spi.PrivateElements;
limpbizkit00ca9f72008-08-02 17:56:17 +000026import com.google.inject.spi.ProviderLookup;
27import com.google.inject.spi.ScopeBinding;
28import com.google.inject.spi.StaticInjectionRequest;
29import com.google.inject.spi.TypeConverterBinding;
limpbizkitee792462009-04-08 23:48:49 +000030import com.google.inject.spi.TypeListenerBinding;
limpbizkit00ca9f72008-08-02 17:56:17 +000031import java.util.Iterator;
32import java.util.List;
33
34/**
35 * Abstract base class for creating an injector from module elements.
36 *
37 * <p>Extending classes must return {@code true} from any overridden
38 * {@code visit*()} methods, in order for the element processor to remove the
39 * handled element.
40 *
41 * @author jessewilson@google.com (Jesse Wilson)
42 */
limpbizkitafa4b5d2008-08-02 18:40:47 +000043abstract class AbstractProcessor implements ElementVisitor<Boolean> {
limpbizkit00ca9f72008-08-02 17:56:17 +000044
45 protected Errors errors;
limpbizkitfcbdf992008-11-26 02:37:35 +000046 protected InjectorImpl injector;
limpbizkit00ca9f72008-08-02 17:56:17 +000047
48 protected AbstractProcessor(Errors errors) {
49 this.errors = errors;
50 }
51
limpbizkitfcbdf992008-11-26 02:37:35 +000052 public void process(Iterable<InjectorShell> isolatedInjectorBuilders) {
53 for (InjectorShell injectorShell : isolatedInjectorBuilders) {
54 process(injectorShell.getInjector(), injectorShell.getElements());
55 }
56 }
57
58 public void process(InjectorImpl injector, List<Element> elements) {
limpbizkit00ca9f72008-08-02 17:56:17 +000059 Errors errorsAnyElement = this.errors;
limpbizkitfcbdf992008-11-26 02:37:35 +000060 this.injector = injector;
limpbizkit00ca9f72008-08-02 17:56:17 +000061 try {
62 for (Iterator<Element> i = elements.iterator(); i.hasNext(); ) {
63 Element element = i.next();
64 this.errors = errorsAnyElement.withSource(element.getSource());
65 Boolean allDone = element.acceptVisitor(this);
66 if (allDone) {
67 i.remove();
68 }
69 }
70 } finally {
71 this.errors = errorsAnyElement;
limpbizkitfcbdf992008-11-26 02:37:35 +000072 this.injector = null;
limpbizkit00ca9f72008-08-02 17:56:17 +000073 }
74 }
75
limpbizkit03b81a62009-03-18 05:34:39 +000076 public Boolean visit(Message message) {
limpbizkit00ca9f72008-08-02 17:56:17 +000077 return false;
78 }
79
limpbizkitbf0d8762009-02-19 09:06:22 +000080 /*if[AOP]*/
limpbizkit03b81a62009-03-18 05:34:39 +000081 public Boolean visit(
limpbizkit4f6274a2009-02-19 21:57:55 +000082 com.google.inject.spi.InterceptorBinding interceptorBinding) {
limpbizkit00ca9f72008-08-02 17:56:17 +000083 return false;
84 }
limpbizkitbf0d8762009-02-19 09:06:22 +000085 /*end[AOP]*/
limpbizkit00ca9f72008-08-02 17:56:17 +000086
limpbizkit03b81a62009-03-18 05:34:39 +000087 public Boolean visit(ScopeBinding scopeBinding) {
limpbizkit00ca9f72008-08-02 17:56:17 +000088 return false;
89 }
90
limpbizkit03b81a62009-03-18 05:34:39 +000091 public Boolean visit(InjectionRequest injectionRequest) {
limpbizkit00ca9f72008-08-02 17:56:17 +000092 return false;
93 }
94
limpbizkit03b81a62009-03-18 05:34:39 +000095 public Boolean visit(StaticInjectionRequest staticInjectionRequest) {
limpbizkit00ca9f72008-08-02 17:56:17 +000096 return false;
97 }
98
limpbizkit03b81a62009-03-18 05:34:39 +000099 public Boolean visit(TypeConverterBinding typeConverterBinding) {
limpbizkit00ca9f72008-08-02 17:56:17 +0000100 return false;
101 }
102
limpbizkit03b81a62009-03-18 05:34:39 +0000103 public <T> Boolean visit(Binding<T> binding) {
limpbizkit00ca9f72008-08-02 17:56:17 +0000104 return false;
105 }
106
limpbizkit03b81a62009-03-18 05:34:39 +0000107 public <T> Boolean visit(ProviderLookup<T> providerLookup) {
limpbizkit00ca9f72008-08-02 17:56:17 +0000108 return false;
109 }
limpbizkit5ea4ab22008-11-25 08:43:49 +0000110
limpbizkit03b81a62009-03-18 05:34:39 +0000111 public Boolean visit(PrivateElements privateElements) {
112 return false;
113 }
114
115 public <T> Boolean visit(MembersInjectorLookup<T> lookup) {
116 return false;
117 }
118
limpbizkitee792462009-04-08 23:48:49 +0000119 public Boolean visit(TypeListenerBinding binding) {
limpbizkit5ea4ab22008-11-25 08:43:49 +0000120 return false;
121 }
limpbizkit00ca9f72008-08-02 17:56:17 +0000122}