blob: 075e74e7e7fdc71d2197eb47a4f12d5594de96af [file] [log] [blame]
limpbizkit06898062008-11-02 05:14:55 +00001/**
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
17package com.google.inject;
18
limpbizkit06898062008-11-02 05:14:55 +000019import com.google.inject.internal.Errors;
20import com.google.inject.internal.ErrorsException;
limpbizkit76c24b12008-12-25 04:32:41 +000021import com.google.inject.internal.InternalContext;
22import com.google.inject.internal.InternalFactory;
limpbizkit0de5e3e2008-12-07 08:28:31 +000023import com.google.inject.spi.Dependency;
limpbizkit06898062008-11-02 05:14:55 +000024
25/**
26 * Resolves a single parameter, to be used in a constructor or method invocation.
27 */
28class SingleParameterInjector<T> {
limpbizkit0de5e3e2008-12-07 08:28:31 +000029 private static final Object[] NO_ARGUMENTS = {};
30
limpbizkit06898062008-11-02 05:14:55 +000031 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,
limpbizkite89c49e2009-05-06 01:02:14 +000052 SingleParameterInjector<?>[] parameterInjectors) throws ErrorsException {
limpbizkit06898062008-11-02 05:14:55 +000053 if (parameterInjectors == null) {
limpbizkit0de5e3e2008-12-07 08:28:31 +000054 return NO_ARGUMENTS;
limpbizkit06898062008-11-02 05:14:55 +000055 }
56
limpbizkit72d11dd2008-11-02 07:59:13 +000057 int numErrorsBefore = errors.size();
limpbizkit06898062008-11-02 05:14:55 +000058
limpbizkite89c49e2009-05-06 01:02:14 +000059 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];
limpbizkit06898062008-11-02 05:14:55 +000065 try {
limpbizkite89c49e2009-05-06 01:02:14 +000066 parameters[i] = parameterInjector.inject(errors, context);
limpbizkit06898062008-11-02 05:14:55 +000067 } catch (ErrorsException e) {
68 errors.merge(e.getErrors());
69 }
70 }
71
limpbizkit72d11dd2008-11-02 07:59:13 +000072 errors.throwIfNewErrors(numErrorsBefore);
limpbizkit06898062008-11-02 05:14:55 +000073 return parameters;
74 }
75}