convertArguments static method
arguments converted with converter to the parameter types of
function where they do not fit, like Spring's
ReflectionHelper.convertAllArguments; the list is returned as it is
when nothing changes: #half(3) with a double
parameter gets 3.0, 'abc'.substring('1') gets 1,
'abc'.charAt(1.0) gets 1. Dart has no reflection, so the
parameter types are probed: int, double, num, String and
bool parameters of the first maxConvertedArity positional
parameters convert; an argument that cannot be converted is passed
as it is (the call then fails as usual).
Implementation
static List<Object?> convertArguments(
Object function,
List<Object?> arguments,
TypeConverter converter,
) {
final (Function target, int count, int converted) = switch (function) {
VarargsFunction(:final function, :final fixedArity) => (
function,
fixedArity + 1,
fixedArity,
),
final Function function => (function, arguments.length, arguments.length),
_ => (_noFunction, 0, 0),
};
if (count > maxConvertedArity || converted > arguments.length) {
return arguments;
}
List<Object?>? result;
for (var i = 0; i < converted; i++) {
final value = arguments[i];
if (value == null || _accepts<Object?>(target, count, i)) {
continue;
}
final Type? type;
if (_accepts<int>(target, count, i) &&
_accepts<double>(target, count, i)) {
type = value is num ? null : num;
} else if (_accepts<double>(target, count, i)) {
type = value is double ? null : double;
} else if (_accepts<int>(target, count, i)) {
type = value is int ? null : int;
} else if (_accepts<String>(target, count, i)) {
type = value is String ? null : String;
} else if (_accepts<bool>(target, count, i)) {
type = value is bool ? null : bool;
} else {
type = null;
}
if (type == null) {
continue;
}
try {
final convertedValue = type == num
? _toNum(value, converter)
: converter.convertValue(value, type);
(result ??= [...arguments])[i] = convertedValue;
} on Exception {
// Not convertible: the call reports the mismatch.
}
}
return result ?? arguments;
}