Java Reference
In-Depth Information
Table 22.3
JNI functions to call Java methods.
Java return
C/C++ return
JNI function
type
type
name
boolean
jboolean
CallBooleanMethod()
byte
Jbyte
CallByteMethod()
char
Jchar
CallCharMethod()
short
jshort
CallShortMethod()
int
Jint
CallIntMethod()
long
Jlong
CallLongMethod()
float
jfloat
CallFloatMethod()
double
jdouble
CallDoubleMethod()
Object
jobject
CallObjectMethod()
22.9.2 Calling Java methods using the method ID
With a method ID in hand, we finally can call the Java method from native code.
Since the Java callback() method we wish to call returns a Java int ,weuse
the JNI function CallIntMethod() as follows:
// Create a jint to pass to the ' callback ' method
jint param = 8;
// Call the ' callback ' method, placing the return in a jint
// named ret
jint ret = jenv- > CallIntMethod (jo, callback - mid, param);
/**** NOTE: exception handling code needed here ****/
// Print the returned value
printf (" return from 'callback'=%d\n", ret);
The arguments to CallIntMethod() are the jobject reference for the object
whose method is to be called, the method ID, and then the actual parameters
required by the Java method being called. This function can throw any exception
that the called Java method can throw. We have omitted the exception handling
code until we discuss exceptions in the next section. The JNIDemo application
on the Web Course demonstrates this technique in a complete working example.
There are corresponding CallXxxMethod() functions for each primitive
return type and a CallObjectMethod() for methods that return Java objects
(seen as jobjects on the C side). The complete list of Call functions is
given in Table 22.3.
To call static Java methods, there are analogous CallStaticXxxMethod()
functions. Like the Get/SetStaticXxxField() methods, they differ from
Search WWH ::




Custom Search