j4a_base.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * copyright (c) 2015 Zhang Rui <bbcallen@gmail.com>
  3. *
  4. * This file is part of jni4android.
  5. *
  6. * jni4android is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * jni4android is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with jni4android; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "j4a_base.h"
  21. #include "j4a/class/android/os/Build.h"
  22. /********************
  23. * Exception Handle
  24. ********************/
  25. bool J4A_ExceptionCheck__throwAny(JNIEnv *env)
  26. {
  27. if ((*env)->ExceptionCheck(env)) {
  28. (*env)->ExceptionDescribe(env);
  29. return true;
  30. }
  31. return false;
  32. }
  33. bool J4A_ExceptionCheck__catchAll(JNIEnv *env)
  34. {
  35. if ((*env)->ExceptionCheck(env)) {
  36. (*env)->ExceptionDescribe(env);
  37. (*env)->ExceptionClear(env);
  38. return true;
  39. }
  40. return false;
  41. }
  42. int J4A_ThrowExceptionOfClass(JNIEnv* env, jclass clazz, const char* msg)
  43. {
  44. if ((*env)->ThrowNew(env, clazz, msg) != JNI_OK)
  45. J4A_ALOGE("%s: Failed: msg: '%s'\n", __func__, msg);
  46. return 0;
  47. }
  48. int J4A_ThrowException(JNIEnv* env, const char* class_sign, const char* msg)
  49. {
  50. int ret = -1;
  51. if (J4A_ExceptionCheck__catchAll(env)) {
  52. J4A_ALOGE("pending exception throwed.\n");
  53. }
  54. jclass exceptionClass = J4A_FindClass__catchAll(env, class_sign);
  55. if (exceptionClass == NULL) {
  56. J4A_FUNC_FAIL_TRACE();
  57. ret = -1;
  58. goto fail;
  59. }
  60. ret = J4A_ThrowExceptionOfClass(env, exceptionClass, msg);
  61. if (ret) {
  62. J4A_FUNC_FAIL_TRACE();
  63. goto fail;
  64. }
  65. ret = 0;
  66. fail:
  67. J4A_DeleteLocalRef__p(env, &exceptionClass);
  68. return ret;
  69. }
  70. int J4A_ThrowIllegalStateException(JNIEnv *env, const char* msg)
  71. {
  72. return J4A_ThrowException(env, "java/lang/IllegalStateException", msg);
  73. }
  74. /********************
  75. * References
  76. ********************/
  77. jclass J4A_NewGlobalRef__catchAll(JNIEnv *env, jobject obj)
  78. {
  79. jclass obj_global = (*env)->NewGlobalRef(env, obj);
  80. if (J4A_ExceptionCheck__catchAll(env) || !(obj_global)) {
  81. J4A_FUNC_FAIL_TRACE();
  82. goto fail;
  83. }
  84. fail:
  85. return obj_global;
  86. }
  87. void J4A_DeleteLocalRef(JNIEnv *env, jobject obj)
  88. {
  89. if (!obj)
  90. return;
  91. (*env)->DeleteLocalRef(env, obj);
  92. }
  93. void J4A_DeleteLocalRef__p(JNIEnv *env, jobject *obj)
  94. {
  95. if (!obj)
  96. return;
  97. J4A_DeleteLocalRef(env, *obj);
  98. *obj = NULL;
  99. }
  100. void J4A_DeleteGlobalRef(JNIEnv *env, jobject obj)
  101. {
  102. if (!obj)
  103. return;
  104. (*env)->DeleteGlobalRef(env, obj);
  105. }
  106. void J4A_DeleteGlobalRef__p(JNIEnv *env, jobject *obj)
  107. {
  108. if (!obj)
  109. return;
  110. J4A_DeleteGlobalRef(env, *obj);
  111. *obj = NULL;
  112. }
  113. void J4A_ReleaseStringUTFChars(JNIEnv *env, jstring str, const char *c_str)
  114. {
  115. if (!str || !c_str)
  116. return;
  117. (*env)->ReleaseStringUTFChars(env, str, c_str);
  118. }
  119. void J4A_ReleaseStringUTFChars__p(JNIEnv *env, jstring str, const char **c_str)
  120. {
  121. if (!str || !c_str)
  122. return;
  123. J4A_ReleaseStringUTFChars(env, str, *c_str);
  124. *c_str = NULL;
  125. }
  126. /********************
  127. * Class Load
  128. ********************/
  129. jclass J4A_FindClass__catchAll(JNIEnv *env, const char *class_sign)
  130. {
  131. jclass clazz = (*env)->FindClass(env, class_sign);
  132. if (J4A_ExceptionCheck__catchAll(env) || !(clazz)) {
  133. J4A_FUNC_FAIL_TRACE();
  134. clazz = NULL;
  135. goto fail;
  136. }
  137. fail:
  138. return clazz;
  139. }
  140. jclass J4A_FindClass__asGlobalRef__catchAll(JNIEnv *env, const char *class_sign)
  141. {
  142. jclass clazz_global = NULL;
  143. jclass clazz = J4A_FindClass__catchAll(env, class_sign);
  144. if (!clazz) {
  145. J4A_FUNC_FAIL_TRACE1(class_sign);
  146. goto fail;
  147. }
  148. clazz_global = J4A_NewGlobalRef__catchAll(env, clazz);
  149. if (!clazz_global) {
  150. J4A_FUNC_FAIL_TRACE1(class_sign);
  151. goto fail;
  152. }
  153. fail:
  154. J4A_DeleteLocalRef__p(env, &clazz);
  155. return clazz_global;
  156. }
  157. jmethodID J4A_GetMethodID__catchAll(JNIEnv *env, jclass clazz, const char *method_name, const char *method_sign)
  158. {
  159. jmethodID method_id = (*env)->GetMethodID(env, clazz, method_name, method_sign);
  160. if (J4A_ExceptionCheck__catchAll(env) || !method_id) {
  161. J4A_FUNC_FAIL_TRACE2(method_name, method_sign);
  162. method_id = NULL;
  163. goto fail;
  164. }
  165. fail:
  166. return method_id;
  167. }
  168. jmethodID J4A_GetStaticMethodID__catchAll(JNIEnv *env, jclass clazz, const char *method_name, const char *method_sign)
  169. {
  170. jmethodID method_id = (*env)->GetStaticMethodID(env, clazz, method_name, method_sign);
  171. if (J4A_ExceptionCheck__catchAll(env) || !method_id) {
  172. J4A_FUNC_FAIL_TRACE2(method_name, method_sign);
  173. method_id = NULL;
  174. goto fail;
  175. }
  176. fail:
  177. return method_id;
  178. }
  179. jfieldID J4A_GetFieldID__catchAll(JNIEnv *env, jclass clazz, const char *field_name, const char *field_sign)
  180. {
  181. jfieldID field_id = (*env)->GetFieldID(env, clazz, field_name, field_sign);
  182. if (J4A_ExceptionCheck__catchAll(env) || !field_id) {
  183. J4A_FUNC_FAIL_TRACE2(field_name, field_sign);
  184. field_id = NULL;
  185. goto fail;
  186. }
  187. fail:
  188. return field_id;
  189. }
  190. jfieldID J4A_GetStaticFieldID__catchAll(JNIEnv *env, jclass clazz, const char *field_name, const char *field_sign)
  191. {
  192. jfieldID field_id = (*env)->GetStaticFieldID(env, clazz, field_name, field_sign);
  193. if (J4A_ExceptionCheck__catchAll(env) || !field_id) {
  194. J4A_FUNC_FAIL_TRACE2(field_name, field_sign);
  195. field_id = NULL;
  196. goto fail;
  197. }
  198. fail:
  199. return field_id;
  200. }
  201. /********************
  202. * Misc Functions
  203. ********************/
  204. jbyteArray J4A_NewByteArray__catchAll(JNIEnv *env, jsize capacity)
  205. {
  206. jbyteArray local = (*env)->NewByteArray(env, capacity);
  207. if (J4A_ExceptionCheck__catchAll(env) || !local)
  208. return NULL;
  209. return local;
  210. }
  211. jbyteArray J4A_NewByteArray__asGlobalRef__catchAll(JNIEnv *env, jsize capacity)
  212. {
  213. jbyteArray local = (*env)->NewByteArray(env, capacity);
  214. if (J4A_ExceptionCheck__catchAll(env) || !local)
  215. return NULL;
  216. jbyteArray global = (*env)->NewGlobalRef(env, local);
  217. J4A_DeleteLocalRef__p(env, &local);
  218. return global;
  219. }
  220. int J4A_GetSystemAndroidApiLevel(JNIEnv *env)
  221. {
  222. static int SDK_INT = 0;
  223. if (SDK_INT > 0)
  224. return SDK_INT;
  225. SDK_INT = J4AC_android_os_Build__VERSION__SDK_INT__get__catchAll(env);
  226. J4A_ALOGI("API-Level: %d\n", SDK_INT);
  227. return SDK_INT;
  228. }