crazyboblee | abc4dd0 | 2007-02-01 01:44:36 +0000 | [diff] [blame] | 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 | */ |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 16 | |
| 17 | package com.google.inject; |
| 18 | |
limpbizkit | e4647a6 | 2008-05-25 18:03:35 +0000 | [diff] [blame] | 19 | import com.google.inject.internal.ErrorMessages; |
kevinb9n | a312d7a | 2007-03-01 10:57:35 +0000 | [diff] [blame] | 20 | import com.google.inject.spi.SourceProviders; |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 21 | import static com.google.common.base.Preconditions.checkNotNull; |
kevinb9n | a312d7a | 2007-03-01 10:57:35 +0000 | [diff] [blame] | 22 | |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 23 | /** |
| 24 | * @author crazybob@google.com (Bob Lee) |
| 25 | */ |
crazyboblee | bd9544e | 2007-02-25 20:32:11 +0000 | [diff] [blame] | 26 | class InternalFactoryToProviderAdapter<T> implements InternalFactory<T> { |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 27 | |
crazyboblee | bd9544e | 2007-02-25 20:32:11 +0000 | [diff] [blame] | 28 | private final Provider<? extends T> provider; |
kevinb9n | a312d7a | 2007-03-01 10:57:35 +0000 | [diff] [blame] | 29 | private final Object source; |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 30 | |
crazyboblee | bd9544e | 2007-02-25 20:32:11 +0000 | [diff] [blame] | 31 | public InternalFactoryToProviderAdapter(Provider<? extends T> provider) { |
kevinb9n | a312d7a | 2007-03-01 10:57:35 +0000 | [diff] [blame] | 32 | this(provider, SourceProviders.UNKNOWN_SOURCE); |
| 33 | } |
| 34 | |
| 35 | public InternalFactoryToProviderAdapter( |
| 36 | Provider<? extends T> provider, Object source) { |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 37 | this.provider = checkNotNull(provider, "provider"); |
| 38 | this.source = checkNotNull(source, "source"); |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 39 | } |
limpbizkit | 51515b5 | 2008-03-11 03:46:25 +0000 | [diff] [blame] | 40 | |
limpbizkit | fcf2b8c | 2007-10-21 18:23:43 +0000 | [diff] [blame] | 41 | public T get(InternalContext context, InjectionPoint<?> injectionPoint) { |
limpbizkit | 51515b5 | 2008-03-11 03:46:25 +0000 | [diff] [blame] | 42 | context.ensureMemberInjected(provider); |
limpbizkit | e4647a6 | 2008-05-25 18:03:35 +0000 | [diff] [blame] | 43 | try { |
| 44 | return injectionPoint.checkForNull(provider.get(), source); |
| 45 | } catch(ProvisionException e) { |
| 46 | throw e; |
| 47 | } catch(RuntimeException e) { |
| 48 | throw new ProvisionException(e, ErrorMessages.ERROR_IN_PROVIDER); |
| 49 | } |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | public String toString() { |
crazyboblee | bd9544e | 2007-02-25 20:32:11 +0000 | [diff] [blame] | 53 | return provider.toString(); |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 54 | } |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 55 | } |