Iterator it = args.iterator();
int normalArgCnt = isVarargs ? typesLen - 1 : typesLen;
int argIdx = 0;
while (argIdx < normalArgCnt) {
Class argType = argTypes[argIdx];
TemplateModel argVal = (TemplateModel) it.next();
Object unwrappedArgVal = w.tryUnwrap(argVal, argType);
if(unwrappedArgVal == BeansWrapper.CAN_NOT_UNWRAP) {
throw createArgumentTypeMismarchException(argIdx, argVal, argType);
}
if (unwrappedArgVal == null && argType.isPrimitive()) {
throw createNullToPrimitiveArgumentException(argIdx, argType);
}
unwrappedArgs[argIdx++] = unwrappedArgVal;
}
if (isVarargs) {
// The last argType, which is the vararg type, wasn't processed yet.
Class varargType = argTypes[typesLen - 1];
Class varargItemType = varargType.getComponentType();
if (!it.hasNext()) {
unwrappedArgs[argIdx++] = Array.newInstance(varargItemType, 0);
} else {
TemplateModel argVal = (TemplateModel) it.next();
Object unwrappedArgVal;
// We first try to treat the last argument as a vararg *array*.
// This is consistent to what OverloadedVarArgMethod does.
if (argsLen - argIdx == 1
&& (unwrappedArgVal = w.tryUnwrap(argVal, varargType))
!= BeansWrapper.CAN_NOT_UNWRAP) {
// It was a vararg array.
unwrappedArgs[argIdx++] = unwrappedArgVal;
} else {
// It wasn't a vararg array, so we assume it's a vararg
// array *item*, possibly followed by further ones.
int varargArrayLen = argsLen - argIdx;
Object varargArray = Array.newInstance(varargItemType, varargArrayLen);
for (int varargIdx = 0; varargIdx < varargArrayLen; varargIdx++) {
TemplateModel varargVal = (TemplateModel) (varargIdx == 0 ? argVal : it.next());
Object unwrappedVarargVal = w.tryUnwrap(varargVal, varargItemType);
if(unwrappedVarargVal == BeansWrapper.CAN_NOT_UNWRAP) {
throw createArgumentTypeMismarchException(
argIdx + varargIdx, varargVal, varargItemType);
}