Main.java, the class which calls the C function, passing the WebSiteVisit object
/**
*
* @author thomas
*/
public class Main {
private native void nativePrintObject(WebSiteVisit wsv);
/** Creates a new instance of Main */
public Main() {
}
static {
System.load("/home/thomas/testlib/HelloWorldNative/dist/HelloWorldNative.so");
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Main me = new Main();
WebSiteVisit a, b;
a = new WebSiteVisit("index.html", "subpage.html", 0x32389283);
b = new WebSiteVisit("subpage.html", "pagetwo.html", 0x32399283);
me.nativePrintObject(a);
me.nativePrintObject(b);
}
}
WebSiteVisit.java, the passed object:
package helloworld;
/**
*
* @author thomas
*/
public class WebSiteVisit {
/** URL of the web page that linked user to this one */
public String refererURL;
public String pageVisited;
public int ipAddress;
/** Creates a new instance of WebSiteVisit */
public WebSiteVisit() {
}
public WebSiteVisit(String referer, String page, int ip) {
this.refererURL = referer;
this.ipAddress = ip;
this.pageVisited = page;
}
}
The c function which receive the object:
JNIEXPORT void JNICALL Java_helloworld_Main_nativePrintObject
(JNIEnv *env, jobject ths, jobject wsv)
{
jfieldID fid;
jint ipAddress;
jstring jPageVisited;
char *pageVisited;
jstring jRefererURL;
char *refererURL;
jclass c;
c = (*env)->GetObjectClass(env, wsv);
fid = (*env)->GetFieldID(env, c, "ipAddress", "I");
ipAddress = (*env)->GetIntField(env, wsv, fid);
printf(" wsv.ipAddress = %d.%d.%d.%dn",
( ipAddress & 0xff000000 ) >> 24,
( ipAddress & 0x00ff0000 ) >> 16,
( ipAddress & 0x0000ff00 ) >> 8,
ipAddress & 0x000000ff);
fid = (*env)->GetFieldID(env, c, "pageVisited", "Ljava/lang/String;");
if(!fid) {
return;
}
jPageVisited = (*env)->GetObjectField(env, wsv, fid);
pageVisited = (*env)->GetStringUTFChars(env, jPageVisited, 0);
if(!pageVisited) {
return;
}
printf(" wsv.pageVisted = %sn", pageVisited);
(*env)->ReleaseStringUTFChars(env, jPageVisited, pageVisited);
fid = (*env)->GetFieldID(env, c, "refererURL", "Ljava/lang/String;");
if(!fid) {
return;
}
jRefererURL = (*env)->GetObjectField(env, wsv, fid);
refererURL = (*env)->GetStringUTFChars(env, jRefererURL, 0);
printf(" wsv.refererURL = %sn", refererURL);
(*env)->ReleaseStringUTFChars(env, jRefererURL, refererURL);
}