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());
129 for (Constructor ctor : checked) {
130 if (ctor.getParameterTypes().length > max) {
131 max = ctor.getParameterTypes().length;
135 final int max2 = max;
136 List<Constructor<?>> ctors = checked;
137 List<Constructor<?>> longest = new ArrayList<Constructor<?>>();
138 CollectionFilter.filter(ctors, longest,
139 new Condition<Constructor<?>>() {
141 public boolean matches(Constructor<?> aObject) {
142 return aObject.getParameterTypes().length == max2;
146 if (longest.size() > 1) {
147 throw new SystemAssemblyException(
148 "Greediest constructor cannot be uniquely determined");
151 constructor = longest.get(0);
157 public ParameterValues getParameters() {
158 getConstructor(); // initialize constructor if needed.
166 private void resetValues() {
167 constructor.setAccessible(true);
168 values = new ParameterValues(constructor.getParameterTypes());
172 * Creates the object in the given scope.
175 * Scope containing required interfaces for this object.
180 public Object create(Scope aScope) {
181 Object[] valueArray = values.values(aScope);
184 return getConstructor().newInstance(valueArray);
185 } catch (Exception e) {
186 throw new SystemAssemblyException("Could not construct object " +
187 getConstructor() + " " + Arrays.asList(valueArray), e);
191 public List<RequiredInterface> getRequiredInterfaces() {
192 getConstructor(); // initialize constructor if needed.
194 return values.getRequiredInterfaces();
197 private Constructor getConstructor() {
198 if (constructor == null) {