#include #include #include "HelloWorld.h" #include static CallBack *cbFunc = NULL; JNIEXPORT int JNICALL objectTest(VARIANT *obj) { return 1; } /* * Class: HelloWorld * Method: testArray * Signature: ([I)V */ JNIEXPORT void JNICALL Java_HelloWorld_testArrayRegion (JNIEnv *env, jclass, jintArray arr) { printf("Java_HelloWorld_testArrayRegion()\n"); // get the first two elements long *buf = new long[2]; env->GetIntArrayRegion(arr, 0, 2, buf); printf("i[0] = %d, i[1] = %d\n", buf[0], buf[1]); delete[] buf; } /* * Class: HelloWorld * Method: testArray * Signature: ([I)V */ JNIEXPORT void JNICALL Java_HelloWorld_testArray (JNIEnv *env, jclass, jintArray arr) { jsize len = env->GetArrayLength(arr); printf("GetArrayLength: %i\n", len); jboolean copy = JNI_FALSE; jint *buf = env->GetIntArrayElements(arr, ©); printf("GetIntArrayElements(): "); for (int i=0; iGetIntArrayElements(arr, ©); printf("\nUpdate array with SetIntArrayRegion() \n"); int new_buf[] = {10, 20, 30, 40, 50}; env->SetIntArrayRegion(arr, 1, 3, (jint *)new_buf); } /* * Class: HelloWorld * Method: creatArray * Signature: ()[I */ JNIEXPORT jintArray JNICALL Java_HelloWorld_createIntArray (JNIEnv *env, jclass) { jintArray arr = env->NewIntArray(5); int new_buf[] = {100, 200, 300, 400, 500}; env->SetIntArrayRegion(arr, 0, 5, (jint *)new_buf); return arr; } /* * Class: HelloWorld * Method: createObjectArray * Signature: (Ljava/lang/String;Ljava/lang/String;)[Ljava/lang/String; */ JNIEXPORT jobjectArray JNICALL Java_HelloWorld_createObjectArray (JNIEnv *env, jclass, jstring s, jstring initial) { jclass clazz = env->GetObjectClass(s); jobjectArray a = env->NewObjectArray(4, clazz, initial); jstring o = Java_HelloWorld_testNewString(env, clazz); env->SetObjectArrayElement(a, 1, o); return a; } /* * Class: HelloWorld * Method: testString * Signature: ()Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_HelloWorld_testNewString (JNIEnv *env, jclass) { char s[] = "Create a string with NewString"; int len = strlen(s); jchar* unicodeChars = new jchar[len]; for (int i=0; iNewString(unicodeChars, len); delete[] unicodeChars; return js; } /* * Class: HelloWorld * Method: testString * Signature: (Ljava/lang/String;)V */ JNIEXPORT void JNICALL Java_HelloWorld_testString (JNIEnv *env, jclass, jstring jstr) { jsize len = env->GetStringLength(jstr); printf("GetStringLength: %i\n", len); jboolean isCopy = 0; const jchar *buf = env->GetStringChars(jstr, &isCopy); printf("GetStringChars: %ls\n", buf); env->ReleaseStringChars(jstr, buf); } /* * Class: HelloWorld * Method: static_sum * Signature: (LSample;II)I */ JNIEXPORT jint JNICALL Java_HelloWorld_static_1sum (JNIEnv *, jclass, jobject, jint, jint) { return 0; } JNIEXPORT jint JNICALL Java_HelloWorld_sum (JNIEnv *env, jobject self, jobject sample, jint i, jint j) { jclass clazz = env->GetObjectClass(sample); jmethodID mid = env->GetMethodID(clazz, "increment", "(I)I"); int result = env->CallIntMethod(sample, mid, 100); printf("result from calling Java method: Sample.inrement -- %i\n", result); mid = env->GetStaticMethodID(clazz, "staticIncrement", "(I)I"); result = env->CallStaticIntMethod(clazz, mid, 100); printf("result from calling a Java static method: Sample.staticInrement -- %i\n", result); return i+j; } /* * Class: HelloWorld * Method: testLong * Signature: (J)J */ JNIEXPORT jlong JNICALL Java_HelloWorld_testLong (JNIEnv *env, jobject self, jlong l) { printf("Java_HelloWorld_testLong() called\n"); printf("long passed from java side: %I64x\n", l); // invoke a java method that returns long jclass clazz = env->GetObjectClass(self); jmethodID mid = env->GetMethodID(clazz, "longJavaMethod", "()J"); long result = env->CallLongMethod(self, mid); printf("result from calling Java method: longJavaMethod() -- %i\n", result); return 0x46b1faa; } /* * Class: HelloWorld * Method: test * Signature: (IILjava/lang/String;)V */ JNIEXPORT void JNICALL Java_HelloWorld_test (JNIEnv *, jobject, jint, jint, jstring) { printf("Java_HelloWorld_test called\n"); } /* * Class: HelloWorld * Method: testConstructors * Signature: (LSample;II)LSample; */ JNIEXPORT jobject JNICALL Java_HelloWorld_testConstructors (JNIEnv *env, jclass , jobject obj, jobject ) { jclass clazz = env->GetObjectClass(obj); jmethodID mid = env->GetMethodID(clazz, "", "(II)V"); jobject ret = env->NewObject(clazz, mid, 100, 1000); return ret; } JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj) { printf("\nYes!!! It works.\n"); printf("Hello world!\n"); jclass clazz = env->GetObjectClass(obj); jfieldID fid = env->GetFieldID(clazz, "data", "I"); int value = env->GetIntField(obj, fid); printf("field: JNITest.data = %d\n", value); fid = env->GetFieldID(clazz, "sample", "LSample;"); jobject sample = env->GetObjectField(obj, fid); jclass clazz1 = env->GetObjectClass(sample); fid = env->GetFieldID(clazz1, "data", "I"); value = env->GetIntField(sample, fid); printf("field: Sample.data = %d\n", value); fid = env->GetStaticFieldID(clazz1, "sData", "I"); value = env->GetStaticIntField(clazz1, fid); printf("static field: Sample.sData = %d\n", value); // test method invocation printf("\nCalling virtual method ... "); fid = env->GetFieldID(clazz, "s2", "LSample"); jobject s2 = env->GetObjectField(obj, fid); jmethodID mid = env->GetMethodID(clazz1, "run", "(III)I"); int ret = env->CallIntMethod(s2, mid, 10, 20, 30); printf("Result: %d\n\n", ret); fid = env->GetFieldID(clazz, "s1", "LSample"); jobject s1 = env->GetObjectField(obj, fid); jclass Sample1_class = env->GetObjectClass(s1); printf("\nCalling nonvirtual method ... "); jmethodID mid1 = env->GetMethodID(Sample1_class, "run", "(III)I"); int result = env->CallNonvirtualIntMethod(s1, Sample1_class, mid1, 10, 20, 30); printf("Result: %d\n\n", result); return; } /* * Class: HelloWorld * Method: exceptionTest * Signature: (Ljava/lang/Exception;)V */ JNIEXPORT void JNICALL Java_HelloWorld_exceptionTest (JNIEnv *env, jobject obj, jthrowable e) { env->Throw(e); } JNIEXPORT void JNICALL Java_HelloWorld_SetCallBack (CallBack *cb) { cbFunc = cb; } JNIEXPORT void JNICALL Java_HelloWorld_InvokeCallBack (int x, int y) { cbFunc(x, y); } JNIEXPORT void JNICALL Java_HelloWorld_CallBack (CallBack *cb, int x, int y) { cb(x, y); } JNIEXPORT void JNICALL Java_HelloWorld_FunctionTable (FunctionTable *ft, int x, int y) { CallBack *cb1 = ft->cb1; cb1(x, y); }