Reflective Style Access to Scripted Methods

The following examples show how to work with BeanShell methods dynamically
from within scripts, using the equivalent of reflective style access in Java.
This is an advanced topic primarily of interest to developers who wish to
do tight integration of BeanShell scripts with their application environment.

Arguments are passed as an array of objects. Primitive types must be
wrapped in their appropriate wrappers.
BeanShell will select among overloaded methods using the standard Java
method resolution rules. (JLS 15.11.2).

The previous section showed how to invoke a method by name when we know
the argument types. Of course, in general we'd like to be able to find out
what methods are defined in the current script or to look up a method by
its signature.

You can get "handles" to all of the methods defined in a context using the
namespace getMethods() method.
getMethods() returns an array of bsh.BshMethod objects,
which are wrappers for the internally parsed representation of BeanShell
scripted methods:

In the above snippet we located the bar() method by its signature. If there
had been overloaded forms of bar() getMethod() would have located the most
specific one according to the standard Java method resolution rules
(JLS 15.11.2).
The result of the lookup is a bsh.BshMethod object, as before.

For the
interpreter and callstack references you can simply pass along the current
context's values via 'this.interpreter' and 'this.callstack', as we did
above. The arguments array may be null or empty for no arguments.

Why would anyone want to do this? Well, perhaps you are sourcing a script
created by a user and want to automatically begin using methods that they
have defined. Perhaps the user is allowed to define methods to take control
of various aspects of your application. With the tools we've described
in this section you can list the methods they have defined and invoke
them dynamically.