1 /** 2 * Copyright (C) 2006 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 com.google.inject.Provider; 20 import com.google.inject.ProvisionException; 21 import com.google.inject.spi.Dependency; 22 23 /** 24 * @author crazybob (at) google.com (Bob Lee) 25 */ 26 final class ProviderToInternalFactoryAdapter<T> implements Provider<T> { 27 28 private final InjectorImpl injector; 29 private final InternalFactory<? extends T> internalFactory; 30 31 public ProviderToInternalFactoryAdapter(InjectorImpl injector, 32 InternalFactory<? extends T> internalFactory) { 33 this.injector = injector; 34 this.internalFactory = internalFactory; 35 } 36 37 public T get() { 38 final Errors errors = new Errors(); 39 try { 40 T t = injector.callInContext(new ContextualCallable<T>() { 41 public T call(InternalContext context) throws ErrorsException { 42 Dependency dependency = context.getDependency(); 43 // Always pretend that we are a linked binding, to support 44 // scoping implicit bindings. If we are not actually a linked 45 // binding, we'll fail properly elsewhere in the chain. 46 return internalFactory.get(errors, context, dependency, true); 47 } 48 }); 49 errors.throwIfNewErrors(0); 50 return t; 51 } catch (ErrorsException e) { 52 throw new ProvisionException(errors.merge(e.getErrors()).getMessages()); 53 } 54 } 55 56 @Override public String toString() { 57 return internalFactory.toString(); 58 } 59 } 60