JniHelper.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #ifndef __ANDROID_JNI_HELPER_H__
  23. #define __ANDROID_JNI_HELPER_H__
  24. #include <jni.h>
  25. #include <string>
  26. #include <vector>
  27. #include <unordered_map>
  28. #include <functional>
  29. #include "base/ccMacros.h"
  30. #include "math/Vec3.h"
  31. //The macro must be used this way to find the native method. The principle is not well understood.
  32. #define JNI_METHOD2(CLASS2,FUNC2) Java_##CLASS2##_##FUNC2
  33. #define JNI_METHOD1(CLASS1,FUNC1) JNI_METHOD2(CLASS1,FUNC1)
  34. NS_CC_BEGIN
  35. typedef struct JniMethodInfo_
  36. {
  37. JNIEnv * env;
  38. jclass classID;
  39. jmethodID methodID;
  40. } JniMethodInfo;
  41. class CC_DLL JniHelper
  42. {
  43. public:
  44. typedef std::unordered_map<JNIEnv *, std::vector<jobject >> LocalRefMapType;
  45. static void setJavaVM(JavaVM *javaVM);
  46. static JavaVM* getJavaVM();
  47. static JNIEnv* getEnv();
  48. static jobject getActivity();
  49. static bool setClassLoaderFrom(jobject activityInstance);
  50. static bool getStaticMethodInfo(JniMethodInfo &methodinfo,
  51. const char *className,
  52. const char *methodName,
  53. const char *paramCode);
  54. static bool getMethodInfo(JniMethodInfo &methodinfo,
  55. const char *className,
  56. const char *methodName,
  57. const char *paramCode);
  58. static std::string jstring2string(jstring str);
  59. static jmethodID loadclassMethod_methodID;
  60. static jobject classloader;
  61. static std::function<void()> classloaderCallback;
  62. template <typename... Ts>
  63. static jobject newObject(const std::string& className, Ts... xs)
  64. {
  65. jobject ret = nullptr;
  66. static const char* methodName = "<init>";
  67. cocos2d::JniMethodInfo t;
  68. std::string signature = "(" + std::string(getJNISignature(xs...)) + ")V";
  69. if (cocos2d::JniHelper::getMethodInfo(t, className.c_str(), methodName, signature.c_str())) {
  70. LocalRefMapType localRefs;
  71. ret = t.env->NewObject(t.classID, t.methodID, convert(t, xs)...);
  72. t.env->DeleteLocalRef(t.classID);
  73. deleteLocalRefs(t.env, localRefs);
  74. } else {
  75. reportError(className, methodName, signature);
  76. }
  77. return ret;
  78. }
  79. template <typename... Ts>
  80. static void callObjectVoidMethod(jobject object,
  81. const std::string& className,
  82. const std::string& methodName,
  83. Ts... xs) {
  84. cocos2d::JniMethodInfo t;
  85. std::string signature = "(" + std::string(getJNISignature(xs...)) + ")V";
  86. if (cocos2d::JniHelper::getMethodInfo(t, className.c_str(), methodName.c_str(), signature.c_str())) {
  87. LocalRefMapType localRefs;
  88. t.env->CallVoidMethod(object, t.methodID, convert(localRefs, t, xs)...);
  89. t.env->DeleteLocalRef(t.classID);
  90. deleteLocalRefs(t.env, localRefs);
  91. } else {
  92. reportError(className, methodName, signature);
  93. }
  94. }
  95. template <typename... Ts>
  96. static float callObjectFloatMethod(jobject object,
  97. const std::string& className,
  98. const std::string& methodName,
  99. Ts... xs) {
  100. float ret = 0.0f;
  101. cocos2d::JniMethodInfo t;
  102. std::string signature = "(" + std::string(getJNISignature(xs...)) + ")F";
  103. if (cocos2d::JniHelper::getMethodInfo(t, className.c_str(), methodName.c_str(), signature.c_str())) {
  104. LocalRefMapType localRefs;
  105. ret = t.env->CallFloatMethod(object, t.methodID, convert(localRefs, t, xs)...);
  106. t.env->DeleteLocalRef(t.classID);
  107. deleteLocalRefs(t.env, localRefs);
  108. } else {
  109. reportError(className, methodName, signature);
  110. }
  111. return ret;
  112. }
  113. template <typename... Ts>
  114. static jbyteArray callObjectByteArrayMethod(jobject object,
  115. const std::string& className,
  116. const std::string& methodName,
  117. Ts... xs) {
  118. jbyteArray ret = nullptr;
  119. cocos2d::JniMethodInfo t;
  120. std::string signature = "(" + std::string(getJNISignature(xs...)) + ")[B";
  121. if (cocos2d::JniHelper::getMethodInfo(t, className.c_str(), methodName.c_str(), signature.c_str())) {
  122. LocalRefMapType localRefs;
  123. ret = (jbyteArray)t.env->CallObjectMethod(object, t.methodID, convert(t, xs)...);
  124. t.env->DeleteLocalRef(t.classID);
  125. deleteLocalRefs(t.env, localRefs);
  126. } else {
  127. reportError(className, methodName, signature);
  128. }
  129. return ret;
  130. }
  131. template <typename... Ts>
  132. static void callStaticVoidMethod(const std::string& className,
  133. const std::string& methodName,
  134. Ts... xs) {
  135. cocos2d::JniMethodInfo t;
  136. std::string signature = "(" + std::string(getJNISignature(xs...)) + ")V";
  137. if (cocos2d::JniHelper::getStaticMethodInfo(t, className.c_str(), methodName.c_str(), signature.c_str())) {
  138. LocalRefMapType localRefs;
  139. t.env->CallStaticVoidMethod(t.classID, t.methodID, convert(localRefs, t, xs)...);
  140. t.env->DeleteLocalRef(t.classID);
  141. deleteLocalRefs(t.env, localRefs);
  142. } else {
  143. reportError(className, methodName, signature);
  144. }
  145. }
  146. template <typename... Ts>
  147. static bool callStaticBooleanMethod(const std::string& className,
  148. const std::string& methodName,
  149. Ts... xs) {
  150. jboolean jret = JNI_FALSE;
  151. cocos2d::JniMethodInfo t;
  152. std::string signature = "(" + std::string(getJNISignature(xs...)) + ")Z";
  153. if (cocos2d::JniHelper::getStaticMethodInfo(t, className.c_str(), methodName.c_str(), signature.c_str())) {
  154. LocalRefMapType localRefs;
  155. jret = t.env->CallStaticBooleanMethod(t.classID, t.methodID, convert(localRefs, t, xs)...);
  156. t.env->DeleteLocalRef(t.classID);
  157. deleteLocalRefs(t.env, localRefs);
  158. } else {
  159. reportError(className, methodName, signature);
  160. }
  161. return (jret == JNI_TRUE);
  162. }
  163. template <typename... Ts>
  164. static int callStaticIntMethod(const std::string& className,
  165. const std::string& methodName,
  166. Ts... xs) {
  167. jint ret = 0;
  168. cocos2d::JniMethodInfo t;
  169. std::string signature = "(" + std::string(getJNISignature(xs...)) + ")I";
  170. if (cocos2d::JniHelper::getStaticMethodInfo(t, className.c_str(), methodName.c_str(), signature.c_str())) {
  171. LocalRefMapType localRefs;
  172. ret = t.env->CallStaticIntMethod(t.classID, t.methodID, convert(t, xs)...);
  173. t.env->DeleteLocalRef(t.classID);
  174. deleteLocalRefs(t.env, localRefs);
  175. } else {
  176. reportError(className, methodName, signature);
  177. }
  178. return ret;
  179. }
  180. template <typename... Ts>
  181. static float callStaticFloatMethod(const std::string& className,
  182. const std::string& methodName,
  183. Ts... xs) {
  184. jfloat ret = 0.0;
  185. cocos2d::JniMethodInfo t;
  186. std::string signature = "(" + std::string(getJNISignature(xs...)) + ")F";
  187. if (cocos2d::JniHelper::getStaticMethodInfo(t, className.c_str(), methodName.c_str(), signature.c_str())) {
  188. LocalRefMapType localRefs;
  189. ret = t.env->CallStaticFloatMethod(t.classID, t.methodID, convert(localRefs, t, xs)...);
  190. t.env->DeleteLocalRef(t.classID);
  191. deleteLocalRefs(t.env, localRefs);
  192. } else {
  193. reportError(className, methodName, signature);
  194. }
  195. return ret;
  196. }
  197. template <typename... Ts>
  198. static float* callStaticFloatArrayMethod(const std::string& className,
  199. const std::string& methodName,
  200. Ts... xs) {
  201. static float ret[32];
  202. cocos2d::JniMethodInfo t;
  203. std::string signature = "(" + std::string(getJNISignature(xs...)) + ")[F";
  204. if (cocos2d::JniHelper::getStaticMethodInfo(t, className.c_str(), methodName.c_str(), signature.c_str())) {
  205. LocalRefMapType localRefs;
  206. jfloatArray array = (jfloatArray) t.env->CallStaticObjectMethod(t.classID, t.methodID, convert(localRefs, t, xs)...);
  207. jsize len = t.env->GetArrayLength(array);
  208. if (len <= 32) {
  209. jfloat* elems = t.env->GetFloatArrayElements(array, 0);
  210. if (elems) {
  211. memcpy(ret, elems, sizeof(float) * len);
  212. t.env->ReleaseFloatArrayElements(array, elems, 0);
  213. };
  214. }
  215. t.env->DeleteLocalRef(t.classID);
  216. deleteLocalRefs(t.env, localRefs);
  217. return &ret[0];
  218. } else {
  219. reportError(className, methodName, signature);
  220. }
  221. return nullptr;
  222. }
  223. template <typename... Ts>
  224. static Vec3 callStaticVec3Method(const std::string& className,
  225. const std::string& methodName,
  226. Ts... xs) {
  227. Vec3 ret;
  228. cocos2d::JniMethodInfo t;
  229. std::string signature = "(" + std::string(getJNISignature(xs...)) + ")[F";
  230. if (cocos2d::JniHelper::getStaticMethodInfo(t, className.c_str(), methodName.c_str(), signature.c_str())) {
  231. LocalRefMapType localRefs;
  232. jfloatArray array = (jfloatArray) t.env->CallStaticObjectMethod(t.classID, t.methodID, convert(localRefs, t, xs)...);
  233. jsize len = t.env->GetArrayLength(array);
  234. if (len == 3) {
  235. jfloat* elems = t.env->GetFloatArrayElements(array, 0);
  236. ret.x = elems[0];
  237. ret.y = elems[1];
  238. ret.z = elems[2];
  239. t.env->ReleaseFloatArrayElements(array, elems, 0);
  240. }
  241. t.env->DeleteLocalRef(t.classID);
  242. deleteLocalRefs(t.env, localRefs);
  243. } else {
  244. reportError(className, methodName, signature);
  245. }
  246. return ret;
  247. }
  248. template <typename... Ts>
  249. static double callStaticDoubleMethod(const std::string& className,
  250. const std::string& methodName,
  251. Ts... xs) {
  252. jdouble ret = 0.0;
  253. cocos2d::JniMethodInfo t;
  254. std::string signature = "(" + std::string(getJNISignature(xs...)) + ")D";
  255. if (cocos2d::JniHelper::getStaticMethodInfo(t, className.c_str(), methodName.c_str(), signature.c_str())) {
  256. LocalRefMapType localRefs;
  257. ret = t.env->CallStaticDoubleMethod(t.classID, t.methodID, convert(localRefs, t, xs)...);
  258. t.env->DeleteLocalRef(t.classID);
  259. deleteLocalRefs(t.env, localRefs);
  260. } else {
  261. reportError(className, methodName, signature);
  262. }
  263. return ret;
  264. }
  265. template <typename... Ts>
  266. static std::string callStaticStringMethod(const std::string& className,
  267. const std::string& methodName,
  268. Ts... xs) {
  269. std::string ret;
  270. cocos2d::JniMethodInfo t;
  271. std::string signature = "(" + std::string(getJNISignature(xs...)) + ")Ljava/lang/String;";
  272. if (cocos2d::JniHelper::getStaticMethodInfo(t, className.c_str(), methodName.c_str(), signature.c_str())) {
  273. LocalRefMapType localRefs;
  274. jstring jret = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID, convert(localRefs, t, xs)...);
  275. ret = cocos2d::JniHelper::jstring2string(jret);
  276. t.env->DeleteLocalRef(t.classID);
  277. t.env->DeleteLocalRef(jret);
  278. deleteLocalRefs(t.env, localRefs);
  279. } else {
  280. reportError(className, methodName, signature);
  281. }
  282. return ret;
  283. }
  284. private:
  285. static JNIEnv* cacheEnv(JavaVM* jvm);
  286. static bool getMethodInfo_DefaultClassLoader(JniMethodInfo &methodinfo,
  287. const char *className,
  288. const char *methodName,
  289. const char *paramCode);
  290. static JavaVM* _psJavaVM;
  291. static jobject _activity;
  292. static jstring convert(LocalRefMapType &localRefs, cocos2d::JniMethodInfo& t, const char* x);
  293. static jstring convert(LocalRefMapType &localRefs, cocos2d::JniMethodInfo& t, const std::string& x);
  294. template <typename T>
  295. static T convert(LocalRefMapType &localRefs, cocos2d::JniMethodInfo&, T x) {
  296. return x;
  297. }
  298. static void deleteLocalRefs(JNIEnv* env, LocalRefMapType &localRefs);
  299. static std::string getJNISignature() {
  300. return "";
  301. }
  302. static std::string getJNISignature(bool) {
  303. return "Z";
  304. }
  305. // jchar is unsigned 16 bits, we do char => jchar conversion on purpose
  306. static std::string getJNISignature(char) {
  307. return "C";
  308. }
  309. static std::string getJNISignature(jshort) {
  310. return "S";
  311. }
  312. static std::string getJNISignature(jint) {
  313. return "I";
  314. }
  315. static std::string getJNISignature(jlong) {
  316. return "J";
  317. }
  318. static std::string getJNISignature(jfloat) {
  319. return "F";
  320. }
  321. static std::string getJNISignature(jdouble) {
  322. return "D";
  323. }
  324. static std::string getJNISignature(jbyteArray) {
  325. return "[B";
  326. }
  327. static std::string getJNISignature(const char*) {
  328. return "Ljava/lang/String;";
  329. }
  330. static std::string getJNISignature(const std::string&) {
  331. return "Ljava/lang/String;";
  332. }
  333. template <typename T>
  334. static std::string getJNISignature(T x) {
  335. // This template should never be instantiated
  336. static_assert(sizeof(x) == 0, "Unsupported argument type");
  337. return "";
  338. }
  339. template <typename T, typename... Ts>
  340. static std::string getJNISignature(T x, Ts... xs) {
  341. return getJNISignature(x) + getJNISignature(xs...);
  342. }
  343. static void reportError(const std::string& className, const std::string& methodName, const std::string& signature);
  344. };
  345. NS_CC_END
  346. #endif // __ANDROID_JNI_HELPER_H__