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 17package com.google.inject.internal; 18 19import com.google.inject.spi.DefaultElementVisitor; 20import com.google.inject.spi.Element; 21 22import java.util.Iterator; 23import java.util.List; 24 25/** 26 * Abstract base class for creating an injector from module elements. 27 * 28 * <p>Extending classes must return {@code true} from any overridden 29 * {@code visit*()} methods, in order for the element processor to remove the 30 * handled element. 31 * 32 * @author jessewilson@google.com (Jesse Wilson) 33 */ 34abstract class AbstractProcessor extends DefaultElementVisitor<Boolean> { 35 36 protected Errors errors; 37 protected InjectorImpl injector; 38 39 protected AbstractProcessor(Errors errors) { 40 this.errors = errors; 41 } 42 43 public void process(Iterable<InjectorShell> isolatedInjectorBuilders) { 44 for (InjectorShell injectorShell : isolatedInjectorBuilders) { 45 process(injectorShell.getInjector(), injectorShell.getElements()); 46 } 47 } 48 49 public void process(InjectorImpl injector, List<Element> elements) { 50 Errors errorsAnyElement = this.errors; 51 this.injector = injector; 52 try { 53 for (Iterator<Element> i = elements.iterator(); i.hasNext(); ) { 54 Element element = i.next(); 55 this.errors = errorsAnyElement.withSource(element.getSource()); 56 Boolean allDone = element.acceptVisitor(this); 57 if (allDone) { 58 i.remove(); 59 } 60 } 61 } finally { 62 this.errors = errorsAnyElement; 63 this.injector = null; 64 } 65 } 66 67 @Override 68 protected Boolean visitOther(Element element) { 69 return false; 70 } 71} 72