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 java.lang.reflect.Constructor;
19 import java.lang.reflect.Modifier;
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.List;
24 import org.wamblee.collections.CollectionFilter;
25 import org.wamblee.conditions.Condition;
26 import org.wamblee.system.core.RequiredInterface;
27 import org.wamblee.system.core.Scope;
28 import org.wamblee.system.core.SystemAssemblyException;
31 * Class that allows configuration of the constructor to use.
33 * In particular, it provides:
35 * <li> Selection of a constructor using explicit selection
36 * {@link #select(Class...)} or using the most greedy constructor
40 * Selection of methods to invoke to inject other objects into the object.
42 * <li> Selection of fields to set.
46 public class ConstructorConfiguration {
48 private Constructor<?> _constructor;
49 private ParameterValues _values;
50 private boolean _publicOnly;
53 * Constructs the configuration. By default the public constructor with the
54 * most arguments will be used.
55 * @param aClass Class to construct.
57 public ConstructorConfiguration(Class aClass) {
64 * Sets whether or no non public constructors are also considered.
65 * Reset the choice of a constructor to its default.
69 public ConstructorConfiguration setNonPublic(boolean aNonPublic) {
70 _publicOnly = !aNonPublic;
77 * Selects an explicit constructor.
78 * @param aTypes Arguments of the constructor.
79 * @return Return the injector to allow call chaining.
81 public ConstructorConfiguration select(Class... aTypes) {
83 _constructor = _class.getDeclaredConstructor(aTypes);
84 } catch (Exception e) {
85 throw new SystemAssemblyException(e.getMessage(), e);
92 * Selects the greediest constructor.
93 * @return The injector to allow call chaining.
94 * @throws SystemAssemblyException if the greediest constructor cannot be uniquely
97 public ConstructorConfiguration greedy() {
98 Constructor<?>[] declared = _class.getDeclaredConstructors();
99 if (declared.length == 0) {
100 throw new SystemAssemblyException("Class '" + _class
101 + " is an interface, primitive type, or array");
104 List<Constructor<?>> checked = new ArrayList<Constructor<?>>();
105 CollectionFilter.filter(Arrays.asList(declared), checked,
106 new Condition<Constructor<?>>() {
108 public boolean matches(Constructor<?> aObject) {
109 if ( !_publicOnly ) {
112 return Modifier.isPublic(aObject.getModifiers());
116 for (Constructor ctor : checked) {
117 if (ctor.getParameterTypes().length > max) {
118 max = ctor.getParameterTypes().length;
121 final int max2 = max;
122 List<Constructor<?>> ctors = checked;
123 List<Constructor<?>> longest = new ArrayList<Constructor<?>>();
124 CollectionFilter.filter(ctors, longest,
125 new Condition<Constructor<?>>() {
127 public boolean matches(Constructor<?> aObject) {
128 return aObject.getParameterTypes().length == max2;
131 if (longest.size() > 1) {
132 throw new SystemAssemblyException(
133 "Greediest constructor cannot be uniquely determined");
135 _constructor = longest.get(0);
140 public ParameterValues getParameters() {
141 getConstructor(); // initialize constructor if needed.
148 private void resetValues() {
149 _constructor.setAccessible(true);
150 _values = new ParameterValues(_constructor.getParameterTypes());
154 * Creates the object in the given scope.
155 * @param aScope Scope containing required interfaces for this object.
158 public Object create(Scope aScope) {
159 Object[] values = _values.values(aScope);
161 return getConstructor().newInstance(values);
162 } catch (Exception e) {
163 throw new SystemAssemblyException("Could not construct object "
164 + getConstructor() + " " + Arrays.asList(values), e);
168 public List<RequiredInterface> getRequiredInterfaces() {
169 getConstructor(); // initialize constructor if needed.
170 return _values.getRequiredInterfaces();
173 private Constructor getConstructor() {
174 if (_constructor == null ) {