As can be seen from the Java wrapper classes we need seven JNI functions - that is we need two for the contructors and five for the methods.
Tip: All functions in the wrapper class with native before it needs to be defined in the JNI(C) files.
The function name of the C function that gets mapped to a Java method is made up as follows:
JNIEXPORT jnitype JNICALL Java_<classname>_<methodname>
(JNIEnv * , jobject , <...> );You can use Table 2-1 and Table 2-2 to get from Java types to JNI types. Please note that Integer etc. should be accessed through a jobject.
Table 2-1. Java to JNI conversions
Basic Type | JNI Type |
boolean | jboolean |
byte | jbyte |
char | jchar |
short | jshort |
int | jint |
long | jlong |
long | jlong |
float | jfloat |
void | void |
For instance the definitions for the two constructors will be look like follows:
JNIEXPORT jlong JNICALL Java_gtk_GtkButton_nativenew
(JNIEnv * , jobject );JNIEXPORT jlong JNICALL Java_gtk_GtkButton_nativenewWithLabel
(JNIEnv * , jobject , jstring );We have to include the jni.h header file (and of course the gtk.h file) so the C header file will look something like Example 2-3.
Example 2-3. gtk_GtkButton.h
#include <jni.h> #include <gtk/gtk.h> #ifndef _Included_gtk_GtkButton #define _Included_gtk_GtkButton #ifdef __cplusplus extern "C" { #endif JNIEXPORT jlong JNICALL Java_gtk_GtkButton_nativenew (JNIEnv *, jobject); JNIEXPORT jlong JNICALL Java_gtk_GtkButton_nativenewWithLabel (JNIEnv *, jobject, jstring); JNIEXPORT void JNICALL Java_gtk_GtkButton_pressed (JNIEnv *, jobject); JNIEXPORT void JNICALL Java_gtk_GtkButton_released (JNIEnv *, jobject); JNIEXPORT void JNICALL Java_gtk_GtkButton_clicked (JNIEnv *, jobject); JNIEXPORT void JNICALL Java_gtk_GtkButton_enter (JNIEnv *, jobject); JNIEXPORT void JNICALL Java_gtk_GtkButton_leave (JNIEnv *, jobject); #ifdef __cplusplus } #endif #endif |
[1] | For more information on the "translation" between Java methods and JNI functions refer to Javasoft's documentation or tutorials. |