2 * Copyright 2005-2010 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;
34 * Class that allows configuration of the constructor to use. In particular, it
37 * <li>Selection of a constructor using explicit selection
38 * {@link #select(Class...)} or using the most greedy constructor
39 * {@link #greedy()}.</li>
40 * <li>Selection of methods to invoke to inject other objects into the object.</li>
41 * <li>Selection of fields to set.</li>
44 public class ConstructorConfiguration {
47 private Constructor<?> constructor;
49 private ParameterValues values;
51 private boolean publicOnly;
54 * Constructs the configuration. By default the public constructor with the
55 * most arguments will be used.
60 public ConstructorConfiguration(Class aClass) {
67 * Sets whether or no non public constructors are also considered. Reset the
68 * choice of a constructor to its default.
74 public ConstructorConfiguration setNonPublic(boolean aNonPublic) {
75 publicOnly = !aNonPublic;
83 * Selects an explicit constructor.
85 * @return Return the injector to allow call chaining.
88 public ConstructorConfiguration select(Class... aTypes) {
90 constructor = clazz.getDeclaredConstructor(aTypes);
91 } catch (Exception e) {
92 throw new SystemAssemblyException(e.getMessage(), e);
101 * Selects the greediest constructor.
103 * @return The injector to allow call chaining.
105 * @throws SystemAssemblyException
106 * if the greediest constructor cannot be uniquely identified.
108 public ConstructorConfiguration greedy() {
109 Constructor<?>[] declared = clazz.getDeclaredConstructors();
111 if (declared.length == 0) {
112 throw new SystemAssemblyException("Class '" + clazz +
113 " is an interface, primitive type, or array");
117 List<Constructor<?>> checked = new ArrayList<Constructor<?>>();
118 CollectionFilter.filter(Arrays.asList(declared), checked,
119 new Condition<Constructor<?>>() {
121 public boolean matches(Constructor<?> aObject) {
125 return Modifier.isPublic(aObject.getModifiers());
130 for (Constructor ctor : checked) {
131 if (ctor.getParameterTypes().length > max) {
132 max = ctor.getParameterTypes().length;
136 final int max2 = max;
137 List<Constructor<?>> ctors = checked;
138 List<Constructor<?>> longest = new ArrayList<Constructor<?>>();
139 CollectionFilter.filter(ctors, longest,
140 new Condition<Constructor<?>>() {
142 public boolean matches(Constructor<?> aObject) {
143 return aObject.getParameterTypes().length == max2;
147 if (longest.size() > 1) {
148 throw new SystemAssemblyException(
149 "Greediest constructor cannot be uniquely determined");
152 constructor = longest.get(0);
158 public ParameterValues getParameters() {
159 getConstructor(); // initialize constructor if needed.
167 private void resetValues() {
168 constructor.setAccessible(true);
169 values = new ParameterValues(constructor.getParameterTypes());
173 * Creates the object in the given scope.
176 * Scope containing required interfaces for this object.
181 public Object create(Scope aScope) {
182 Object[] valueArray = values.values(aScope);
185 return getConstructor().newInstance(valueArray);
186 } catch (Exception e) {
187 throw new SystemAssemblyException("Could not construct object " +
188 getConstructor() + " " + Arrays.asList(valueArray), e);
192 public List<RequiredInterface> getRequiredInterfaces() {
193 getConstructor(); // initialize constructor if needed.
195 return values.getRequiredInterfaces();
198 private Constructor getConstructor() {
199 if (constructor == null) {