1 /* 2 * Copyright (C) 2010 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.internal; 18 19 import static com.google.common.base.Preconditions.checkNotNull; 20 import static com.google.common.base.Preconditions.checkState; 21 22 import com.google.inject.Stage; 23 import com.google.inject.internal.InjectorImpl.InjectorOptions; 24 import com.google.inject.spi.DisableCircularProxiesOption; 25 import com.google.inject.spi.RequireAtInjectOnConstructorsOption; 26 import com.google.inject.spi.RequireExactBindingAnnotationsOption; 27 import com.google.inject.spi.RequireExplicitBindingsOption; 28 29 /** 30 * A processor to gather injector options. 31 * 32 * @author sameb (at) google.com (Sam Berlin) 33 */ 34 class InjectorOptionsProcessor extends AbstractProcessor { 35 36 private boolean disableCircularProxies = false; 37 private boolean jitDisabled = false; 38 private boolean atInjectRequired = false; 39 private boolean exactBindingAnnotationsRequired = false; 40 41 InjectorOptionsProcessor(Errors errors) { 42 super(errors); 43 } 44 45 @Override 46 public Boolean visit(DisableCircularProxiesOption option) { 47 disableCircularProxies = true; 48 return true; 49 } 50 51 @Override 52 public Boolean visit(RequireExplicitBindingsOption option) { 53 jitDisabled = true; 54 return true; 55 } 56 57 @Override 58 public Boolean visit(RequireAtInjectOnConstructorsOption option) { 59 atInjectRequired = true; 60 return true; 61 } 62 63 @Override 64 public Boolean visit(RequireExactBindingAnnotationsOption option) { 65 exactBindingAnnotationsRequired = true; 66 return true; 67 } 68 69 InjectorOptions getOptions(Stage stage, InjectorOptions parentOptions) { 70 checkNotNull(stage, "stage must be set"); 71 if(parentOptions == null) { 72 return new InjectorOptions( 73 stage, 74 jitDisabled, 75 disableCircularProxies, 76 atInjectRequired, 77 exactBindingAnnotationsRequired); 78 } else { 79 checkState(stage == parentOptions.stage, "child & parent stage don't match"); 80 return new InjectorOptions( 81 stage, 82 jitDisabled || parentOptions.jitDisabled, 83 disableCircularProxies || parentOptions.disableCircularProxies, 84 atInjectRequired || parentOptions.atInjectRequired, 85 exactBindingAnnotationsRequired || parentOptions.exactBindingAnnotationsRequired); 86 } 87 } 88 89 } 90