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.Member;
20 import java.lang.reflect.Modifier;
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.List;
25 import org.wamblee.collections.CollectionFilter;
26 import org.wamblee.conditions.Condition;
27 import org.wamblee.system.core.RequiredInterface;
28 import org.wamblee.system.core.Scope;
29 import org.wamblee.system.core.SystemAssemblyException;
31 public class ConstructorConfiguration {
33 private Constructor<?> _constructor;
34 private ParameterValues _values;
35 private boolean _publicOnly;
38 * Constructs the configuration. By default no constructor is selected and
39 * one of {@link #select(Class...)} or
40 * {@link #greedy()} must be called.
41 * @param aClass Class to construct.
43 public ConstructorConfiguration(Class aClass) {
50 * Sets whether or no non public constructors are also considered.
51 * Reset the choice of a constructor.
55 public ConstructorConfiguration setNonPublic(boolean aNonPublic) {
56 _publicOnly = !aNonPublic;
63 * Selects an explicit constructor.
64 * @param aTypes Arguments of the constructor.
65 * @return Return the injector to allow call chaining.
67 public ConstructorConfiguration select(Class... aTypes) {
69 _constructor = _class.getDeclaredConstructor(aTypes);
70 } catch (Exception e) {
71 throw new SystemAssemblyException(e.getMessage(), e);
78 * Selects the greediest constructor.
79 * @return The injector to allow call chaining.
80 * @throws SystemAssemblyException if the greediest constructor cannot be uniquely
83 public ConstructorConfiguration greedy() {
84 Constructor<?>[] declared = _class.getDeclaredConstructors();
85 if (declared.length == 0) {
86 throw new SystemAssemblyException("Class '" + _class
87 + " is an interface, primitive type, or array");
90 List<Constructor<?>> checked = new ArrayList<Constructor<?>>();
91 CollectionFilter.filter(Arrays.asList(declared), checked,
92 new Condition<Constructor<?>>() {
94 public boolean matches(Constructor<?> aObject) {
98 return Modifier.isPublic(aObject.getModifiers());
102 for (Constructor ctor : checked) {
103 if (ctor.getParameterTypes().length > max) {
104 max = ctor.getParameterTypes().length;
107 final int max2 = max;
108 List<Constructor<?>> ctors = checked;
109 List<Constructor<?>> longest = new ArrayList<Constructor<?>>();
110 CollectionFilter.filter(ctors, longest,
111 new Condition<Constructor<?>>() {
113 public boolean matches(Constructor<?> aObject) {
114 return aObject.getParameterTypes().length == max2;
117 if (longest.size() > 1) {
118 throw new SystemAssemblyException(
119 "Greediest constructor cannot be uniquely determined");
121 _constructor = longest.get(0);
126 public ParameterValues getParameters() {
127 getConstructor(); // initialize constructor if needed.
134 private void resetValues() {
135 _constructor.setAccessible(true);
136 _values = new ParameterValues(_constructor.getParameterTypes());
140 * Creates the object in the given scope.
141 * @param aScope Scope containing required interfaces for this object.
144 public Object create(Scope aScope) {
145 Object[] values = _values.values(aScope);
147 return getConstructor().newInstance(values);
148 } catch (Exception e) {
149 throw new SystemAssemblyException("Could not construct object "
150 + getConstructor() + " " + Arrays.asList(values), e);
154 public List<RequiredInterface> getRequiredInterfaces() {
155 getConstructor(); // initialize constructor if needed.
156 return _values.getRequiredInterfaces();
159 private Constructor getConstructor() {
160 if (_constructor == null ) {