JniHelper.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2017 Chukong Technologies Inc.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #ifndef __ANDROID_JNI_HELPER_H__
  22. #define __ANDROID_JNI_HELPER_H__
  23. #include <jni.h>
  24. #include <string>
  25. #include <vector>
  26. #include <unordered_map>
  27. #include <functional>
  28. #include "platform/CCPlatformMacros.h"
  29. #include "math/Vec3.h"
  30. NS_CC_BEGIN
  31. typedef struct JniMethodInfo_
  32. {
  33. JNIEnv * env;
  34. jclass classID;
  35. jmethodID methodID;
  36. } JniMethodInfo;
  37. class CC_DLL JniHelper
  38. {
  39. public:
  40. static void setJavaVM(JavaVM *javaVM);
  41. static JavaVM* getJavaVM();
  42. static JNIEnv* getEnv();
  43. static jobject getActivity();
  44. static bool setClassLoaderFrom(jobject activityInstance);
  45. static bool getStaticMethodInfo(JniMethodInfo &methodinfo,
  46. const char *className,
  47. const char *methodName,
  48. const char *paramCode);
  49. static bool getMethodInfo(JniMethodInfo &methodinfo,
  50. const char *className,
  51. const char *methodName,
  52. const char *paramCode);
  53. static std::string jstring2string(jstring str);
  54. static jmethodID loadclassMethod_methodID;
  55. static jobject classloader;
  56. static std::function<void()> classloaderCallback;
  57. template <typename... Ts>
  58. static void callStaticVoidMethod(const std::string& className,
  59. const std::string& methodName,
  60. Ts... xs) {
  61. cocos2d::JniMethodInfo t;
  62. std::string signature = "(" + std::string(getJNISignature(xs...)) + ")V";
  63. if (cocos2d::JniHelper::getStaticMethodInfo(t, className.c_str(), methodName.c_str(), signature.c_str())) {
  64. t.env->CallStaticVoidMethod(t.classID, t.methodID, convert(t, xs)...);
  65. t.env->DeleteLocalRef(t.classID);
  66. deleteLocalRefs(t.env);
  67. } else {
  68. reportError(className, methodName, signature);
  69. }
  70. }
  71. template <typename... Ts>
  72. static bool callStaticBooleanMethod(const std::string& className,
  73. const std::string& methodName,
  74. Ts... xs) {
  75. jboolean jret = JNI_FALSE;
  76. cocos2d::JniMethodInfo t;
  77. std::string signature = "(" + std::string(getJNISignature(xs...)) + ")Z";
  78. if (cocos2d::JniHelper::getStaticMethodInfo(t, className.c_str(), methodName.c_str(), signature.c_str())) {
  79. jret = t.env->CallStaticBooleanMethod(t.classID, t.methodID, convert(t, xs)...);
  80. t.env->DeleteLocalRef(t.classID);
  81. deleteLocalRefs(t.env);
  82. } else {
  83. reportError(className, methodName, signature);
  84. }
  85. return (jret == JNI_TRUE);
  86. }
  87. template <typename... Ts>
  88. static int callStaticIntMethod(const std::string& className,
  89. const std::string& methodName,
  90. Ts... xs) {
  91. jint ret = 0;
  92. cocos2d::JniMethodInfo t;
  93. std::string signature = "(" + std::string(getJNISignature(xs...)) + ")I";
  94. if (cocos2d::JniHelper::getStaticMethodInfo(t, className.c_str(), methodName.c_str(), signature.c_str())) {
  95. ret = t.env->CallStaticIntMethod(t.classID, t.methodID, convert(t, xs)...);
  96. t.env->DeleteLocalRef(t.classID);
  97. deleteLocalRefs(t.env);
  98. } else {
  99. reportError(className, methodName, signature);
  100. }
  101. return ret;
  102. }
  103. template <typename... Ts>
  104. static float callStaticFloatMethod(const std::string& className,
  105. const std::string& methodName,
  106. Ts... xs) {
  107. jfloat ret = 0.0;
  108. cocos2d::JniMethodInfo t;
  109. std::string signature = "(" + std::string(getJNISignature(xs...)) + ")F";
  110. if (cocos2d::JniHelper::getStaticMethodInfo(t, className.c_str(), methodName.c_str(), signature.c_str())) {
  111. ret = t.env->CallStaticFloatMethod(t.classID, t.methodID, convert(t, xs)...);
  112. t.env->DeleteLocalRef(t.classID);
  113. deleteLocalRefs(t.env);
  114. } else {
  115. reportError(className, methodName, signature);
  116. }
  117. return ret;
  118. }
  119. template <typename... Ts>
  120. static float* callStaticFloatArrayMethod(const std::string& className,
  121. const std::string& methodName,
  122. Ts... xs) {
  123. static float ret[32];
  124. cocos2d::JniMethodInfo t;
  125. std::string signature = "(" + std::string(getJNISignature(xs...)) + ")[F";
  126. if (cocos2d::JniHelper::getStaticMethodInfo(t, className.c_str(), methodName.c_str(), signature.c_str())) {
  127. jfloatArray array = (jfloatArray) t.env->CallStaticObjectMethod(t.classID, t.methodID, convert(t, xs)...);
  128. jsize len = t.env->GetArrayLength(array);
  129. if (len <= 32) {
  130. jfloat* elems = t.env->GetFloatArrayElements(array, 0);
  131. if (elems) {
  132. memcpy(ret, elems, sizeof(float) * len);
  133. t.env->ReleaseFloatArrayElements(array, elems, 0);
  134. };
  135. }
  136. t.env->DeleteLocalRef(t.classID);
  137. deleteLocalRefs(t.env);
  138. return &ret[0];
  139. } else {
  140. reportError(className, methodName, signature);
  141. }
  142. return nullptr;
  143. }
  144. template <typename... Ts>
  145. static Vec3 callStaticVec3Method(const std::string& className,
  146. const std::string& methodName,
  147. Ts... xs) {
  148. Vec3 ret;
  149. cocos2d::JniMethodInfo t;
  150. std::string signature = "(" + std::string(getJNISignature(xs...)) + ")[F";
  151. if (cocos2d::JniHelper::getStaticMethodInfo(t, className.c_str(), methodName.c_str(), signature.c_str())) {
  152. jfloatArray array = (jfloatArray) t.env->CallStaticObjectMethod(t.classID, t.methodID, convert(t, xs)...);
  153. jsize len = t.env->GetArrayLength(array);
  154. if (len == 3) {
  155. jfloat* elems = t.env->GetFloatArrayElements(array, 0);
  156. ret.x = elems[0];
  157. ret.y = elems[1];
  158. ret.z = elems[2];
  159. t.env->ReleaseFloatArrayElements(array, elems, 0);
  160. }
  161. t.env->DeleteLocalRef(t.classID);
  162. deleteLocalRefs(t.env);
  163. } else {
  164. reportError(className, methodName, signature);
  165. }
  166. return ret;
  167. }
  168. template <typename... Ts>
  169. static double callStaticDoubleMethod(const std::string& className,
  170. const std::string& methodName,
  171. Ts... xs) {
  172. jdouble ret = 0.0;
  173. cocos2d::JniMethodInfo t;
  174. std::string signature = "(" + std::string(getJNISignature(xs...)) + ")D";
  175. if (cocos2d::JniHelper::getStaticMethodInfo(t, className.c_str(), methodName.c_str(), signature.c_str())) {
  176. ret = t.env->CallStaticDoubleMethod(t.classID, t.methodID, convert(t, xs)...);
  177. t.env->DeleteLocalRef(t.classID);
  178. deleteLocalRefs(t.env);
  179. } else {
  180. reportError(className, methodName, signature);
  181. }
  182. return ret;
  183. }
  184. template <typename... Ts>
  185. static std::string callStaticStringMethod(const std::string& className,
  186. const std::string& methodName,
  187. Ts... xs) {
  188. std::string ret;
  189. cocos2d::JniMethodInfo t;
  190. std::string signature = "(" + std::string(getJNISignature(xs...)) + ")Ljava/lang/String;";
  191. if (cocos2d::JniHelper::getStaticMethodInfo(t, className.c_str(), methodName.c_str(), signature.c_str())) {
  192. jstring jret = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID, convert(t, xs)...);
  193. ret = cocos2d::JniHelper::jstring2string(jret);
  194. t.env->DeleteLocalRef(t.classID);
  195. t.env->DeleteLocalRef(jret);
  196. deleteLocalRefs(t.env);
  197. } else {
  198. reportError(className, methodName, signature);
  199. }
  200. return ret;
  201. }
  202. private:
  203. static JNIEnv* cacheEnv(JavaVM* jvm);
  204. static bool getMethodInfo_DefaultClassLoader(JniMethodInfo &methodinfo,
  205. const char *className,
  206. const char *methodName,
  207. const char *paramCode);
  208. static JavaVM* _psJavaVM;
  209. static jobject _activity;
  210. static jstring convert(cocos2d::JniMethodInfo& t, const char* x);
  211. static jstring convert(cocos2d::JniMethodInfo& t, const std::string& x);
  212. template <typename T>
  213. static T convert(cocos2d::JniMethodInfo&, T x) {
  214. return x;
  215. }
  216. static std::unordered_map<JNIEnv*, std::vector<jobject>> localRefs;
  217. static void deleteLocalRefs(JNIEnv* env);
  218. static std::string getJNISignature() {
  219. return "";
  220. }
  221. static std::string getJNISignature(bool) {
  222. return "Z";
  223. }
  224. static std::string getJNISignature(char) {
  225. return "C";
  226. }
  227. static std::string getJNISignature(short) {
  228. return "S";
  229. }
  230. static std::string getJNISignature(int) {
  231. return "I";
  232. }
  233. static std::string getJNISignature(long) {
  234. return "J";
  235. }
  236. static std::string getJNISignature(float) {
  237. return "F";
  238. }
  239. static std::string getJNISignature(double) {
  240. return "D";
  241. }
  242. static std::string getJNISignature(const char*) {
  243. return "Ljava/lang/String;";
  244. }
  245. static std::string getJNISignature(const std::string&) {
  246. return "Ljava/lang/String;";
  247. }
  248. template <typename T>
  249. static std::string getJNISignature(T x) {
  250. // This template should never be instantiated
  251. static_assert(sizeof(x) == 0, "Unsupported argument type");
  252. return "";
  253. }
  254. template <typename T, typename... Ts>
  255. static std::string getJNISignature(T x, Ts... xs) {
  256. return getJNISignature(x) + getJNISignature(xs...);
  257. }
  258. static void reportError(const std::string& className, const std::string& methodName, const std::string& signature);
  259. };
  260. NS_CC_END
  261. #endif // __ANDROID_JNI_HELPER_H__