blob: 5dfdfc82f803a23c73851c8d13cdaf247f430a17 [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
kevinb9n1601ae52008-06-03 22:21:04 +000019import static com.google.common.base.Preconditions.checkNotNull;
limpbizkitdf98fcd2008-06-14 05:02:15 +000020import com.google.inject.internal.ErrorMessage;
21import com.google.inject.spi.SourceProviders;
kevinb9na312d7a2007-03-01 10:57:35 +000022
crazyboblee7289ac12007-02-01 00:28:09 +000023/**
24 * @author crazybob@google.com (Bob Lee)
25*/
crazybobleebd9544e2007-02-25 20:32:11 +000026class InternalFactoryToProviderAdapter<T> implements InternalFactory<T> {
crazyboblee7289ac12007-02-01 00:28:09 +000027
crazybobleebd9544e2007-02-25 20:32:11 +000028 private final Provider<? extends T> provider;
kevinb9na312d7a2007-03-01 10:57:35 +000029 private final Object source;
crazyboblee7289ac12007-02-01 00:28:09 +000030
crazybobleebd9544e2007-02-25 20:32:11 +000031 public InternalFactoryToProviderAdapter(Provider<? extends T> provider) {
kevinb9na312d7a2007-03-01 10:57:35 +000032 this(provider, SourceProviders.UNKNOWN_SOURCE);
33 }
34
35 public InternalFactoryToProviderAdapter(
36 Provider<? extends T> provider, Object source) {
kevinb9n1601ae52008-06-03 22:21:04 +000037 this.provider = checkNotNull(provider, "provider");
38 this.source = checkNotNull(source, "source");
crazyboblee7289ac12007-02-01 00:28:09 +000039 }
limpbizkit51515b52008-03-11 03:46:25 +000040
limpbizkitfcf2b8c2007-10-21 18:23:43 +000041 public T get(InternalContext context, InjectionPoint<?> injectionPoint) {
limpbizkit51515b52008-03-11 03:46:25 +000042 context.ensureMemberInjected(provider);
limpbizkite4647a62008-05-25 18:03:35 +000043 try {
44 return injectionPoint.checkForNull(provider.get(), source);
45 } catch(ProvisionException e) {
46 throw e;
47 } catch(RuntimeException e) {
limpbizkitdf98fcd2008-06-14 05:02:15 +000048 throw new ProvisionException(ErrorMessage.errorInProvider().toString(), e);
limpbizkite4647a62008-05-25 18:03:35 +000049 }
crazyboblee7289ac12007-02-01 00:28:09 +000050 }
51
52 public String toString() {
crazybobleebd9544e2007-02-25 20:32:11 +000053 return provider.toString();
crazyboblee7289ac12007-02-01 00:28:09 +000054 }
crazyboblee7289ac12007-02-01 00:28:09 +000055}