IBM InfoSphere Streams Version 4.1.0

Developing Java native functions

You create Java™ native functions by adding Java annotations that describe Java public static methods as native functions to the SPL compiler.

You compile the Java source code and index the toolkit by running the spl-make-toolkit command or the sc command. Then, InfoSphere® Streams creates the necessary toolkit artifacts that enable SPL code to call the Java method directly with the same syntax that is used to call an SPL function.

To define a Java public static method as a native function, add the @Function annotation to a public static method in the Java source code.

In Java, the following code is an example of a definition for the function pow:
@Function (namespace=”spl.math.java”)
public static double pow(double x, double y) {
   return Math.pow(x, y);
}

In the previous Java example, the @Function annotation has the optional namespace attribute. When the namespace attribute is omitted, SPL gets the values for that attribute from the value of the @Namespace annotation for the package of the annotated method, if it exists. Otherwise, SPL gets the values for the namespace attribute from the package name of the annotated method.

In SPL, calls to Java native functions look like calls to other functions, whether those functions are SPL functions or native functions. The following SPL code calls the pow function:
use spl.math.java::*;
float64 res = pow(1.5, 3.1);
In the following Java example, the @Function annotation has the optional name and description attributes.
public class JavaFunctions {
  @Function(name="getUser", description="Return value of USER environment variable")
  public static String getUser() {
    return System.getenv("USER");
}

When the Java compiler compiles the Java source, the annotation processing creates a nested class for the class that contains the annotated method. The nested class is named class_name$StreamsModel. When the toolkit index is rebuilt, SPL artifacts are created under the SPL_namespace/native.function directory.

The generated SPL includes the following artifacts:
  • The function model file, javaFunction.xml, for the Java native functions in the namespace
  • A C++ header file, which contains code to call the Java methods from SPL
Because these artifacts are generated automatically, they are read-only.

For more information about creating a Java native function, see the API documentation for the com.ibm.streams.function.model package.