Created
January 16, 2010 03:41
-
-
Save enukane/278629 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package my.hoge; | |
public class HogeBack { | |
private int num; | |
private native int callbackMethod(int num); | |
public void HogeBack() | |
{ | |
num = 0; | |
} | |
public void increment() | |
{ | |
num++; | |
} | |
public void increment(int n) | |
{ | |
num += n; | |
} | |
public int getNumber() | |
{ | |
return num; | |
} | |
public void printHoge(){ | |
System.out.println("Hoge!"); | |
} | |
public String getHoge(){ | |
return new String("Hoge"); | |
} | |
public int add(int i, int j) | |
{ | |
return i + j; | |
} | |
public String getMsg(String msg){ | |
return new String("Hoget is " + msg); | |
} | |
public int invokeMethod(int num){ | |
System.out.println("Invoking method"); | |
return callbackMethod(num); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<jni.h> | |
#include<string.h> | |
/* | |
* here we expect Hoge class in my.hoge package | |
* | |
*/ | |
jint nativeCallback(JNIEnv* env, jobject obj, jint value){ | |
printf("Native Callback method\n"); | |
int result = 1000 + (jint)value; | |
return (jint)result; | |
} | |
int main(int argc, char** argv) | |
{ | |
JNIEnv *jnienv; | |
JavaVM *javavm; | |
JavaVMInitArgs vm_args; | |
JavaVMOption options[1]; | |
options[0].optionString = "-Djava.class.path=."; | |
vm_args.version = JNI_VERSION_1_2; | |
vm_args.options = options; | |
vm_args.nOptions = 1; | |
vm_args.ignoreUnrecognized = true; | |
printf("Java VM generate\n"); | |
int result = JNI_CreateJavaVM(&javavm, (void **)&jnienv, &vm_args); | |
puts("create jvm"); | |
if(result != 0){ | |
printf("jvm failed to create : %d\n", result); | |
return 1; | |
} | |
puts("searching class"); | |
jclass cls = jnienv->FindClass("my/hoge/HogeBack"); | |
if(cls == 0){ | |
puts("failed to find class"); | |
return 1; | |
} | |
puts("find constructor method"); | |
jmethodID cns = jnienv->GetMethodID(cls, "<init>", "()V"); | |
if(cns == NULL){ | |
puts("constructor method find failed"); | |
return 1; | |
} | |
puts("instantialize"); | |
jobject obj = jnienv->NewObject(cls, cns); | |
if(!obj){ | |
puts("failed to new"); | |
return 1; | |
} | |
// printHoge | |
puts("searching method: printHoge"); | |
jmethodID printHogeId = jnienv->GetMethodID(cls, "printHoge", "()V"); | |
if(printHogeId == NULL){ | |
puts("failed to find printHoget"); | |
return 1; | |
} | |
puts("----method calling"); | |
jnienv->CallVoidMethod(obj, printHogeId); | |
puts("----method called"); | |
// add | |
puts("searching method: add"); | |
jmethodID addId = jnienv->GetMethodID(cls, "add", "(II)I"); | |
if(!addId){ | |
puts("failed to find add"); | |
return 1; | |
} | |
puts("----call"); | |
int addResult = (int)jnienv->CallIntMethod(obj, addId, 10, 100); | |
printf("----called : result = %d\n", addResult); | |
// getHoge | |
puts("searching method: getHoge"); | |
jmethodID getHogeId = jnienv->GetMethodID(cls, "getHoge", "()Ljava/lang/String;"); | |
if(!getHogeId){ | |
puts("failed to find getHoge"); | |
return 1; | |
} | |
puts("----call"); | |
jstring jstr1 = (jstring)jnienv->CallObjectMethod(obj, getHogeId); | |
const char* str1 = jnienv->GetStringUTFChars(jstr1, 0); | |
char* resStr1 = strdup(str1); | |
jnienv->ReleaseStringUTFChars(jstr1, str1); | |
printf("----called : result = %s\n", resStr1); | |
// getMsg | |
puts("searching method: geMsg"); | |
jmethodID getMsgId = jnienv->GetMethodID(cls, "getMsg", "(Ljava/lang/String;)Ljava/lang/String;"); | |
if(!getMsgId){ | |
puts("failed to find getMsg"); | |
return 1; | |
} | |
puts("----call"); | |
jstring jstr2 = (jstring)jnienv->CallObjectMethod(obj, getMsgId, jnienv->NewStringUTF("Hell to the Sack code!")); | |
const char* str2 = jnienv->GetStringUTFChars(jstr2, 0); | |
char* resStr2 = strdup(str2); | |
jnienv->ReleaseStringUTFChars(jstr2, str2); | |
printf("----called : result = %s\n", resStr2); | |
// callback | |
puts("Callback test"); | |
JNINativeMethod n_method = {"callbackMethod", "(I)I", (void *)nativeCallback}; | |
result = jnienv->RegisterNatives(cls, &n_method, 1); | |
if(result){ | |
puts("failed to register native function"); | |
return 1; | |
} | |
puts("Callback set done"); | |
puts("Callback invoke test"); | |
jmethodID invokeMethodId = jnienv->GetMethodID(cls, "invokeMethod", "(I)I"); | |
if(!invokeMethodId){ | |
puts("failed to fine invokeMethod"); | |
return 1; | |
} | |
puts("----call"); | |
int callbacked = (int)jnienv->CallIntMethod(obj, invokeMethodId, 17); | |
printf("----called : result = %d\n", callbacked); | |
// destroy JVM | |
result = javavm->DestroyJavaVM(); | |
if(result){ | |
puts("Failed to destroy jvm"); | |
return 1; | |
} | |
puts("Safely destroyed jvm"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment