Parsing function arguments
This tutorial shows how to parse arguments passed to an extension function.
1. Add the following Java™ code to the BigDecimalExtension class:
@XAPIFunction("add_bigdecimal")
public void add(RuntimeContext ctxt) {
RuntimeServices runtimeServices = getRuntimeServices();
InvocationService invocation = runtimeServices.getInvocationService();
Object[] arguments = invocation.parseArguments(ctxt, 1,"ss", false);
BigDecimal arg1 = new BigDecimal(((
XAPIString) arguments[0]).getString());
BigDecimal arg2 = new BigDecimal(((
XAPIString) arguments[1]).getString());
BigDecimal result = arg1.add(arg2);
ctxt.setReturnValue(result.toString());
}
2 Change the sample.php code to exercise the new function.
<?php $arg0 = '111111111111111'; $arg1 = '222222222222222'; $result = add_bigdecimal($arg0, $arg1); echo 'The answer is: '.$result; ?>
3. Run the IBM® WebSphere® sMash application and point a Web browser at http://localhost:8080/sample.php.
4. Now change sample.php to make an incorrect call to add_bigdecimal.
<?php $result = add_bigdecimal($arg0); ?>
5. The Web browser displays an error messaging explaining that too few arguments were passed to the extension function.
The example uses the parseArguments method on the InvocationService interface. The string passed to
the method is called the type specifier and defines the number and type of arguments expected (s means string
in this context).