Returns a function that invokes the specified Java method
mName in class cName. The specified method
invoked is indentified by the parameter types pType.... The
class name cName is specified as a string containing the
fully qualified class name, the method name is specified as a string
containing the mathod name, while the parameter types are specified as
strings containing the fully qualified class name or primitive type of
the parameter.
Consider the following example, which imports the getFile
method in the Java URL class. There is only one version of this method
and it takes no arguments. e.g.
:-) javaFunction(
:-) "java.net.URL",
:-) "getFile"
:-) );
There is 1 result
<function getFile>
The number of arguments required by the returned function depends on the
definition of the Java method. If the Java method is defined as
static, the returned function will require the same number of
arguments as the Java method. If the Java method is an instance
method, the returned function will require an extra argument. This extra
argument must be an instance of the class specified as the first
argument to the javaFunction function. Phew! To help make
this easier to understand, see the subsection on Static vs. Instance method
examples.
In order to make use of this function, we must assign it to a variable
and provide is with a java.net.URL instance. We can obtain
a java.net.URL instance by using the javaConstructor function, e.g.
:-) var newJavaURL = javaConstructor(
:-) "java.net.URL",
:-) "java.lang.String",
:-) "java.lang.String",
:-) "int",
:-) "java.lang.String"
:-) );
There are 0 results
:-) var url = newJavaURL( "http", "www.sourceforge.net", 80, "/projects/millscript/" );
There are 0 results
:-) var getFile = javaFunction( "java.net.URL", "getFile" );
There are 0 results
:-) url.getFile;
There is 1 result
"/projects/millscript/"
To illustrate the difference between the way static and instance methods are handled in MillScript, lets have a look at two simple examples(Well as simple as this can get!).
The static method I want to use is valueOf from
the java.lang.Integer class. In particular I want to use
the version of valueOf that takes a single
java.lang.String as its argument. To import this method
we use:
:-) javaFunction( "java.lang.Integer", "valueOf", "java.lang.String" );
There is 1 result
<function valueOf>
As this is a static method, we do not need to supply an
instance of java.lang.Integer in order to apply it. Hence
we can do the following:
:-) var valueOf = javaFunction( "java.lang.Integer", "valueOf", "java.lang.String" );
There are 0 results
:-) valueOf( "445874588" );
There is 1 result
445874588
In the above example it's worth noting that we had to supply an
instance of java.lang.String as the single argument to
the valueOf function. We didn't need to do anything
special to achieve this because all MillScript strings are
java.lang.Strings! The final value returned is a
java.lang.Integer object, so it can be used anywhere a
MillScript integer can.
Now for the instance example. This time I want to use the
indexOf operator from the java.lang.String
class. Again, specifically I want the version that takes a single
java.lang.String as it's argument. To import this method
we use:
:-) javaFunction( "java.lang.String", "indexOf", "java.lang.String" );
There is 1 result
<function indexOf>
Unfortunately this looks very similar to the valueOf
example above, so you might expect to be able to apply the returned
function with a single java.lang.Sting as its argument.
Lets see what happens:
:-) var indexOf = javaFunction( "java.lang.String", "indexOf", "java.lang.String" );
There are 0 results
:-) indexOf( "this is a java.lang.String" );
MISHAP : Too few arguments when calling function
BECAUSE : Exactly 2 required
FUNCTION : <function indexOf>
ARG (1) : this is a java.lang.String
DOING : <function indexOf>
LINE NO. : 39
PHASE : Evaluation
Trapped : org.millscript.millscript.Alert$EscapeException
Resetting input and resuming execution
As you can see this didn't work. The indexOf method is an
instance method, so it needs an instance of it's containing
class to dispatch on. In the case of an instance method, we
must supply a class instance as the first argument to the function in
MillScript. In our example the containing class is
java.lang.String, so things are kept nice and simple,
e.g.
:-) var indexOf = javaFunction( "java.lang.String", "indexOf", "java.lang.String" );
There are 0 results
:-) "this is a java.lang.String".indexOf( "java.lang.String" );
There is 1 result
10
Hence, the first index of "java.lang.String" in
"this is a java.lang.String" is 10. Unfortunately my
example exposes a fundamental difference between Java and MillScript:
Java uses zero based indexing, while MillScript uses one based.
Although this function returns 10, which is fine in Java, the correct
answer for MillScript is 11! It should go without saying that with
this kind of power comes responsibility! You could create a lot of
problems for yourself when using this functionality.