|
|
||||||||||||||||||||||||
Calling Java MethodsThese sections show how to call Java methods. CommonThe 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 following is a table of how each type maps to its signature character:
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 CallsFor 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.
|
© 1998 Microsoft Corporation. All rights reserved. Terms of use. |