package org.wamblee.system.adapters;
import java.lang.reflect.Constructor;
-import java.lang.reflect.Member;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
* </ul>
*/
public class ConstructorConfiguration {
- private Class _class;
- private Constructor<?> _constructor;
- private ParameterValues _values;
- private boolean _publicOnly;
+ private Class clazz;
+ private Constructor<?> constructor;
+ private ParameterValues values;
+ private boolean publicOnly;
/**
* Constructs the configuration. By default the public constructor with the
* @param aClass Class to construct.
*/
public ConstructorConfiguration(Class aClass) {
- _class = aClass;
- _constructor = null;
- _publicOnly = true;
+ clazz = aClass;
+ constructor = null;
+ publicOnly = true;
}
/**
* @return
*/
public ConstructorConfiguration setNonPublic(boolean aNonPublic) {
- _publicOnly = !aNonPublic;
- _constructor = null;
- _values = null;
+ publicOnly = !aNonPublic;
+ constructor = null;
+ values = null;
return this;
}
*/
public ConstructorConfiguration select(Class... aTypes) {
try {
- _constructor = _class.getDeclaredConstructor(aTypes);
+ constructor = clazz.getDeclaredConstructor(aTypes);
} catch (Exception e) {
throw new SystemAssemblyException(e.getMessage(), e);
}
* identified.
*/
public ConstructorConfiguration greedy() {
- Constructor<?>[] declared = _class.getDeclaredConstructors();
+ Constructor<?>[] declared = clazz.getDeclaredConstructors();
if (declared.length == 0) {
- throw new SystemAssemblyException("Class '" + _class
+ throw new SystemAssemblyException("Class '" + clazz
+ " is an interface, primitive type, or array");
}
int max = -1;
new Condition<Constructor<?>>() {
@Override
public boolean matches(Constructor<?> aObject) {
- if ( !_publicOnly ) {
+ if ( !publicOnly ) {
return true;
} else {
return Modifier.isPublic(aObject.getModifiers());
throw new SystemAssemblyException(
"Greediest constructor cannot be uniquely determined");
}
- _constructor = longest.get(0);
+ constructor = longest.get(0);
resetValues();
return this;
}
public ParameterValues getParameters() {
getConstructor(); // initialize constructor if needed.
- return _values;
+ return values;
}
/**
* Resets the values.
*/
private void resetValues() {
- _constructor.setAccessible(true);
- _values = new ParameterValues(_constructor.getParameterTypes());
+ constructor.setAccessible(true);
+ values = new ParameterValues(constructor.getParameterTypes());
}
/**
* @return object.
*/
public Object create(Scope aScope) {
- Object[] values = _values.values(aScope);
+ Object[] valueArray = values.values(aScope);
try {
- return getConstructor().newInstance(values);
+ return getConstructor().newInstance(valueArray);
} catch (Exception e) {
throw new SystemAssemblyException("Could not construct object "
- + getConstructor() + " " + Arrays.asList(values), e);
+ + getConstructor() + " " + Arrays.asList(valueArray), e);
}
}
public List<RequiredInterface> getRequiredInterfaces() {
getConstructor(); // initialize constructor if needed.
- return _values.getRequiredInterfaces();
+ return values.getRequiredInterfaces();
}
private Constructor getConstructor() {
- if (_constructor == null ) {
+ if (constructor == null ) {
greedy();
}
- return _constructor;
+ return constructor;
}
}