2 * Copyright 2008 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.system.adapters;
18 import org.wamblee.collections.CollectionFilter;
20 import org.wamblee.conditions.Condition;
22 import org.wamblee.system.core.RequiredInterface;
23 import org.wamblee.system.core.Scope;
24 import org.wamblee.system.core.SystemAssemblyException;
26 import java.lang.reflect.Constructor;
27 import java.lang.reflect.Modifier;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.List;
35 * Class that allows configuration of the constructor to use. In
36 * particular, it provides:
38 * <li>Selection of a constructor using explicit selection {@link
39 * #select(Class...)} or using the most greedy constructor {@link
41 * <li>Selection of methods to invoke to inject other objects into
43 * <li>Selection of fields to set.</li>
46 public class ConstructorConfiguration {
55 private Constructor<?> constructor;
60 private ParameterValues values;
65 private boolean publicOnly;
68 * Constructs the configuration. By default the public constructor with the
69 * most arguments will be used.
70 * @param aClass Class to construct.
72 public ConstructorConfiguration(Class aClass) {
79 * Sets whether or no non public constructors are also considered.
80 * Reset the choice of a constructor to its default.
86 public ConstructorConfiguration setNonPublic(boolean aNonPublic) {
87 publicOnly = !aNonPublic;
95 * Selects an explicit constructor.
97 * @return Return the injector to allow call chaining.
99 * @throws SystemAssemblyException DOCUMENT ME!
101 public ConstructorConfiguration select(Class... aTypes) {
103 constructor = clazz.getDeclaredConstructor(aTypes);
104 } catch (Exception e) {
105 throw new SystemAssemblyException(e.getMessage(), e);
114 * Selects the greediest constructor.
116 * @return The injector to allow call chaining.
118 * @throws SystemAssemblyException if the greediest constructor cannot be
119 * uniquely identified.
121 public ConstructorConfiguration greedy() {
122 Constructor<?>[] declared = clazz.getDeclaredConstructors();
124 if (declared.length == 0) {
125 throw new SystemAssemblyException("Class '" + clazz
126 + " is an interface, primitive type, or array");
130 List<Constructor<?>> checked = new ArrayList<Constructor<?>>();
131 CollectionFilter.filter(Arrays.asList(declared), checked,
132 new Condition<Constructor<?>>() {
134 public boolean matches(Constructor<?> aObject) {
138 return Modifier.isPublic(aObject.getModifiers());
143 for (Constructor ctor : checked) {
144 if (ctor.getParameterTypes().length > max) {
145 max = ctor.getParameterTypes().length;
149 final int max2 = max;
150 List<Constructor<?>> ctors = checked;
151 List<Constructor<?>> longest = new ArrayList<Constructor<?>>();
152 CollectionFilter.filter(ctors, longest,
153 new Condition<Constructor<?>>() {
155 public boolean matches(Constructor<?> aObject) {
156 return aObject.getParameterTypes().length == max2;
160 if (longest.size() > 1) {
161 throw new SystemAssemblyException(
162 "Greediest constructor cannot be uniquely determined");
165 constructor = longest.get(0);
171 public ParameterValues getParameters() {
172 getConstructor(); // initialize constructor if needed.
180 private void resetValues() {
181 constructor.setAccessible(true);
182 values = new ParameterValues(constructor.getParameterTypes());
186 * Creates the object in the given scope.
188 * @param aScope Scope containing required interfaces for this object.
192 * @throws SystemAssemblyException DOCUMENT ME!
194 public Object create(Scope aScope) {
195 Object[] valueArray = values.values(aScope);
198 return getConstructor().newInstance(valueArray);
199 } catch (Exception e) {
200 throw new SystemAssemblyException("Could not construct object "
201 + getConstructor() + " " + Arrays.asList(valueArray), e);
205 public List<RequiredInterface> getRequiredInterfaces() {
206 getConstructor(); // initialize constructor if needed.
208 return values.getRequiredInterfaces();
211 private Constructor getConstructor() {
212 if (constructor == null) {