.JavaMethod
Call a Java Static Method

Description

Invokes a static method for a given Java class.

Usage

.JavaMethod(X, METHOD, SIGNATURE, ...)

Arguments

X A string specifying a full Java classname, such as "java/lang/Math". Either a slash or a period can separate the components of the class name.
METHOD A string naming a static method within the class.
SIGNATURE A string describing the method argument types and the return type.
... Zero or more arguments to be passed to the Java method.

Details

The SIGNATURE argument is a "JNI method descriptor" string that specifies the types of the method arguments and the method return type. Specifically, a SIGNATURE string looks like the following: "(<first-arg-type><second-arg-type>...)<return-value-type>" where each "type" can be one of the following:
Thus, the signature "(DD)D" specifies a method that takes two double values and returns a double value. The signature "(Ljava/lang/String;)V" specifies a method that takes a string argument and has no return value.
Each of the above types (except "V") can be proceeded by "[" to indicate that the argument or return value is a Java array of the following type. Thus, the signature "([DI)[Ljava/lang/String;" specifies a method that takes a double array and a scalar integer and returns an array of String values. The .JavaMethod function gives an error if a vector with more or less than one element is passed to a scalar Java argument.
Spotfire Enterprise Runtime for R cannot directly represent all of the primitive data types in Java. Therefore, returned "int" and "short" and "char" values are all returned as integers, and returned "long" and "double" values are both returned as doubles. Sending NA argument values to Java typically does not produce NA values in Java, except for doubles. An NA string value (NA_character_) is transferred to Java as a "null" value and is converted to an NA string on return.
The .JavaMethod function is limited: It can call only static Java methods, and it supports only a small number of argument types. However, it is possible to use this function in combination with additional Java code to perform more complex operations. For example, if you need to call a non-static method on a Java object, you could use .JavaMethod to call a static method that creates the object and saves it in a static variable, and then call another static method to perform an operation on the saved object.
Another problem is transferring a more complex data object, such as a data frame, from Spotfire Enterprise Runtime for R to Java. In this case, you could send each column of the data frame via a call to .JavaMethod to a Java static method that collects the data columns in an appropriate Java object.
The limitations of the .JavaMethod function are removed in the .JavaCall function, which provides a way to create references to Java objects, and call non-static methods on them.
The .JavaAttachClassPath function allows adding additional Java class files to be searched by .JavaMethod.
Value
returns the value returned from the Java method. This is NULL if the Java Method has no return value (void).
See Also
.JavaAttachClassPath, .JavaCall
Examples
# call a Java static method taking two doubles,
# and returning the double value 1024
.JavaMethod("java/lang/Math", "pow", "(DD)D", 2, 10)

# have string as input and output .JavaMethod("java.lang.System", "getProperty", "(Ljava/lang/String;)Ljava/lang/String;", "java.version")

# return logical value .JavaMethod("java.lang.Double", "isNaN", "(D)Z", NaN)

Package terrJava version 6.1.4-13
Package Index