limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2008 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; |
| 18 | |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 19 | import com.google.inject.internal.Errors; |
| 20 | import com.google.inject.internal.ErrorsException; |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 21 | import com.google.inject.internal.InternalContext; |
| 22 | import com.google.inject.internal.InternalFactory; |
limpbizkit | 0de5e3e | 2008-12-07 08:28:31 +0000 | [diff] [blame] | 23 | import com.google.inject.spi.Dependency; |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * Resolves a single parameter, to be used in a constructor or method invocation. |
| 27 | */ |
| 28 | class SingleParameterInjector<T> { |
limpbizkit | 0de5e3e | 2008-12-07 08:28:31 +0000 | [diff] [blame] | 29 | private static final Object[] NO_ARGUMENTS = {}; |
| 30 | |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 31 | private final Dependency<T> dependency; |
| 32 | private final InternalFactory<? extends T> factory; |
| 33 | |
| 34 | SingleParameterInjector(Dependency<T> dependency, InternalFactory<? extends T> factory) { |
| 35 | this.dependency = dependency; |
| 36 | this.factory = factory; |
| 37 | } |
| 38 | |
| 39 | private T inject(Errors errors, InternalContext context) throws ErrorsException { |
| 40 | context.setDependency(dependency); |
| 41 | try { |
| 42 | return factory.get(errors.withSource(dependency), context, dependency); |
| 43 | } finally { |
| 44 | context.setDependency(null); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Returns an array of parameter values. |
| 50 | */ |
| 51 | static Object[] getAll(Errors errors, InternalContext context, |
limpbizkit | e89c49e | 2009-05-06 01:02:14 +0000 | [diff] [blame] | 52 | SingleParameterInjector<?>[] parameterInjectors) throws ErrorsException { |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 53 | if (parameterInjectors == null) { |
limpbizkit | 0de5e3e | 2008-12-07 08:28:31 +0000 | [diff] [blame] | 54 | return NO_ARGUMENTS; |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 55 | } |
| 56 | |
limpbizkit | 72d11dd | 2008-11-02 07:59:13 +0000 | [diff] [blame] | 57 | int numErrorsBefore = errors.size(); |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 58 | |
limpbizkit | e89c49e | 2009-05-06 01:02:14 +0000 | [diff] [blame] | 59 | int size = parameterInjectors.length; |
| 60 | Object[] parameters = new Object[size]; |
| 61 | |
| 62 | // optimization: use manual for/each to save allocating an iterator here |
| 63 | for (int i = 0; i < size; i++) { |
| 64 | SingleParameterInjector<?> parameterInjector = parameterInjectors[i]; |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 65 | try { |
limpbizkit | e89c49e | 2009-05-06 01:02:14 +0000 | [diff] [blame] | 66 | parameters[i] = parameterInjector.inject(errors, context); |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 67 | } catch (ErrorsException e) { |
| 68 | errors.merge(e.getErrors()); |
| 69 | } |
| 70 | } |
| 71 | |
limpbizkit | 72d11dd | 2008-11-02 07:59:13 +0000 | [diff] [blame] | 72 | errors.throwIfNewErrors(numErrorsBefore); |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 73 | return parameters; |
| 74 | } |
| 75 | } |