* @param params the parameters
* @param args the arguments
*/
public void declareFunctionLocals(String functionName, SimpleNode params, Object[] args) throws Exception
{
NametableStack ntStack = getCurrentContext().getNametableStack();
int n = params.jjtGetNumChildren();
Map defaults = new HashMap();
for (int i = 0; i < args.length; i++)
{
if (args[i] instanceof AstNamedArgument)
{
SimpleNode arg = (SimpleNode) args[i];
Object value = arg.jjtGetChild(0).jjtAccept(this, null);
defaults.put(arg.getData(), value);
}
}
if (params instanceof AstFormalParameters)
{
for (int i = 0; i < n; i++)
{
AstFormalParameter param = (AstFormalParameter) params.jjtGetChild(i);
AstVariableDeclaratorId id = (AstVariableDeclaratorId) param.jjtGetChild(1);
AstType type = (AstType) param.jjtGetChild(0);
Class paramType = (Class) type.jjtAccept(this, null);
if (param.jjtGetNumChildren() == 2)
{//no defaults
if (args.length <= i)
{
throw new IllegalArgumentException("Mismatch number of arguments");
} else if (args[i] instanceof AstNamedArgument)
{
throw new IllegalArgumentException("Expecting required paramater "
+ id.getData() + " for function " + functionName);
} else
{
if (_checkFunctionArgumentType)
{//do type checking
if (paramType.isPrimitive())
{
if (args[i] == null)
{
throw new IllegalArgumentException("Primitive argument " + id.getData() + " cannot be null");
} else if (!ReflectUtils.isPrimitiveInstance(paramType, args[i]))
{
throw new IllegalArgumentException("Mismatch argument type for " + id.getData() + ". Expecting "
+ paramType.getName());
}
} else if (args[i] != null)
{
if (!paramType.isInstance(args[i]))
{
throw new IllegalArgumentException("Mismatch argument type for " + id.getData()
+ ". Expecting " + paramType.getName());
}
}
}
ntStack.declare((String) id.getData(), args[i]);
}
} else
{//has default
if (defaults.containsKey(id.getData()))
{
Object value = defaults.remove(id.getData());
ntStack.declare((String) id.getData(), value);
} else
{
if (i >= args.length || args[i] instanceof AstNamedArgument)
{
Object value = param.jjtGetChild(2).jjtAccept(this, null);
ntStack.declare((String) id.getData(), value);
} else
{
ntStack.declare((String) id.getData(), args[i]);
}
}
}
}
if (defaults.size() > 0)
{
throw new IllegalArgumentException("Unknown default " + defaults + " for function " + functionName);
}
} else
{//assume variable parameters (varargs)
ntStack.declare((String) params.getData(), args);
}
}