in xstream/src/java/com/thoughtworks/xstream/core/util/DependencyInjectionFactory.java [86:262]
public static <T> T newInstance(final BitSet usedDependencies, final Class<T> type, final Object... dependencies) {
if (dependencies != null && dependencies.length > 63) {
throw new IllegalArgumentException("More than 63 arguments are not supported");
}
Constructor<?> bestMatchingCtor = null;
final ArrayList<Object> matchingDependencies = new ArrayList<>();
List<Object> possibleMatchingDependencies = null;
long usedDeps = 0;
long possibleUsedDeps = 0;
if (dependencies != null && dependencies.length > 0) {
// sort available ctors according their arity
final Constructor<?>[] ctors = type.getConstructors();
if (ctors.length > 1) {
Arrays.sort(ctors, new Comparator<Constructor<?>>() {
@Override
public int compare(final Constructor<?> o1, final Constructor<?> o2) {
return o2.getParameterTypes().length - o1.getParameterTypes().length;
}
});
}
final TypedValue[] typedDependencies = new TypedValue[dependencies.length];
for (int i = 0; i < dependencies.length; i++) {
Object dependency = dependencies[i];
Class<?> depType = dependency.getClass();
if (depType.isPrimitive()) {
depType = Primitives.box(depType);
} else if (depType == TypedNull.class) {
depType = ((TypedNull<?>)dependency).getType();
dependency = null;
}
typedDependencies[i] = new TypedValue(depType, dependency);
}
Constructor<?> possibleCtor = null;
int arity = Integer.MAX_VALUE;
for (int i = 0; bestMatchingCtor == null && i < ctors.length; i++) {
final Constructor<?> constructor = ctors[i];
final Class<?>[] parameterTypes = constructor.getParameterTypes();
if (parameterTypes.length > dependencies.length) {
continue;
} else if (parameterTypes.length == 0) {
if (possibleCtor == null) {
bestMatchingCtor = constructor;
}
break;
}
if (arity > parameterTypes.length) {
if (possibleCtor != null) {
continue;
}
arity = parameterTypes.length;
}
for (int j = 0; j < parameterTypes.length; j++) {
if (parameterTypes[j].isPrimitive()) {
parameterTypes[j] = Primitives.box(parameterTypes[j]);
}
}
// first approach: test the ctor params against the dependencies in the sequence of the parameter
// declaration
matchingDependencies.clear();
usedDeps = 0;
for (int j = 0, k = 0; j < parameterTypes.length
&& parameterTypes.length + k - j <= typedDependencies.length; k++) {
if (parameterTypes[j].isAssignableFrom(typedDependencies[k].type)) {
matchingDependencies.add(typedDependencies[k].value);
usedDeps |= 1L << k;
if (++j == parameterTypes.length) {
bestMatchingCtor = constructor;
break;
}
}
}
if (bestMatchingCtor == null) {
boolean possible = true; // assumption
// try to match all dependencies in the sequence of the parameter declaration
final TypedValue[] deps = new TypedValue[typedDependencies.length];
System.arraycopy(typedDependencies, 0, deps, 0, deps.length);
matchingDependencies.clear();
usedDeps = 0;
for (final Class<?> parameterType : parameterTypes) {
int assignable = -1;
for (int k = 0; k < deps.length; k++) {
if (deps[k] == null) {
continue;
}
if (deps[k].type == parameterType) {
assignable = k;
// optimal match
break;
} else if (parameterType.isAssignableFrom(deps[k].type)) {
// use most specific type
if (assignable < 0
|| deps[assignable].type != deps[k].type
&& deps[assignable].type.isAssignableFrom(deps[k].type)) {
assignable = k;
}
}
}
if (assignable >= 0) {
matchingDependencies.add(deps[assignable].value);
usedDeps |= 1L << assignable;
deps[assignable] = null; // do not match same dep twice
} else {
possible = false;
break;
}
}
if (possible) {
// the smaller the value, the smaller the indices in the deps array
if (possibleCtor != null && usedDeps >= possibleUsedDeps) {
continue;
}
possibleCtor = constructor;
@SuppressWarnings("unchecked")
final List<Object> clone = (List<Object>)matchingDependencies.clone();
possibleMatchingDependencies = clone;
possibleUsedDeps = usedDeps;
}
}
}
if (bestMatchingCtor == null) {
if (possibleCtor == null) {
usedDeps = 0;
final ObjectAccessException ex = new ObjectAccessException(
"Cannot construct type, none of the arguments match any constructor's parameters");
ex.add("construction-type", type.getName());
throw ex;
} else {
bestMatchingCtor = possibleCtor;
matchingDependencies.clear();
matchingDependencies.addAll(possibleMatchingDependencies);
usedDeps = possibleUsedDeps;
}
}
}
Throwable th = null;
try {
final T instance;
if (bestMatchingCtor == null) {
instance = type.newInstance();
} else {
@SuppressWarnings("unchecked")
final T obj = (T)bestMatchingCtor.newInstance(matchingDependencies.toArray());
instance = obj;
}
if (usedDependencies != null) {
usedDependencies.clear();
int i = 0;
for (long l = 1; l < usedDeps; l <<= 1, ++i) {
if ((usedDeps & l) > 0) {
usedDependencies.set(i);
}
}
}
return instance;
} catch (final InstantiationException | IllegalAccessException | SecurityException | ExceptionInInitializerError e) {
th = e;
} catch (final InvocationTargetException e) {
th = e.getCause();
}
final ObjectAccessException ex = new ObjectAccessException("Cannot construct type", th);
ex.add("construction-type", type.getName());
throw ex;
}