Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.stackoverflow.data;
- public class BeanObject {
- String foo = "";
- public String getFoo() {
- return foo;
- }
- }
- package org.stackoverflow.jni;
- import org.stackoverflow.data.BeanObject;
- public class NativeClazz {
- // Static area for forced initialization
- static {
- // Load Native Library (C++)
- System.loadLibrary("Native_Library_File_Name");
- // Load Class and Method Ids
- initialize();
- }
- /**
- * Native Initialization loads ClassIDs and MethodIDs to static variables.
- */
- public static native void initialize();
- /**
- * Native Destructor unloads ClassIDs from global memory.
- */
- public static native void destroy();
- /**
- * A static native method you plan to call.
- */
- public static native void staticNativeMethod(BeanObject bean);
- /**
- * A non-static native method you plan to call, to show this also works with
- * instantiated Java classes.
- */
- public native void instanceNativeMethod(BeanObject bean);
- }
- /* DO NOT EDIT THIS FILE - it is machine generated */
- #include <jni.h>
- /* Header for class org_stackoverflow_jni_NativeClazz */
- #ifndef _Included_org_stackoverflow_jni_NativeClazz
- #define _Included_org_stackoverflow_jni_NativeClazz
- #ifdef __cplusplus
- extern "C" {
- #endif
- /*
- * Class: org_stackoverflow_jni_NativeClazz
- * Method: initialize
- * Signature: ()V
- */
- JNIEXPORT void JNICALL Java_org_stackoverflow_jni_NativeClazz_initialize
- (JNIEnv *, jclass);
- /*
- * Class: org_stackoverflow_jni_NativeClazz
- * Method: destroy
- * Signature: ()V
- */
- JNIEXPORT void JNICALL Java_org_stackoverflow_jni_NativeClazz_destroy
- (JNIEnv *, jclass);
- /*
- * Class: org_stackoverflow_jni_NativeClazz_staticNativeMethod
- * Method: staticNativeMethod
- * Signature: ()V
- */
- JNIEXPORT void JNICALL Java_org_stackoverflow_jni_NativeClazz_staticNativeMethod
- (JNIEnv *, jclass, jobject);
- /*
- * Class: org_stackoverflow_jni_NativeClazz_instanceNativeMethod
- * Method: instanceNativeMethod
- * Signature: ()V
- */
- JNIEXPORT void JNICALL Java_org_stackoverflow_jni_NativeClazz_instanceNativeMethod
- (JNIEnv *, jobject, jobject);
- #ifdef __cplusplus
- }
- #endif
- #endif
- #include "org_stackoverflow_jni_NativeClazz.h"
- using namespace std;
- /**************************************************************
- * Static Global Variables to cache Java Class and Method IDs
- **************************************************************/
- static jclass JC_BeanObject;
- static jmethodID JMID_BeanObject_getFoo;
- /**************************************************************
- * Initialize the static Class and Method Id variables
- **************************************************************/
- JNIEXPORT void JNICALL Java_org_stackoverflow_jni_NativeClazz_initialize
- (JNIEnv * env, jclass clazz) {
- // Temporary local reference holder
- jclass tempLocalClassRef;
- // STEP 1/3 : Load the class id
- tempLocalClassRef = env->FindClass("org/stackoverflow/data/BeanObject");
- // STEP 2/3 : Assign the ClassId as a Global Reference
- JC_BeanObject = (jclass) env->NewGlobalRef(tempLocalClassRef);
- // STEP 3/3 : Delete the no longer needed local reference
- env->DeleteLocalRef(tempLocalClassRef);
- // Load the method id
- JMID_BeanObject_getFoo = env->GetMethodID(JC_BeanObject, "getFoo", "(Ljava/lang/String;)V");
- // ... repeat prior line for any other methods of BeanObject
- // ... repeat STEPS 1-3 for any other classes; re-use tempLocalClassRef.
- }
- /**************************************************************
- * Destroy the global static Class Id variables
- **************************************************************/
- JNIEXPORT void JNICALL Java_org_stackoverflow_jni_NativeClazz_destroy
- (JNIEnv * env, jclass clazz) {
- // Destroy the global references
- env->DeleteGlobalRef(JC_BeanObject);
- // ... repeat for any other global references
- }
- /**************************************************************
- * A Static Native Method
- **************************************************************/
- JNIEXPORT void JNICALL Java_org_stackoverflow_jni_NativeClazz_staticNativeMethod
- (JNIEnv * env, jclass clazz, jobject jBeanObject) {
- jstring jFoo = (jstring)env->CallObjectMethod(jBeanObject, JMID_BeanObject_getFoo);
- }
- /**************************************************************
- * Instance / Non-Static Native Method
- **************************************************************/
- JNIEXPORT void JNICALL Java_org_stackoverflow_jni_NativeClazz_instanceNativeMethod
- (JNIEnv * env, jobject selfReference, jobject jBeanObject) {
- jstring jFoo = (jstring)env->CallObjectMethod(jBeanObject, JMID_BeanObject_getFoo);
- }
- public class SimpleClazz {
- public int value = 10;
- public native int getValue();
- static {
- // Load Native Library
- System.loadLibrary("The native library name");
- }
- }
- /* DO NOT EDIT THIS FILE - it is machine generated */
- #include <jni.h>
- /* Header for class SimpleClazz */
- #ifndef _Included_SimpleClazz
- #define _Included_SimpleClazz
- #ifdef __cplusplus
- extern "C" {
- #endif
- /*
- * Class: SimpleClazz
- * Method: getValue
- * Signature: ()I
- */
- JNIEXPORT jint JNICALL Java_SimpleClazz_getValue
- (JNIEnv *, jobject);
- #ifdef __cplusplus
- }
- #endif
- #endif
- #include <jni.h>
- #include <SimpleClazz.h>
- static jclass simpleCls;
- // According to http://docs.oracle.com/javase/1.5.0/docs/guide/jni/spec/invocation.html#JNI_OnLoad
- // The VM calls JNI_OnLoad when the native library is loaded (for example, through System.loadLibrary).
- jint JNI_OnLoad(JavaVM* vm, void* reserved) {
- if ((*vm)->GetEnv(vm, (void **) &env, JNI_VERSION_1_6) != JNI_OK) {
- return JNI_ERR;
- } else {
- jclass localSimpleCls = (*env)->FindClass("SimpleClazz");
- if (localSimpleCls == NULL) {
- return JNI_ERR;
- }
- simpleCls = (jclass) (*env)->NewGlobalRef(env, localSimpleCls);
- }
- return JNI_VERSION_1_6;
- }
- JNIEXPORT jint JNICALL Java_SimpleClazz_getValue(JNIEnv * env, jobject thiz){
- static jfieldID valueID = NULL;
- if (valueID == NULL) {
- valueID = (*env)->GetFieldID(env, simpleCls, "value", "I");
- if (valueID == NULL){
- return JNI_ERR; // Exception thrown
- }
- }
- jint value = (*env)->GetIntField(env, thiz, valueID);
- return value;
- }
- // According to http://docs.oracle.com/javase/1.5.0/docs/guide/jni/spec/invocation.html#JNI_OnUnload
- // The VM calls JNI_OnUnload when the class loader containing the native library is garbage collected.
- void JNI_OnUnload(JavaVM *vm, void *reserved) {
- JNIEnv* env;
- if ((*vm)->GetEnv(vm, (void **) &env, JNI_VERSION_1_6) != JNI_OK) {
- // Something is wrong but nothing we can do about this :(
- return;
- } else {
- if (0 != NULL){
- (*env)->DeleteGlobalRef(env, simpleCls);
- }
- }
- }
- typedef struct MYVARIANT_FID_CACHE {
- int cached;
- jclass clazz;
- jfieldID pAddress;
- } MYVARIANT_FID_CACHE;
- MYVARIANT_FID_CACHE VARIANTFc;
- void cacheMYVARIANTFields(JNIEnv *env, jobject lpObject)
- {
- if (VARIANTFc.cached) return;
- VARIANTFc.clazz = env->GetObjectClass(lpObject);
- VARIANTFc.pAddress = env->GetFieldID(VARIANTFc.clazz, "pAddress", "I");
- VARIANTFc.cached = 1;
- }
- VARIANT *getMYVARIANTFields(JNIEnv *env, jobject lpObject, VARIANT *lpStruct)
- {
- if (!VARIANTFc.cached) cacheVARIANT2Fields(env, lpObject);
- lpStruct = (VARIANT*)(env->GetIntField(lpObject, VARIANTFc.pAddress));
- return lpStruct;
- }
- package demo;
- public class WithNatives {
- static {
- initIDs();
- }
- private static native void initIDs();
- }
- static jmethodID methodId;
- void JNICALL Java_demo_WithNatives_initIDs(JNIEnv *env, jclass clazz)
- {
- // initialize methodId
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement