limpbizkit | 00ca9f7 | 2008-08-02 17:56:17 +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.Errors; |
| 20 | import com.google.inject.spi.Element; |
limpbizkit | afa4b5d | 2008-08-02 18:40:47 +0000 | [diff] [blame] | 21 | import com.google.inject.spi.ElementVisitor; |
limpbizkit | 00ca9f7 | 2008-08-02 17:56:17 +0000 | [diff] [blame] | 22 | import com.google.inject.spi.InjectionRequest; |
limpbizkit | ee79246 | 2009-04-08 23:48:49 +0000 | [diff] [blame] | 23 | import com.google.inject.spi.MembersInjectorLookup; |
limpbizkit | 00ca9f7 | 2008-08-02 17:56:17 +0000 | [diff] [blame] | 24 | import com.google.inject.spi.Message; |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 25 | import com.google.inject.spi.PrivateElements; |
limpbizkit | 00ca9f7 | 2008-08-02 17:56:17 +0000 | [diff] [blame] | 26 | import com.google.inject.spi.ProviderLookup; |
| 27 | import com.google.inject.spi.ScopeBinding; |
| 28 | import com.google.inject.spi.StaticInjectionRequest; |
| 29 | import com.google.inject.spi.TypeConverterBinding; |
limpbizkit | ee79246 | 2009-04-08 23:48:49 +0000 | [diff] [blame] | 30 | import com.google.inject.spi.TypeListenerBinding; |
limpbizkit | 00ca9f7 | 2008-08-02 17:56:17 +0000 | [diff] [blame] | 31 | import java.util.Iterator; |
| 32 | import 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 | */ |
limpbizkit | afa4b5d | 2008-08-02 18:40:47 +0000 | [diff] [blame] | 43 | abstract class AbstractProcessor implements ElementVisitor<Boolean> { |
limpbizkit | 00ca9f7 | 2008-08-02 17:56:17 +0000 | [diff] [blame] | 44 | |
| 45 | protected Errors errors; |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 46 | protected InjectorImpl injector; |
limpbizkit | 00ca9f7 | 2008-08-02 17:56:17 +0000 | [diff] [blame] | 47 | |
| 48 | protected AbstractProcessor(Errors errors) { |
| 49 | this.errors = errors; |
| 50 | } |
| 51 | |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 52 | 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) { |
limpbizkit | 00ca9f7 | 2008-08-02 17:56:17 +0000 | [diff] [blame] | 59 | Errors errorsAnyElement = this.errors; |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 60 | this.injector = injector; |
limpbizkit | 00ca9f7 | 2008-08-02 17:56:17 +0000 | [diff] [blame] | 61 | 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; |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 72 | this.injector = null; |
limpbizkit | 00ca9f7 | 2008-08-02 17:56:17 +0000 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 76 | public Boolean visit(Message message) { |
limpbizkit | 00ca9f7 | 2008-08-02 17:56:17 +0000 | [diff] [blame] | 77 | return false; |
| 78 | } |
| 79 | |
limpbizkit | bf0d876 | 2009-02-19 09:06:22 +0000 | [diff] [blame] | 80 | /*if[AOP]*/ |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 81 | public Boolean visit( |
limpbizkit | 4f6274a | 2009-02-19 21:57:55 +0000 | [diff] [blame] | 82 | com.google.inject.spi.InterceptorBinding interceptorBinding) { |
limpbizkit | 00ca9f7 | 2008-08-02 17:56:17 +0000 | [diff] [blame] | 83 | return false; |
| 84 | } |
limpbizkit | bf0d876 | 2009-02-19 09:06:22 +0000 | [diff] [blame] | 85 | /*end[AOP]*/ |
limpbizkit | 00ca9f7 | 2008-08-02 17:56:17 +0000 | [diff] [blame] | 86 | |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 87 | public Boolean visit(ScopeBinding scopeBinding) { |
limpbizkit | 00ca9f7 | 2008-08-02 17:56:17 +0000 | [diff] [blame] | 88 | return false; |
| 89 | } |
| 90 | |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 91 | public Boolean visit(InjectionRequest injectionRequest) { |
limpbizkit | 00ca9f7 | 2008-08-02 17:56:17 +0000 | [diff] [blame] | 92 | return false; |
| 93 | } |
| 94 | |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 95 | public Boolean visit(StaticInjectionRequest staticInjectionRequest) { |
limpbizkit | 00ca9f7 | 2008-08-02 17:56:17 +0000 | [diff] [blame] | 96 | return false; |
| 97 | } |
| 98 | |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 99 | public Boolean visit(TypeConverterBinding typeConverterBinding) { |
limpbizkit | 00ca9f7 | 2008-08-02 17:56:17 +0000 | [diff] [blame] | 100 | return false; |
| 101 | } |
| 102 | |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 103 | public <T> Boolean visit(Binding<T> binding) { |
limpbizkit | 00ca9f7 | 2008-08-02 17:56:17 +0000 | [diff] [blame] | 104 | return false; |
| 105 | } |
| 106 | |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 107 | public <T> Boolean visit(ProviderLookup<T> providerLookup) { |
limpbizkit | 00ca9f7 | 2008-08-02 17:56:17 +0000 | [diff] [blame] | 108 | return false; |
| 109 | } |
limpbizkit | 5ea4ab2 | 2008-11-25 08:43:49 +0000 | [diff] [blame] | 110 | |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 111 | public Boolean visit(PrivateElements privateElements) { |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | public <T> Boolean visit(MembersInjectorLookup<T> lookup) { |
| 116 | return false; |
| 117 | } |
| 118 | |
limpbizkit | ee79246 | 2009-04-08 23:48:49 +0000 | [diff] [blame] | 119 | public Boolean visit(TypeListenerBinding binding) { |
limpbizkit | 5ea4ab2 | 2008-11-25 08:43:49 +0000 | [diff] [blame] | 120 | return false; |
| 121 | } |
limpbizkit | 00ca9f7 | 2008-08-02 17:56:17 +0000 | [diff] [blame] | 122 | } |