Created
November 27, 2014 14:51
-
-
Save Zubnix/35962eb6bbb4b4dcdd01 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
@C({ | |
//includes | |
"#include <stlib.h>", | |
//global variables | |
"static JavaVM* jvm;", | |
"static jclass handler_class;", | |
"static jmethodID j_func_handle;", | |
//defines | |
"#define GET_ATTACHED_JENV(jenv){\\", | |
" int getEnvStat = (*jvm)->GetEnv(jvm, (void **)&jenv,JNI_VERSION_1_6);\\", | |
" if (getEnvStat == JNI_EDETACHED) {\\", | |
" (*jvm)->AttachCurrentThread(jvm, (void **) &jenv,NULL);\\", | |
" }\\", | |
"}", | |
//custom functions | |
"JNIEXPORT", | |
"jint JNI_OnLoad(JavaVM *vm, void *reserved) {", | |
" jvm = vm;", | |
" return JNI_VERSION_1_6;", | |
"}", | |
"void handle_callback(void *user_data) {", | |
" JNIEnv * const env;", | |
" GET_ATTACHED_JENV(env);", | |
" jobject handler = (jobject) user_data;", | |
" (*env)->CallVoidMethod(env, handler, j_func_handle);", | |
"}" | |
}) | |
public class Example { | |
static{ | |
registerHandler(Handler.class); | |
} | |
public static interface Handler { | |
void handle(); | |
} | |
@C({ | |
"handler_class = (*env)->NewGlobalRef(env, clazz);", | |
"j_func_handle = (*env)->GetMethodID(env, itf_class, \"handle\", \"()V\");" | |
}) | |
private static native void registerHandler(Class<? extends Handler> clazz); | |
//native function body | |
@C({ | |
"const char* first_arg = (*env)->GetStringUTFChars(env, firstArg, 0);", | |
"int second_arg = (int) secondArg;", | |
"handle_callback(handler)", | |
"return (jlong) 1235L;" | |
}) | |
public native long someNativeCall(String firstArg, int secondArg, Handler handler); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment