blob: 8c5a2ebddc2383f37a5bd89a1a8e04dc7f22fc3c [file] [log] [blame]
crazybobleeabc4dd02007-02-01 01:44:36 +00001/**
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 */
crazyboblee7289ac12007-02-01 00:28:09 +000016
17package com.google.inject;
18
limpbizkit9dc32d42008-06-15 11:29:10 +000019import com.google.inject.internal.Errors;
limpbizkit163c48a2008-06-16 02:58:08 +000020import com.google.inject.internal.ErrorsException;
limpbizkit76c24b12008-12-25 04:32:41 +000021import com.google.inject.internal.InternalContext;
22import com.google.inject.internal.InternalFactory;
limpbizkita98bc7a2008-08-29 16:52:44 +000023import com.google.inject.spi.Dependency;
limpbizkit9dc32d42008-06-15 11:29:10 +000024
crazyboblee7289ac12007-02-01 00:28:09 +000025/**
26 * @author crazybob@google.com (Bob Lee)
kevinb9na99dca72007-02-11 04:48:57 +000027 */
crazybobleebd9544e2007-02-25 20:32:11 +000028class ProviderToInternalFactoryAdapter<T> implements Provider<T> {
crazyboblee7289ac12007-02-01 00:28:09 +000029
kevinb9na2915a92007-02-28 06:20:30 +000030 private final InjectorImpl injector;
crazyboblee7289ac12007-02-01 00:28:09 +000031 private final InternalFactory<? extends T> internalFactory;
32
kevinb9na2915a92007-02-28 06:20:30 +000033 public ProviderToInternalFactoryAdapter(InjectorImpl injector,
crazyboblee7289ac12007-02-01 00:28:09 +000034 InternalFactory<? extends T> internalFactory) {
kevinb9na2915a92007-02-28 06:20:30 +000035 this.injector = injector;
crazyboblee7289ac12007-02-01 00:28:09 +000036 this.internalFactory = internalFactory;
37 }
38
39 public T get() {
limpbizkit9dc32d42008-06-15 11:29:10 +000040 final Errors errors = new Errors();
41 try {
42 T t = injector.callInContext(new ContextualCallable<T>() {
limpbizkit163c48a2008-06-16 02:58:08 +000043 public T call(InternalContext context) throws ErrorsException {
limpbizkita98bc7a2008-08-29 16:52:44 +000044 Dependency dependency = context.getDependency();
45 return internalFactory.get(errors, context, dependency);
limpbizkit9dc32d42008-06-15 11:29:10 +000046 }
47 });
limpbizkit72d11dd2008-11-02 07:59:13 +000048 errors.throwIfNewErrors(0);
limpbizkit9dc32d42008-06-15 11:29:10 +000049 return t;
limpbizkit163c48a2008-06-16 02:58:08 +000050 } catch (ErrorsException e) {
limpbizkit490833f2008-11-02 00:12:39 +000051 throw new ProvisionException(errors.merge(e.getErrors()).getMessages());
limpbizkit9dc32d42008-06-15 11:29:10 +000052 }
crazyboblee7289ac12007-02-01 00:28:09 +000053 }
54
limpbizkit9dc32d42008-06-15 11:29:10 +000055 @Override public String toString() {
crazyboblee7289ac12007-02-01 00:28:09 +000056 return internalFactory.toString();
57 }
58}