Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.74 KB | None | 0 0
  1. package org.stackoverflow.data;
  2.  
  3. public class BeanObject {
  4.  
  5. String foo = "";
  6.  
  7. public String getFoo() {
  8.  
  9. return foo;
  10. }
  11. }
  12.  
  13. package org.stackoverflow.jni;
  14.  
  15. import org.stackoverflow.data.BeanObject;
  16.  
  17. public class NativeClazz {
  18.  
  19. // Static area for forced initialization
  20. static {
  21.  
  22. // Load Native Library (C++)
  23. System.loadLibrary("Native_Library_File_Name");
  24.  
  25. // Load Class and Method Ids
  26. initialize();
  27. }
  28.  
  29. /**
  30. * Native Initialization loads ClassIDs and MethodIDs to static variables.
  31. */
  32. public static native void initialize();
  33.  
  34. /**
  35. * Native Destructor unloads ClassIDs from global memory.
  36. */
  37. public static native void destroy();
  38.  
  39. /**
  40. * A static native method you plan to call.
  41. */
  42. public static native void staticNativeMethod(BeanObject bean);
  43.  
  44. /**
  45. * A non-static native method you plan to call, to show this also works with
  46. * instantiated Java classes.
  47. */
  48. public native void instanceNativeMethod(BeanObject bean);
  49. }
  50.  
  51. /* DO NOT EDIT THIS FILE - it is machine generated */
  52. #include <jni.h>
  53. /* Header for class org_stackoverflow_jni_NativeClazz */
  54.  
  55. #ifndef _Included_org_stackoverflow_jni_NativeClazz
  56. #define _Included_org_stackoverflow_jni_NativeClazz
  57. #ifdef __cplusplus
  58. extern "C" {
  59. #endif
  60. /*
  61. * Class: org_stackoverflow_jni_NativeClazz
  62. * Method: initialize
  63. * Signature: ()V
  64. */
  65. JNIEXPORT void JNICALL Java_org_stackoverflow_jni_NativeClazz_initialize
  66. (JNIEnv *, jclass);
  67.  
  68. /*
  69. * Class: org_stackoverflow_jni_NativeClazz
  70. * Method: destroy
  71. * Signature: ()V
  72. */
  73. JNIEXPORT void JNICALL Java_org_stackoverflow_jni_NativeClazz_destroy
  74. (JNIEnv *, jclass);
  75.  
  76. /*
  77. * Class: org_stackoverflow_jni_NativeClazz_staticNativeMethod
  78. * Method: staticNativeMethod
  79. * Signature: ()V
  80. */
  81. JNIEXPORT void JNICALL Java_org_stackoverflow_jni_NativeClazz_staticNativeMethod
  82. (JNIEnv *, jclass, jobject);
  83.  
  84. /*
  85. * Class: org_stackoverflow_jni_NativeClazz_instanceNativeMethod
  86. * Method: instanceNativeMethod
  87. * Signature: ()V
  88. */
  89. JNIEXPORT void JNICALL Java_org_stackoverflow_jni_NativeClazz_instanceNativeMethod
  90. (JNIEnv *, jobject, jobject);
  91.  
  92. #ifdef __cplusplus
  93. }
  94. #endif
  95. #endif
  96.  
  97. #include "org_stackoverflow_jni_NativeClazz.h"
  98.  
  99. using namespace std;
  100.  
  101. /**************************************************************
  102. * Static Global Variables to cache Java Class and Method IDs
  103. **************************************************************/
  104. static jclass JC_BeanObject;
  105. static jmethodID JMID_BeanObject_getFoo;
  106.  
  107. /**************************************************************
  108. * Initialize the static Class and Method Id variables
  109. **************************************************************/
  110. JNIEXPORT void JNICALL Java_org_stackoverflow_jni_NativeClazz_initialize
  111. (JNIEnv * env, jclass clazz) {
  112.  
  113. // Temporary local reference holder
  114. jclass tempLocalClassRef;
  115.  
  116. // STEP 1/3 : Load the class id
  117. tempLocalClassRef = env->FindClass("org/stackoverflow/data/BeanObject");
  118.  
  119. // STEP 2/3 : Assign the ClassId as a Global Reference
  120. JC_BeanObject = (jclass) env->NewGlobalRef(tempLocalClassRef);
  121.  
  122. // STEP 3/3 : Delete the no longer needed local reference
  123. env->DeleteLocalRef(tempLocalClassRef);
  124.  
  125. // Load the method id
  126. JMID_BeanObject_getFoo = env->GetMethodID(JC_BeanObject, "getFoo", "(Ljava/lang/String;)V");
  127.  
  128. // ... repeat prior line for any other methods of BeanObject
  129.  
  130. // ... repeat STEPS 1-3 for any other classes; re-use tempLocalClassRef.
  131. }
  132.  
  133. /**************************************************************
  134. * Destroy the global static Class Id variables
  135. **************************************************************/
  136. JNIEXPORT void JNICALL Java_org_stackoverflow_jni_NativeClazz_destroy
  137. (JNIEnv * env, jclass clazz) {
  138.  
  139. // Destroy the global references
  140. env->DeleteGlobalRef(JC_BeanObject);
  141.  
  142. // ... repeat for any other global references
  143.  
  144. }
  145.  
  146. /**************************************************************
  147. * A Static Native Method
  148. **************************************************************/
  149. JNIEXPORT void JNICALL Java_org_stackoverflow_jni_NativeClazz_staticNativeMethod
  150. (JNIEnv * env, jclass clazz, jobject jBeanObject) {
  151.  
  152. jstring jFoo = (jstring)env->CallObjectMethod(jBeanObject, JMID_BeanObject_getFoo);
  153. }
  154.  
  155. /**************************************************************
  156. * Instance / Non-Static Native Method
  157. **************************************************************/
  158. JNIEXPORT void JNICALL Java_org_stackoverflow_jni_NativeClazz_instanceNativeMethod
  159. (JNIEnv * env, jobject selfReference, jobject jBeanObject) {
  160.  
  161. jstring jFoo = (jstring)env->CallObjectMethod(jBeanObject, JMID_BeanObject_getFoo);
  162. }
  163.  
  164. public class SimpleClazz {
  165.  
  166. public int value = 10;
  167.  
  168. public native int getValue();
  169.  
  170. static {
  171. // Load Native Library
  172. System.loadLibrary("The native library name");
  173. }
  174. }
  175.  
  176. /* DO NOT EDIT THIS FILE - it is machine generated */
  177. #include <jni.h>
  178. /* Header for class SimpleClazz */
  179.  
  180. #ifndef _Included_SimpleClazz
  181. #define _Included_SimpleClazz
  182. #ifdef __cplusplus
  183. extern "C" {
  184. #endif
  185. /*
  186. * Class: SimpleClazz
  187. * Method: getValue
  188. * Signature: ()I
  189. */
  190. JNIEXPORT jint JNICALL Java_SimpleClazz_getValue
  191. (JNIEnv *, jobject);
  192.  
  193. #ifdef __cplusplus
  194. }
  195. #endif
  196. #endif
  197.  
  198. #include <jni.h>
  199. #include <SimpleClazz.h>
  200.  
  201. static jclass simpleCls;
  202.  
  203. // According to http://docs.oracle.com/javase/1.5.0/docs/guide/jni/spec/invocation.html#JNI_OnLoad
  204. // The VM calls JNI_OnLoad when the native library is loaded (for example, through System.loadLibrary).
  205. jint JNI_OnLoad(JavaVM* vm, void* reserved) {
  206. if ((*vm)->GetEnv(vm, (void **) &env, JNI_VERSION_1_6) != JNI_OK) {
  207. return JNI_ERR;
  208. } else {
  209. jclass localSimpleCls = (*env)->FindClass("SimpleClazz");
  210.  
  211. if (localSimpleCls == NULL) {
  212. return JNI_ERR;
  213. }
  214. simpleCls = (jclass) (*env)->NewGlobalRef(env, localSimpleCls);
  215. }
  216. return JNI_VERSION_1_6;
  217. }
  218.  
  219.  
  220.  
  221. JNIEXPORT jint JNICALL Java_SimpleClazz_getValue(JNIEnv * env, jobject thiz){
  222. static jfieldID valueID = NULL;
  223. if (valueID == NULL) {
  224. valueID = (*env)->GetFieldID(env, simpleCls, "value", "I");
  225. if (valueID == NULL){
  226. return JNI_ERR; // Exception thrown
  227. }
  228. }
  229. jint value = (*env)->GetIntField(env, thiz, valueID);
  230. return value;
  231. }
  232.  
  233. // According to http://docs.oracle.com/javase/1.5.0/docs/guide/jni/spec/invocation.html#JNI_OnUnload
  234. // The VM calls JNI_OnUnload when the class loader containing the native library is garbage collected.
  235. void JNI_OnUnload(JavaVM *vm, void *reserved) {
  236. JNIEnv* env;
  237. if ((*vm)->GetEnv(vm, (void **) &env, JNI_VERSION_1_6) != JNI_OK) {
  238. // Something is wrong but nothing we can do about this :(
  239. return;
  240. } else {
  241. if (0 != NULL){
  242. (*env)->DeleteGlobalRef(env, simpleCls);
  243. }
  244. }
  245. }
  246.  
  247. typedef struct MYVARIANT_FID_CACHE {
  248. int cached;
  249. jclass clazz;
  250. jfieldID pAddress;
  251. } MYVARIANT_FID_CACHE;
  252.  
  253. MYVARIANT_FID_CACHE VARIANTFc;
  254.  
  255. void cacheMYVARIANTFields(JNIEnv *env, jobject lpObject)
  256. {
  257. if (VARIANTFc.cached) return;
  258. VARIANTFc.clazz = env->GetObjectClass(lpObject);
  259. VARIANTFc.pAddress = env->GetFieldID(VARIANTFc.clazz, "pAddress", "I");
  260. VARIANTFc.cached = 1;
  261. }
  262.  
  263. VARIANT *getMYVARIANTFields(JNIEnv *env, jobject lpObject, VARIANT *lpStruct)
  264. {
  265. if (!VARIANTFc.cached) cacheVARIANT2Fields(env, lpObject);
  266.  
  267. lpStruct = (VARIANT*)(env->GetIntField(lpObject, VARIANTFc.pAddress));
  268.  
  269. return lpStruct;
  270. }
  271.  
  272. package demo;
  273. public class WithNatives {
  274.  
  275. static {
  276. initIDs();
  277. }
  278.  
  279. private static native void initIDs();
  280.  
  281. }
  282.  
  283. static jmethodID methodId;
  284.  
  285. void JNICALL Java_demo_WithNatives_initIDs(JNIEnv *env, jclass clazz)
  286. {
  287. // initialize methodId
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement