Introduction to Using the Raw Native Interface
 In this topic

*Common

*Repeated Calls

 

Java & Native Code    PreviousRNINext
Introduction to Using the Raw Native Interface     Previous RNI Next

 


Calling Java Methods

These sections show how to call Java methods.

Common

The most common method for calling a Java API from native code is execute_java_dynamic_method (see also execute_java_dynamic_method64 and execute_java_dynamic_methodV). The following example shows how to call move on a Rectangle object:

    execute_java_dynamic_method(NULL, phRectangle, "move", "(II)V", x, y);

The parameters should be as follows:

  • The first should always be NULL
  • The second is the object whose method you want to call
  • The third is the name of the method
  • The fourth is its signature
  • Finally, pass the parameters. The signature specifies the types of the parameters surrounded by parentheses followed by the return type. In this case, "(II)V" translates to "param 1 is an integer, param 2 is an integer, and the return type is void."

The following is a table of how each type maps to its signature character:
Parameter | Signature Char
array [
boolean Z
byte B
char C
double D
float F
int I
long J
object L
short S
void V

For arrays, the signature char is followed the signature char of the type of the array. For example, an array of bytes would be "[B".

For objects, the signature is followed by the object class name and ends with a semicolon(;). For example, the signature for a Rectangle is "Ljava/awt/Rectangle;".

To call a static method, you can use execute_java_static_method (execute_java_static_method64 or execute_java_static_methodV). For example, to call System.gc(), write the following:

    ClassClass *pcc = FindClass(NULL, "java/lang/System", FALSE); 
    execute_java_static_method(NULL, pcc, "gc", "()V");

FindClass() is used to get a pointer to the class object. The first parameter should be NULL, the second is the class name, and the third should be FALSE. You can call execute_java_static_method with the returned class pointer. The first parameter should be NULL, the second is the class pointer, the third is the method name, the fourth is the method signature. Finally call any arguments (but in this case, there aren't any).

Repeated Calls

For performing repeated calls to the same method of a particular object, you can use the more low-level do_execute_java_method API to avoid repeated name-lookups. The following is an example of to move on a Rectangle 10 times:

    struct methodblock *pmb = get_methodblock(phRectangle, "move", "(II)V");

    for (i = 0; i < 10; i++)
    {
        do_execute_java_method(NULL, phRectangle, NULL, NULL, pmb, FALSE, x, y);
    }

get_methodblock is used to get a pointer to a cached data structure that represents the object and the method. The first parameter is the object pointer, the second is the method name, and the third is the method signature. You can call execute_java_static_method with the returned methodblock pointer. The first parameter should be NULL, the second is the object pointer, the third and fourth parameters should also be NULL, the fifth should be TRUE for a static call and FALSE for a non-static call. Finally, pass any arguments.

Top © 1998 Microsoft Corporation. All rights reserved. Terms of use.