java_class_wrapper.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416
  1. /**************************************************************************/
  2. /* java_class_wrapper.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "api/java_class_wrapper.h"
  31. #include "string_android.h"
  32. #include "thread_jandroid.h"
  33. bool JavaClass::_call_method(JavaObject *p_instance, const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error, Variant &ret) {
  34. HashMap<StringName, List<MethodInfo>>::Iterator M = methods.find(p_method);
  35. if (!M) {
  36. return false;
  37. }
  38. JNIEnv *env = get_jni_env();
  39. ERR_FAIL_NULL_V(env, false);
  40. MethodInfo *method = nullptr;
  41. for (MethodInfo &E : M->value) {
  42. if (!p_instance && !E._static && !E._constructor) {
  43. r_error.error = Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL;
  44. continue;
  45. }
  46. int pc = E.param_types.size();
  47. if (p_argcount < pc) {
  48. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  49. r_error.expected = pc;
  50. continue;
  51. }
  52. if (p_argcount > pc) {
  53. r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  54. r_error.expected = pc;
  55. continue;
  56. }
  57. uint32_t *ptypes = E.param_types.ptrw();
  58. bool valid = true;
  59. for (int i = 0; i < pc; i++) {
  60. Variant::Type arg_expected = Variant::NIL;
  61. switch (ptypes[i]) {
  62. case ARG_TYPE_VOID: {
  63. //bug?
  64. } break;
  65. case ARG_TYPE_BOOLEAN: {
  66. if (p_args[i]->get_type() != Variant::BOOL) {
  67. arg_expected = Variant::BOOL;
  68. }
  69. } break;
  70. case ARG_NUMBER_CLASS_BIT | ARG_TYPE_BYTE:
  71. case ARG_NUMBER_CLASS_BIT | ARG_TYPE_CHAR:
  72. case ARG_NUMBER_CLASS_BIT | ARG_TYPE_SHORT:
  73. case ARG_NUMBER_CLASS_BIT | ARG_TYPE_INT:
  74. case ARG_NUMBER_CLASS_BIT | ARG_TYPE_LONG:
  75. case ARG_TYPE_BYTE:
  76. case ARG_TYPE_CHAR:
  77. case ARG_TYPE_SHORT:
  78. case ARG_TYPE_INT:
  79. case ARG_TYPE_LONG: {
  80. if (!p_args[i]->is_num()) {
  81. arg_expected = Variant::INT;
  82. }
  83. } break;
  84. case ARG_NUMBER_CLASS_BIT | ARG_TYPE_FLOAT:
  85. case ARG_NUMBER_CLASS_BIT | ARG_TYPE_DOUBLE:
  86. case ARG_TYPE_FLOAT:
  87. case ARG_TYPE_DOUBLE: {
  88. if (!p_args[i]->is_num()) {
  89. arg_expected = Variant::FLOAT;
  90. }
  91. } break;
  92. case ARG_TYPE_STRING: {
  93. if (!p_args[i]->is_string()) {
  94. arg_expected = Variant::STRING;
  95. }
  96. } break;
  97. case ARG_TYPE_CLASS: {
  98. if (p_args[i]->get_type() != Variant::OBJECT && p_args[i]->get_type() != Variant::NIL) {
  99. arg_expected = Variant::OBJECT;
  100. } else {
  101. Ref<RefCounted> ref = *p_args[i];
  102. if (ref.is_valid()) {
  103. if (Object::cast_to<JavaObject>(ref.ptr())) {
  104. Ref<JavaObject> jo = ref;
  105. //could be faster
  106. String cn = E.param_sigs[i].operator String();
  107. if (cn.begins_with("L") && cn.ends_with(";")) {
  108. cn = cn.substr(1, cn.length() - 2);
  109. }
  110. jclass c = env->FindClass(cn.utf8().get_data());
  111. if (!c || !env->IsInstanceOf(jo->instance, c)) {
  112. arg_expected = Variant::OBJECT;
  113. } else {
  114. //ok
  115. }
  116. } else {
  117. arg_expected = Variant::OBJECT;
  118. }
  119. }
  120. }
  121. } break;
  122. default: {
  123. if (p_args[i]->get_type() != Variant::ARRAY) {
  124. arg_expected = Variant::ARRAY;
  125. }
  126. } break;
  127. }
  128. if (arg_expected != Variant::NIL) {
  129. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  130. r_error.argument = i;
  131. r_error.expected = arg_expected;
  132. valid = false;
  133. break;
  134. }
  135. }
  136. if (!valid) {
  137. continue;
  138. }
  139. method = &E;
  140. break;
  141. }
  142. if (!method) {
  143. return true; //no version convinces
  144. }
  145. r_error.error = Callable::CallError::CALL_OK;
  146. jvalue *argv = nullptr;
  147. if (method->param_types.size()) {
  148. argv = (jvalue *)alloca(sizeof(jvalue) * method->param_types.size());
  149. }
  150. List<jobject> to_free;
  151. for (int i = 0; i < method->param_types.size(); i++) {
  152. switch (method->param_types[i]) {
  153. case ARG_TYPE_VOID: {
  154. //can't happen
  155. argv[i].l = nullptr; //I hope this works
  156. } break;
  157. case ARG_TYPE_BOOLEAN: {
  158. argv[i].z = *p_args[i];
  159. } break;
  160. case ARG_TYPE_BYTE: {
  161. argv[i].b = *p_args[i];
  162. } break;
  163. case ARG_TYPE_CHAR: {
  164. argv[i].c = *p_args[i];
  165. } break;
  166. case ARG_TYPE_SHORT: {
  167. argv[i].s = *p_args[i];
  168. } break;
  169. case ARG_TYPE_INT: {
  170. argv[i].i = *p_args[i];
  171. } break;
  172. case ARG_TYPE_LONG: {
  173. argv[i].j = (int64_t)*p_args[i];
  174. } break;
  175. case ARG_TYPE_FLOAT: {
  176. argv[i].f = *p_args[i];
  177. } break;
  178. case ARG_TYPE_DOUBLE: {
  179. argv[i].d = *p_args[i];
  180. } break;
  181. case ARG_NUMBER_CLASS_BIT | ARG_TYPE_BOOLEAN: {
  182. jclass bclass = env->FindClass("java/lang/Boolean");
  183. jmethodID ctor = env->GetMethodID(bclass, "<init>", "(Z)V");
  184. jvalue val;
  185. val.z = (bool)(*p_args[i]);
  186. jobject obj = env->NewObjectA(bclass, ctor, &val);
  187. argv[i].l = obj;
  188. to_free.push_back(obj);
  189. } break;
  190. case ARG_NUMBER_CLASS_BIT | ARG_TYPE_BYTE: {
  191. jclass bclass = env->FindClass("java/lang/Byte");
  192. jmethodID ctor = env->GetMethodID(bclass, "<init>", "(B)V");
  193. jvalue val;
  194. val.b = (int)(*p_args[i]);
  195. jobject obj = env->NewObjectA(bclass, ctor, &val);
  196. argv[i].l = obj;
  197. to_free.push_back(obj);
  198. } break;
  199. case ARG_NUMBER_CLASS_BIT | ARG_TYPE_CHAR: {
  200. jclass bclass = env->FindClass("java/lang/Character");
  201. jmethodID ctor = env->GetMethodID(bclass, "<init>", "(C)V");
  202. jvalue val;
  203. val.c = (int)(*p_args[i]);
  204. jobject obj = env->NewObjectA(bclass, ctor, &val);
  205. argv[i].l = obj;
  206. to_free.push_back(obj);
  207. } break;
  208. case ARG_NUMBER_CLASS_BIT | ARG_TYPE_SHORT: {
  209. jclass bclass = env->FindClass("java/lang/Short");
  210. jmethodID ctor = env->GetMethodID(bclass, "<init>", "(S)V");
  211. jvalue val;
  212. val.s = (int)(*p_args[i]);
  213. jobject obj = env->NewObjectA(bclass, ctor, &val);
  214. argv[i].l = obj;
  215. to_free.push_back(obj);
  216. } break;
  217. case ARG_NUMBER_CLASS_BIT | ARG_TYPE_INT: {
  218. jclass bclass = env->FindClass("java/lang/Integer");
  219. jmethodID ctor = env->GetMethodID(bclass, "<init>", "(I)V");
  220. jvalue val;
  221. val.i = (int)(*p_args[i]);
  222. jobject obj = env->NewObjectA(bclass, ctor, &val);
  223. argv[i].l = obj;
  224. to_free.push_back(obj);
  225. } break;
  226. case ARG_NUMBER_CLASS_BIT | ARG_TYPE_LONG: {
  227. jclass bclass = env->FindClass("java/lang/Long");
  228. jmethodID ctor = env->GetMethodID(bclass, "<init>", "(J)V");
  229. jvalue val;
  230. val.j = (int64_t)(*p_args[i]);
  231. jobject obj = env->NewObjectA(bclass, ctor, &val);
  232. argv[i].l = obj;
  233. to_free.push_back(obj);
  234. } break;
  235. case ARG_NUMBER_CLASS_BIT | ARG_TYPE_FLOAT: {
  236. jclass bclass = env->FindClass("java/lang/Float");
  237. jmethodID ctor = env->GetMethodID(bclass, "<init>", "(F)V");
  238. jvalue val;
  239. val.f = (float)(*p_args[i]);
  240. jobject obj = env->NewObjectA(bclass, ctor, &val);
  241. argv[i].l = obj;
  242. to_free.push_back(obj);
  243. } break;
  244. case ARG_NUMBER_CLASS_BIT | ARG_TYPE_DOUBLE: {
  245. jclass bclass = env->FindClass("java/lang/Double");
  246. jmethodID ctor = env->GetMethodID(bclass, "<init>", "(D)V");
  247. jvalue val;
  248. val.d = (double)(*p_args[i]);
  249. jobject obj = env->NewObjectA(bclass, ctor, &val);
  250. argv[i].l = obj;
  251. to_free.push_back(obj);
  252. } break;
  253. case ARG_TYPE_STRING: {
  254. String s = *p_args[i];
  255. jstring jStr = env->NewStringUTF(s.utf8().get_data());
  256. argv[i].l = jStr;
  257. to_free.push_back(jStr);
  258. } break;
  259. case ARG_TYPE_CLASS: {
  260. Ref<JavaObject> jo = *p_args[i];
  261. if (jo.is_valid()) {
  262. argv[i].l = jo->instance;
  263. } else {
  264. argv[i].l = nullptr; //I hope this works
  265. }
  266. } break;
  267. case ARG_ARRAY_BIT | ARG_TYPE_BOOLEAN: {
  268. Array arr = *p_args[i];
  269. jbooleanArray a = env->NewBooleanArray(arr.size());
  270. for (int j = 0; j < arr.size(); j++) {
  271. jboolean val = arr[j];
  272. env->SetBooleanArrayRegion(a, j, 1, &val);
  273. }
  274. argv[i].l = a;
  275. to_free.push_back(a);
  276. } break;
  277. case ARG_ARRAY_BIT | ARG_TYPE_BYTE: {
  278. Array arr = *p_args[i];
  279. jbyteArray a = env->NewByteArray(arr.size());
  280. for (int j = 0; j < arr.size(); j++) {
  281. jbyte val = arr[j];
  282. env->SetByteArrayRegion(a, j, 1, &val);
  283. }
  284. argv[i].l = a;
  285. to_free.push_back(a);
  286. } break;
  287. case ARG_ARRAY_BIT | ARG_TYPE_CHAR: {
  288. Array arr = *p_args[i];
  289. jcharArray a = env->NewCharArray(arr.size());
  290. for (int j = 0; j < arr.size(); j++) {
  291. jchar val = arr[j];
  292. env->SetCharArrayRegion(a, j, 1, &val);
  293. }
  294. argv[i].l = a;
  295. to_free.push_back(a);
  296. } break;
  297. case ARG_ARRAY_BIT | ARG_TYPE_SHORT: {
  298. Array arr = *p_args[i];
  299. jshortArray a = env->NewShortArray(arr.size());
  300. for (int j = 0; j < arr.size(); j++) {
  301. jshort val = arr[j];
  302. env->SetShortArrayRegion(a, j, 1, &val);
  303. }
  304. argv[i].l = a;
  305. to_free.push_back(a);
  306. } break;
  307. case ARG_ARRAY_BIT | ARG_TYPE_INT: {
  308. Array arr = *p_args[i];
  309. jintArray a = env->NewIntArray(arr.size());
  310. for (int j = 0; j < arr.size(); j++) {
  311. jint val = arr[j];
  312. env->SetIntArrayRegion(a, j, 1, &val);
  313. }
  314. argv[i].l = a;
  315. to_free.push_back(a);
  316. } break;
  317. case ARG_ARRAY_BIT | ARG_TYPE_LONG: {
  318. Array arr = *p_args[i];
  319. jlongArray a = env->NewLongArray(arr.size());
  320. for (int j = 0; j < arr.size(); j++) {
  321. jlong val = (int64_t)arr[j];
  322. env->SetLongArrayRegion(a, j, 1, &val);
  323. }
  324. argv[i].l = a;
  325. to_free.push_back(a);
  326. } break;
  327. case ARG_ARRAY_BIT | ARG_TYPE_FLOAT: {
  328. Array arr = *p_args[i];
  329. jfloatArray a = env->NewFloatArray(arr.size());
  330. for (int j = 0; j < arr.size(); j++) {
  331. jfloat val = arr[j];
  332. env->SetFloatArrayRegion(a, j, 1, &val);
  333. }
  334. argv[i].l = a;
  335. to_free.push_back(a);
  336. } break;
  337. case ARG_ARRAY_BIT | ARG_TYPE_DOUBLE: {
  338. Array arr = *p_args[i];
  339. jdoubleArray a = env->NewDoubleArray(arr.size());
  340. for (int j = 0; j < arr.size(); j++) {
  341. jdouble val = arr[j];
  342. env->SetDoubleArrayRegion(a, j, 1, &val);
  343. }
  344. argv[i].l = a;
  345. to_free.push_back(a);
  346. } break;
  347. case ARG_ARRAY_BIT | ARG_TYPE_STRING: {
  348. Array arr = *p_args[i];
  349. jobjectArray a = env->NewObjectArray(arr.size(), env->FindClass("java/lang/String"), nullptr);
  350. for (int j = 0; j < arr.size(); j++) {
  351. String s = arr[j];
  352. jstring jStr = env->NewStringUTF(s.utf8().get_data());
  353. env->SetObjectArrayElement(a, j, jStr);
  354. to_free.push_back(jStr);
  355. }
  356. argv[i].l = a;
  357. to_free.push_back(a);
  358. } break;
  359. case ARG_ARRAY_BIT | ARG_TYPE_CLASS: {
  360. argv[i].l = nullptr;
  361. } break;
  362. }
  363. }
  364. r_error.error = Callable::CallError::CALL_OK;
  365. bool success = true;
  366. switch (method->return_type) {
  367. case ARG_TYPE_VOID: {
  368. if (method->_static) {
  369. env->CallStaticVoidMethodA(_class, method->method, argv);
  370. } else {
  371. env->CallVoidMethodA(p_instance->instance, method->method, argv);
  372. }
  373. ret = Variant();
  374. } break;
  375. case ARG_TYPE_BOOLEAN: {
  376. if (method->_static) {
  377. ret = env->CallStaticBooleanMethodA(_class, method->method, argv);
  378. } else {
  379. ret = env->CallBooleanMethodA(p_instance->instance, method->method, argv);
  380. }
  381. } break;
  382. case ARG_TYPE_BYTE: {
  383. if (method->_static) {
  384. ret = env->CallStaticByteMethodA(_class, method->method, argv);
  385. } else {
  386. ret = env->CallByteMethodA(p_instance->instance, method->method, argv);
  387. }
  388. } break;
  389. case ARG_TYPE_CHAR: {
  390. if (method->_static) {
  391. ret = env->CallStaticCharMethodA(_class, method->method, argv);
  392. } else {
  393. ret = env->CallCharMethodA(p_instance->instance, method->method, argv);
  394. }
  395. } break;
  396. case ARG_TYPE_SHORT: {
  397. if (method->_static) {
  398. ret = env->CallStaticShortMethodA(_class, method->method, argv);
  399. } else {
  400. ret = env->CallShortMethodA(p_instance->instance, method->method, argv);
  401. }
  402. } break;
  403. case ARG_TYPE_INT: {
  404. if (method->_static) {
  405. ret = env->CallStaticIntMethodA(_class, method->method, argv);
  406. } else {
  407. ret = env->CallIntMethodA(p_instance->instance, method->method, argv);
  408. }
  409. } break;
  410. case ARG_TYPE_LONG: {
  411. if (method->_static) {
  412. ret = (int64_t)env->CallStaticLongMethodA(_class, method->method, argv);
  413. } else {
  414. ret = (int64_t)env->CallLongMethodA(p_instance->instance, method->method, argv);
  415. }
  416. } break;
  417. case ARG_TYPE_FLOAT: {
  418. if (method->_static) {
  419. ret = env->CallStaticFloatMethodA(_class, method->method, argv);
  420. } else {
  421. ret = env->CallFloatMethodA(p_instance->instance, method->method, argv);
  422. }
  423. } break;
  424. case ARG_TYPE_DOUBLE: {
  425. if (method->_static) {
  426. ret = env->CallStaticDoubleMethodA(_class, method->method, argv);
  427. } else {
  428. ret = env->CallDoubleMethodA(p_instance->instance, method->method, argv);
  429. }
  430. } break;
  431. default: {
  432. jobject obj;
  433. if (method->_constructor) {
  434. obj = env->NewObject(_class, method->method, argv);
  435. } else if (method->_static) {
  436. obj = env->CallStaticObjectMethodA(_class, method->method, argv);
  437. } else {
  438. obj = env->CallObjectMethodA(p_instance->instance, method->method, argv);
  439. }
  440. if (!obj) {
  441. ret = Variant();
  442. } else {
  443. if (!_convert_object_to_variant(env, obj, ret, method->return_type)) {
  444. ret = Variant();
  445. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  446. success = false;
  447. }
  448. env->DeleteLocalRef(obj);
  449. }
  450. } break;
  451. }
  452. for (jobject &E : to_free) {
  453. env->DeleteLocalRef(E);
  454. }
  455. return success;
  456. }
  457. Variant JavaClass::callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  458. Variant ret;
  459. String method = (p_method == java_constructor_name) ? "<init>" : p_method;
  460. bool found = _call_method(nullptr, method, p_args, p_argcount, r_error, ret);
  461. if (found) {
  462. return ret;
  463. }
  464. return RefCounted::callp(p_method, p_args, p_argcount, r_error);
  465. }
  466. String JavaClass::get_java_class_name() const {
  467. return java_class_name;
  468. }
  469. TypedArray<Dictionary> JavaClass::get_java_method_list() const {
  470. TypedArray<Dictionary> method_list;
  471. for (const KeyValue<StringName, List<MethodInfo>> &item : methods) {
  472. for (const MethodInfo &mi : item.value) {
  473. Dictionary method;
  474. method["name"] = mi._constructor ? java_constructor_name : String(item.key);
  475. method["id"] = (uint64_t)mi.method;
  476. method["default_args"] = Array();
  477. method["flags"] = METHOD_FLAGS_DEFAULT & (mi._static || mi._constructor ? METHOD_FLAG_STATIC : METHOD_FLAG_NORMAL);
  478. {
  479. Array a;
  480. for (uint32_t argtype : mi.param_types) {
  481. Dictionary d;
  482. Variant::Type t = Variant::NIL;
  483. float likelihood = 0.0;
  484. _convert_to_variant_type(argtype, t, likelihood);
  485. d["type"] = t;
  486. if (t == Variant::OBJECT) {
  487. d["hint"] = PROPERTY_HINT_RESOURCE_TYPE;
  488. d["hint_string"] = "JavaObject";
  489. } else {
  490. d["hint"] = 0;
  491. d["hint_string"] = "";
  492. }
  493. a.push_back(d);
  494. }
  495. method["args"] = a;
  496. }
  497. {
  498. Dictionary d;
  499. if (mi._constructor) {
  500. d["type"] = Variant::OBJECT;
  501. d["hint"] = PROPERTY_HINT_RESOURCE_TYPE;
  502. d["hint_string"] = "JavaObject";
  503. } else {
  504. Variant::Type t = Variant::NIL;
  505. float likelihood = 0.0;
  506. _convert_to_variant_type(mi.return_type, t, likelihood);
  507. d["type"] = t;
  508. if (t == Variant::OBJECT) {
  509. d["hint"] = PROPERTY_HINT_RESOURCE_TYPE;
  510. d["hint_string"] = "JavaObject";
  511. } else {
  512. d["hint"] = 0;
  513. d["hint_string"] = "";
  514. }
  515. }
  516. method["return_type"] = d;
  517. }
  518. method_list.push_back(method);
  519. }
  520. }
  521. return method_list;
  522. }
  523. Ref<JavaClass> JavaClass::get_java_parent_class() const {
  524. ERR_FAIL_NULL_V(_class, Ref<JavaClass>());
  525. JNIEnv *env = get_jni_env();
  526. ERR_FAIL_NULL_V(env, Ref<JavaClass>());
  527. jclass superclass = (jclass)env->CallObjectMethod(_class, JavaClassWrapper::singleton->Class_getSuperclass);
  528. if (!superclass) {
  529. return Ref<JavaClass>();
  530. }
  531. Ref<JavaClass> ret = JavaClassWrapper::singleton->wrap_jclass(superclass);
  532. env->DeleteLocalRef(superclass);
  533. return ret;
  534. }
  535. String JavaClass::to_string() {
  536. return "<JavaClass:" + java_class_name + ">";
  537. }
  538. JavaClass::JavaClass() {
  539. }
  540. JavaClass::~JavaClass() {
  541. if (_class) {
  542. JNIEnv *env = get_jni_env();
  543. ERR_FAIL_NULL(env);
  544. env->DeleteGlobalRef(_class);
  545. }
  546. }
  547. /////////////////////
  548. Variant JavaObject::callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  549. if (instance) {
  550. Ref<JavaClass> c = base_class;
  551. while (c.is_valid()) {
  552. Variant ret;
  553. bool found = c->_call_method(this, p_method, p_args, p_argcount, r_error, ret);
  554. if (found) {
  555. return ret;
  556. }
  557. c = c->get_java_parent_class();
  558. }
  559. }
  560. return RefCounted::callp(p_method, p_args, p_argcount, r_error);
  561. }
  562. Ref<JavaClass> JavaObject::get_java_class() const {
  563. return base_class;
  564. }
  565. String JavaObject::to_string() {
  566. if (base_class.is_valid() && instance) {
  567. return "<JavaObject:" + base_class->java_class_name + " \"" + (String)call("toString") + "\">";
  568. }
  569. return RefCounted::to_string();
  570. }
  571. JavaObject::JavaObject() {
  572. }
  573. JavaObject::JavaObject(const Ref<JavaClass> &p_base, jobject p_instance) {
  574. JNIEnv *env = get_jni_env();
  575. ERR_FAIL_NULL(env);
  576. base_class = p_base;
  577. instance = env->NewGlobalRef(p_instance);
  578. }
  579. JavaObject::~JavaObject() {
  580. if (instance) {
  581. JNIEnv *env = get_jni_env();
  582. ERR_FAIL_NULL(env);
  583. env->DeleteGlobalRef(instance);
  584. }
  585. }
  586. ////////////////////
  587. bool JavaClassWrapper::_get_type_sig(JNIEnv *env, jobject obj, uint32_t &sig, String &strsig) {
  588. jstring name2 = (jstring)env->CallObjectMethod(obj, Class_getName);
  589. String str_type = jstring_to_string(name2, env);
  590. env->DeleteLocalRef(name2);
  591. uint32_t t = 0;
  592. if (str_type.begins_with("[")) {
  593. t = JavaClass::ARG_ARRAY_BIT;
  594. strsig = "[";
  595. str_type = str_type.substr(1, str_type.length() - 1);
  596. if (str_type.begins_with("[")) {
  597. print_line("Nested arrays not supported for type: " + str_type);
  598. return false;
  599. }
  600. if (str_type.begins_with("L")) {
  601. str_type = str_type.substr(1, str_type.length() - 2); //ok it's a class
  602. }
  603. }
  604. if (str_type == "void" || str_type == "V") {
  605. t |= JavaClass::ARG_TYPE_VOID;
  606. strsig += "V";
  607. } else if (str_type == "boolean" || str_type == "Z") {
  608. t |= JavaClass::ARG_TYPE_BOOLEAN;
  609. strsig += "Z";
  610. } else if (str_type == "byte" || str_type == "B") {
  611. t |= JavaClass::ARG_TYPE_BYTE;
  612. strsig += "B";
  613. } else if (str_type == "char" || str_type == "C") {
  614. t |= JavaClass::ARG_TYPE_CHAR;
  615. strsig += "C";
  616. } else if (str_type == "short" || str_type == "S") {
  617. t |= JavaClass::ARG_TYPE_SHORT;
  618. strsig += "S";
  619. } else if (str_type == "int" || str_type == "I") {
  620. t |= JavaClass::ARG_TYPE_INT;
  621. strsig += "I";
  622. } else if (str_type == "long" || str_type == "J") {
  623. t |= JavaClass::ARG_TYPE_LONG;
  624. strsig += "J";
  625. } else if (str_type == "float" || str_type == "F") {
  626. t |= JavaClass::ARG_TYPE_FLOAT;
  627. strsig += "F";
  628. } else if (str_type == "double" || str_type == "D") {
  629. t |= JavaClass::ARG_TYPE_DOUBLE;
  630. strsig += "D";
  631. } else if (str_type == "java.lang.String") {
  632. t |= JavaClass::ARG_TYPE_STRING;
  633. strsig += "Ljava/lang/String;";
  634. } else if (str_type == "java.lang.Boolean") {
  635. t |= JavaClass::ARG_TYPE_BOOLEAN | JavaClass::ARG_NUMBER_CLASS_BIT;
  636. strsig += "Ljava/lang/Boolean;";
  637. } else if (str_type == "java.lang.Byte") {
  638. t |= JavaClass::ARG_TYPE_BYTE | JavaClass::ARG_NUMBER_CLASS_BIT;
  639. strsig += "Ljava/lang/Byte;";
  640. } else if (str_type == "java.lang.Character") {
  641. t |= JavaClass::ARG_TYPE_CHAR | JavaClass::ARG_NUMBER_CLASS_BIT;
  642. strsig += "Ljava/lang/Character;";
  643. } else if (str_type == "java.lang.Short") {
  644. t |= JavaClass::ARG_TYPE_SHORT | JavaClass::ARG_NUMBER_CLASS_BIT;
  645. strsig += "Ljava/lang/Short;";
  646. } else if (str_type == "java.lang.Integer") {
  647. t |= JavaClass::ARG_TYPE_INT | JavaClass::ARG_NUMBER_CLASS_BIT;
  648. strsig += "Ljava/lang/Integer;";
  649. } else if (str_type == "java.lang.Long") {
  650. t |= JavaClass::ARG_TYPE_LONG | JavaClass::ARG_NUMBER_CLASS_BIT;
  651. strsig += "Ljava/lang/Long;";
  652. } else if (str_type == "java.lang.Float") {
  653. t |= JavaClass::ARG_TYPE_FLOAT | JavaClass::ARG_NUMBER_CLASS_BIT;
  654. strsig += "Ljava/lang/Float;";
  655. } else if (str_type == "java.lang.Double") {
  656. t |= JavaClass::ARG_TYPE_DOUBLE | JavaClass::ARG_NUMBER_CLASS_BIT;
  657. strsig += "Ljava/lang/Double;";
  658. } else {
  659. //a class likely
  660. strsig += "L" + str_type.replace(".", "/") + ";";
  661. t |= JavaClass::ARG_TYPE_CLASS;
  662. }
  663. sig = t;
  664. return true;
  665. }
  666. bool JavaClass::_convert_object_to_variant(JNIEnv *env, jobject obj, Variant &var, uint32_t p_sig) {
  667. if (!obj) {
  668. var = Variant(); //seems null is just null...
  669. return true;
  670. }
  671. switch (p_sig) {
  672. case ARG_TYPE_VOID: {
  673. return Variant();
  674. } break;
  675. case ARG_TYPE_BOOLEAN | ARG_NUMBER_CLASS_BIT: {
  676. var = env->CallBooleanMethod(obj, JavaClassWrapper::singleton->Boolean_booleanValue);
  677. return true;
  678. } break;
  679. case ARG_TYPE_BYTE | ARG_NUMBER_CLASS_BIT: {
  680. var = env->CallByteMethod(obj, JavaClassWrapper::singleton->Byte_byteValue);
  681. return true;
  682. } break;
  683. case ARG_TYPE_CHAR | ARG_NUMBER_CLASS_BIT: {
  684. var = env->CallCharMethod(obj, JavaClassWrapper::singleton->Character_characterValue);
  685. return true;
  686. } break;
  687. case ARG_TYPE_SHORT | ARG_NUMBER_CLASS_BIT: {
  688. var = env->CallShortMethod(obj, JavaClassWrapper::singleton->Short_shortValue);
  689. return true;
  690. } break;
  691. case ARG_TYPE_INT | ARG_NUMBER_CLASS_BIT: {
  692. var = env->CallIntMethod(obj, JavaClassWrapper::singleton->Integer_integerValue);
  693. return true;
  694. } break;
  695. case ARG_TYPE_LONG | ARG_NUMBER_CLASS_BIT: {
  696. var = (int64_t)env->CallLongMethod(obj, JavaClassWrapper::singleton->Long_longValue);
  697. return true;
  698. } break;
  699. case ARG_TYPE_FLOAT | ARG_NUMBER_CLASS_BIT: {
  700. var = env->CallFloatMethod(obj, JavaClassWrapper::singleton->Float_floatValue);
  701. return true;
  702. } break;
  703. case ARG_TYPE_DOUBLE | ARG_NUMBER_CLASS_BIT: {
  704. var = env->CallDoubleMethod(obj, JavaClassWrapper::singleton->Double_doubleValue);
  705. return true;
  706. } break;
  707. case ARG_TYPE_STRING: {
  708. var = jstring_to_string((jstring)obj, env);
  709. return true;
  710. } break;
  711. case ARG_TYPE_CLASS: {
  712. jclass java_class = env->GetObjectClass(obj);
  713. Ref<JavaClass> java_class_wrapped = JavaClassWrapper::singleton->wrap_jclass(java_class);
  714. env->DeleteLocalRef(java_class);
  715. if (java_class_wrapped.is_valid()) {
  716. Ref<JavaObject> ret = Ref<JavaObject>(memnew(JavaObject(java_class_wrapped, obj)));
  717. var = ret;
  718. return true;
  719. }
  720. return false;
  721. } break;
  722. case ARG_ARRAY_BIT | ARG_TYPE_VOID: {
  723. var = Array(); // ?
  724. return true;
  725. } break;
  726. case ARG_ARRAY_BIT | ARG_TYPE_BOOLEAN: {
  727. Array ret;
  728. jobjectArray arr = (jobjectArray)obj;
  729. int count = env->GetArrayLength(arr);
  730. for (int i = 0; i < count; i++) {
  731. jboolean val;
  732. env->GetBooleanArrayRegion((jbooleanArray)arr, 0, 1, &val);
  733. ret.push_back(val);
  734. }
  735. var = ret;
  736. return true;
  737. } break;
  738. case ARG_ARRAY_BIT | ARG_TYPE_BYTE: {
  739. Array ret;
  740. jobjectArray arr = (jobjectArray)obj;
  741. int count = env->GetArrayLength(arr);
  742. for (int i = 0; i < count; i++) {
  743. jbyte val;
  744. env->GetByteArrayRegion((jbyteArray)arr, 0, 1, &val);
  745. ret.push_back(val);
  746. }
  747. var = ret;
  748. return true;
  749. } break;
  750. case ARG_ARRAY_BIT | ARG_TYPE_CHAR: {
  751. Array ret;
  752. jobjectArray arr = (jobjectArray)obj;
  753. int count = env->GetArrayLength(arr);
  754. for (int i = 0; i < count; i++) {
  755. jchar val;
  756. env->GetCharArrayRegion((jcharArray)arr, 0, 1, &val);
  757. ret.push_back(val);
  758. }
  759. var = ret;
  760. return true;
  761. } break;
  762. case ARG_ARRAY_BIT | ARG_TYPE_SHORT: {
  763. Array ret;
  764. jobjectArray arr = (jobjectArray)obj;
  765. int count = env->GetArrayLength(arr);
  766. for (int i = 0; i < count; i++) {
  767. jshort val;
  768. env->GetShortArrayRegion((jshortArray)arr, 0, 1, &val);
  769. ret.push_back(val);
  770. }
  771. var = ret;
  772. return true;
  773. } break;
  774. case ARG_ARRAY_BIT | ARG_TYPE_INT: {
  775. Array ret;
  776. jobjectArray arr = (jobjectArray)obj;
  777. int count = env->GetArrayLength(arr);
  778. for (int i = 0; i < count; i++) {
  779. jint val;
  780. env->GetIntArrayRegion((jintArray)arr, 0, 1, &val);
  781. ret.push_back(val);
  782. }
  783. var = ret;
  784. return true;
  785. } break;
  786. case ARG_ARRAY_BIT | ARG_TYPE_LONG: {
  787. Array ret;
  788. jobjectArray arr = (jobjectArray)obj;
  789. int count = env->GetArrayLength(arr);
  790. for (int i = 0; i < count; i++) {
  791. jlong val;
  792. env->GetLongArrayRegion((jlongArray)arr, 0, 1, &val);
  793. ret.push_back((int64_t)val);
  794. }
  795. var = ret;
  796. return true;
  797. } break;
  798. case ARG_ARRAY_BIT | ARG_TYPE_FLOAT: {
  799. Array ret;
  800. jobjectArray arr = (jobjectArray)obj;
  801. int count = env->GetArrayLength(arr);
  802. for (int i = 0; i < count; i++) {
  803. jfloat val;
  804. env->GetFloatArrayRegion((jfloatArray)arr, 0, 1, &val);
  805. ret.push_back(val);
  806. }
  807. var = ret;
  808. return true;
  809. } break;
  810. case ARG_ARRAY_BIT | ARG_TYPE_DOUBLE: {
  811. Array ret;
  812. jobjectArray arr = (jobjectArray)obj;
  813. int count = env->GetArrayLength(arr);
  814. for (int i = 0; i < count; i++) {
  815. jdouble val;
  816. env->GetDoubleArrayRegion((jdoubleArray)arr, 0, 1, &val);
  817. ret.push_back(val);
  818. }
  819. var = ret;
  820. return true;
  821. } break;
  822. case ARG_NUMBER_CLASS_BIT | ARG_ARRAY_BIT | ARG_TYPE_BOOLEAN: {
  823. Array ret;
  824. jobjectArray arr = (jobjectArray)obj;
  825. int count = env->GetArrayLength(arr);
  826. for (int i = 0; i < count; i++) {
  827. jobject o = env->GetObjectArrayElement(arr, i);
  828. if (!o) {
  829. ret.push_back(Variant());
  830. } else {
  831. bool val = env->CallBooleanMethod(o, JavaClassWrapper::singleton->Boolean_booleanValue);
  832. ret.push_back(val);
  833. }
  834. env->DeleteLocalRef(o);
  835. }
  836. var = ret;
  837. return true;
  838. } break;
  839. case ARG_NUMBER_CLASS_BIT | ARG_ARRAY_BIT | ARG_TYPE_BYTE: {
  840. Array ret;
  841. jobjectArray arr = (jobjectArray)obj;
  842. int count = env->GetArrayLength(arr);
  843. for (int i = 0; i < count; i++) {
  844. jobject o = env->GetObjectArrayElement(arr, i);
  845. if (!o) {
  846. ret.push_back(Variant());
  847. } else {
  848. int val = env->CallByteMethod(o, JavaClassWrapper::singleton->Byte_byteValue);
  849. ret.push_back(val);
  850. }
  851. env->DeleteLocalRef(o);
  852. }
  853. var = ret;
  854. return true;
  855. } break;
  856. case ARG_NUMBER_CLASS_BIT | ARG_ARRAY_BIT | ARG_TYPE_CHAR: {
  857. Array ret;
  858. jobjectArray arr = (jobjectArray)obj;
  859. int count = env->GetArrayLength(arr);
  860. for (int i = 0; i < count; i++) {
  861. jobject o = env->GetObjectArrayElement(arr, i);
  862. if (!o) {
  863. ret.push_back(Variant());
  864. } else {
  865. int val = env->CallCharMethod(o, JavaClassWrapper::singleton->Character_characterValue);
  866. ret.push_back(val);
  867. }
  868. env->DeleteLocalRef(o);
  869. }
  870. var = ret;
  871. return true;
  872. } break;
  873. case ARG_NUMBER_CLASS_BIT | ARG_ARRAY_BIT | ARG_TYPE_SHORT: {
  874. Array ret;
  875. jobjectArray arr = (jobjectArray)obj;
  876. int count = env->GetArrayLength(arr);
  877. for (int i = 0; i < count; i++) {
  878. jobject o = env->GetObjectArrayElement(arr, i);
  879. if (!o) {
  880. ret.push_back(Variant());
  881. } else {
  882. int val = env->CallShortMethod(o, JavaClassWrapper::singleton->Short_shortValue);
  883. ret.push_back(val);
  884. }
  885. env->DeleteLocalRef(o);
  886. }
  887. var = ret;
  888. return true;
  889. } break;
  890. case ARG_NUMBER_CLASS_BIT | ARG_ARRAY_BIT | ARG_TYPE_INT: {
  891. Array ret;
  892. jobjectArray arr = (jobjectArray)obj;
  893. int count = env->GetArrayLength(arr);
  894. for (int i = 0; i < count; i++) {
  895. jobject o = env->GetObjectArrayElement(arr, i);
  896. if (!o) {
  897. ret.push_back(Variant());
  898. } else {
  899. int val = env->CallIntMethod(o, JavaClassWrapper::singleton->Integer_integerValue);
  900. ret.push_back(val);
  901. }
  902. env->DeleteLocalRef(o);
  903. }
  904. var = ret;
  905. return true;
  906. } break;
  907. case ARG_NUMBER_CLASS_BIT | ARG_ARRAY_BIT | ARG_TYPE_LONG: {
  908. Array ret;
  909. jobjectArray arr = (jobjectArray)obj;
  910. int count = env->GetArrayLength(arr);
  911. for (int i = 0; i < count; i++) {
  912. jobject o = env->GetObjectArrayElement(arr, i);
  913. if (!o) {
  914. ret.push_back(Variant());
  915. } else {
  916. int64_t val = env->CallLongMethod(o, JavaClassWrapper::singleton->Long_longValue);
  917. ret.push_back(val);
  918. }
  919. env->DeleteLocalRef(o);
  920. }
  921. var = ret;
  922. return true;
  923. } break;
  924. case ARG_NUMBER_CLASS_BIT | ARG_ARRAY_BIT | ARG_TYPE_FLOAT: {
  925. Array ret;
  926. jobjectArray arr = (jobjectArray)obj;
  927. int count = env->GetArrayLength(arr);
  928. for (int i = 0; i < count; i++) {
  929. jobject o = env->GetObjectArrayElement(arr, i);
  930. if (!o) {
  931. ret.push_back(Variant());
  932. } else {
  933. float val = env->CallFloatMethod(o, JavaClassWrapper::singleton->Float_floatValue);
  934. ret.push_back(val);
  935. }
  936. env->DeleteLocalRef(o);
  937. }
  938. var = ret;
  939. return true;
  940. } break;
  941. case ARG_NUMBER_CLASS_BIT | ARG_ARRAY_BIT | ARG_TYPE_DOUBLE: {
  942. Array ret;
  943. jobjectArray arr = (jobjectArray)obj;
  944. int count = env->GetArrayLength(arr);
  945. for (int i = 0; i < count; i++) {
  946. jobject o = env->GetObjectArrayElement(arr, i);
  947. if (!o) {
  948. ret.push_back(Variant());
  949. } else {
  950. double val = env->CallDoubleMethod(o, JavaClassWrapper::singleton->Double_doubleValue);
  951. ret.push_back(val);
  952. }
  953. env->DeleteLocalRef(o);
  954. }
  955. var = ret;
  956. return true;
  957. } break;
  958. case ARG_ARRAY_BIT | ARG_TYPE_STRING: {
  959. Array ret;
  960. jobjectArray arr = (jobjectArray)obj;
  961. int count = env->GetArrayLength(arr);
  962. for (int i = 0; i < count; i++) {
  963. jobject o = env->GetObjectArrayElement(arr, i);
  964. if (!o) {
  965. ret.push_back(Variant());
  966. } else {
  967. String val = jstring_to_string((jstring)o, env);
  968. ret.push_back(val);
  969. }
  970. env->DeleteLocalRef(o);
  971. }
  972. var = ret;
  973. return true;
  974. } break;
  975. case ARG_ARRAY_BIT | ARG_TYPE_CLASS: {
  976. } break;
  977. }
  978. return false;
  979. }
  980. Ref<JavaClass> JavaClassWrapper::_wrap(const String &p_class, bool p_allow_private_methods_access) {
  981. String class_name_dots = p_class.replace("/", ".");
  982. if (class_cache.has(class_name_dots)) {
  983. return class_cache[class_name_dots];
  984. }
  985. JNIEnv *env = get_jni_env();
  986. ERR_FAIL_NULL_V(env, Ref<JavaClass>());
  987. jclass bclass = env->FindClass(class_name_dots.replace(".", "/").utf8().get_data());
  988. ERR_FAIL_NULL_V(bclass, Ref<JavaClass>());
  989. jobjectArray constructors = (jobjectArray)env->CallObjectMethod(bclass, Class_getDeclaredConstructors);
  990. ERR_FAIL_NULL_V(constructors, Ref<JavaClass>());
  991. jobjectArray methods = (jobjectArray)env->CallObjectMethod(bclass, Class_getDeclaredMethods);
  992. ERR_FAIL_NULL_V(methods, Ref<JavaClass>());
  993. Ref<JavaClass> java_class = memnew(JavaClass);
  994. java_class->java_class_name = class_name_dots;
  995. Vector<String> class_name_parts = class_name_dots.split(".");
  996. java_class->java_constructor_name = class_name_parts[class_name_parts.size() - 1];
  997. java_class->_class = (jclass)env->NewGlobalRef(bclass);
  998. class_cache[class_name_dots] = java_class;
  999. LocalVector<jobject> methods_and_constructors;
  1000. int constructor_count = env->GetArrayLength(constructors);
  1001. int method_count = env->GetArrayLength(methods);
  1002. methods_and_constructors.resize(method_count + constructor_count);
  1003. for (int i = 0; i < constructor_count; i++) {
  1004. methods_and_constructors[i] = env->GetObjectArrayElement(constructors, i);
  1005. }
  1006. for (int i = 0; i < method_count; i++) {
  1007. methods_and_constructors[constructor_count + i] = env->GetObjectArrayElement(methods, i);
  1008. }
  1009. for (int i = 0; i < (int)methods_and_constructors.size(); i++) {
  1010. jobject obj = methods_and_constructors[i];
  1011. ERR_CONTINUE(!obj);
  1012. bool is_constructor = i < constructor_count;
  1013. String str_method;
  1014. if (is_constructor) {
  1015. str_method = "<init>";
  1016. } else {
  1017. jstring name = (jstring)env->CallObjectMethod(obj, Method_getName);
  1018. str_method = jstring_to_string(name, env);
  1019. env->DeleteLocalRef(name);
  1020. }
  1021. Vector<String> params;
  1022. jint mods = env->CallIntMethod(obj, is_constructor ? Constructor_getModifiers : Method_getModifiers);
  1023. if (!(mods & 0x0001) && (is_constructor || !p_allow_private_methods_access)) {
  1024. env->DeleteLocalRef(obj);
  1025. continue; //not public bye
  1026. }
  1027. jobjectArray param_types = (jobjectArray)env->CallObjectMethod(obj, is_constructor ? Constructor_getParameterTypes : Method_getParameterTypes);
  1028. int count = env->GetArrayLength(param_types);
  1029. if (!java_class->methods.has(str_method)) {
  1030. java_class->methods[str_method] = List<JavaClass::MethodInfo>();
  1031. }
  1032. JavaClass::MethodInfo mi;
  1033. mi._static = (mods & 0x8) != 0;
  1034. mi._constructor = is_constructor;
  1035. bool valid = true;
  1036. String signature = "(";
  1037. for (int j = 0; j < count; j++) {
  1038. jobject obj2 = env->GetObjectArrayElement(param_types, j);
  1039. String strsig;
  1040. uint32_t sig = 0;
  1041. if (!_get_type_sig(env, obj2, sig, strsig)) {
  1042. valid = false;
  1043. env->DeleteLocalRef(obj2);
  1044. break;
  1045. }
  1046. signature += strsig;
  1047. mi.param_types.push_back(sig);
  1048. mi.param_sigs.push_back(strsig);
  1049. env->DeleteLocalRef(obj2);
  1050. }
  1051. if (!valid) {
  1052. print_line("Method can't be bound (unsupported arguments): " + class_name_dots + "::" + str_method);
  1053. env->DeleteLocalRef(obj);
  1054. env->DeleteLocalRef(param_types);
  1055. continue;
  1056. }
  1057. signature += ")";
  1058. if (is_constructor) {
  1059. signature += "V";
  1060. mi.return_type = JavaClass::ARG_TYPE_CLASS;
  1061. } else {
  1062. jobject return_type = (jobject)env->CallObjectMethod(obj, Method_getReturnType);
  1063. String strsig;
  1064. uint32_t sig = 0;
  1065. if (!_get_type_sig(env, return_type, sig, strsig)) {
  1066. print_line("Method can't be bound (unsupported return type): " + class_name_dots + "::" + str_method);
  1067. env->DeleteLocalRef(obj);
  1068. env->DeleteLocalRef(param_types);
  1069. env->DeleteLocalRef(return_type);
  1070. continue;
  1071. }
  1072. signature += strsig;
  1073. mi.return_type = sig;
  1074. env->DeleteLocalRef(return_type);
  1075. }
  1076. bool discard = false;
  1077. for (List<JavaClass::MethodInfo>::Element *E = java_class->methods[str_method].front(); E; E = E->next()) {
  1078. float new_likeliness = 0;
  1079. float existing_likeliness = 0;
  1080. if (E->get().param_types.size() != mi.param_types.size()) {
  1081. continue;
  1082. }
  1083. bool this_valid = true;
  1084. for (int j = 0; j < E->get().param_types.size(); j++) {
  1085. Variant::Type _new;
  1086. float new_l;
  1087. Variant::Type existing;
  1088. float existing_l;
  1089. JavaClass::_convert_to_variant_type(E->get().param_types[j], existing, existing_l);
  1090. JavaClass::_convert_to_variant_type(mi.param_types[j], _new, new_l);
  1091. if (_new != existing) {
  1092. this_valid = false;
  1093. break;
  1094. }
  1095. new_likeliness += new_l;
  1096. existing_likeliness = existing_l;
  1097. }
  1098. if (!this_valid) {
  1099. continue;
  1100. }
  1101. if (new_likeliness > existing_likeliness) {
  1102. java_class->methods[str_method].erase(E);
  1103. break;
  1104. } else {
  1105. discard = true;
  1106. }
  1107. }
  1108. if (!discard) {
  1109. if (mi._static) {
  1110. mi.method = env->GetStaticMethodID(bclass, str_method.utf8().get_data(), signature.utf8().get_data());
  1111. } else {
  1112. mi.method = env->GetMethodID(bclass, str_method.utf8().get_data(), signature.utf8().get_data());
  1113. }
  1114. ERR_CONTINUE(!mi.method);
  1115. java_class->methods[str_method].push_back(mi);
  1116. }
  1117. env->DeleteLocalRef(obj);
  1118. env->DeleteLocalRef(param_types);
  1119. }
  1120. env->DeleteLocalRef(constructors);
  1121. env->DeleteLocalRef(methods);
  1122. jobjectArray fields = (jobjectArray)env->CallObjectMethod(bclass, Class_getFields);
  1123. int count = env->GetArrayLength(fields);
  1124. for (int i = 0; i < count; i++) {
  1125. jobject obj = env->GetObjectArrayElement(fields, i);
  1126. ERR_CONTINUE(!obj);
  1127. jstring name = (jstring)env->CallObjectMethod(obj, Field_getName);
  1128. String str_field = jstring_to_string(name, env);
  1129. env->DeleteLocalRef(name);
  1130. int mods = env->CallIntMethod(obj, Field_getModifiers);
  1131. if ((mods & 0x8) && (mods & 0x10) && (mods & 0x1)) { //static final public!
  1132. jobject objc = env->CallObjectMethod(obj, Field_get, nullptr);
  1133. if (objc) {
  1134. uint32_t sig;
  1135. String strsig;
  1136. jclass cl = env->GetObjectClass(objc);
  1137. if (JavaClassWrapper::_get_type_sig(env, cl, sig, strsig)) {
  1138. if ((sig & JavaClass::ARG_TYPE_MASK) <= JavaClass::ARG_TYPE_STRING) {
  1139. Variant value;
  1140. if (JavaClass::_convert_object_to_variant(env, objc, value, sig)) {
  1141. java_class->constant_map[str_field] = value;
  1142. }
  1143. }
  1144. }
  1145. env->DeleteLocalRef(cl);
  1146. }
  1147. env->DeleteLocalRef(objc);
  1148. }
  1149. env->DeleteLocalRef(obj);
  1150. }
  1151. env->DeleteLocalRef(fields);
  1152. return java_class;
  1153. }
  1154. Ref<JavaClass> JavaClassWrapper::wrap_jclass(jclass p_class, bool p_allow_private_methods_access) {
  1155. JNIEnv *env = get_jni_env();
  1156. ERR_FAIL_NULL_V(env, Ref<JavaClass>());
  1157. jstring class_name = (jstring)env->CallObjectMethod(p_class, Class_getName);
  1158. String class_name_string = jstring_to_string(class_name, env);
  1159. env->DeleteLocalRef(class_name);
  1160. return _wrap(class_name_string, p_allow_private_methods_access);
  1161. }
  1162. JavaClassWrapper *JavaClassWrapper::singleton = nullptr;
  1163. JavaClassWrapper::JavaClassWrapper() {
  1164. singleton = this;
  1165. JNIEnv *env = get_jni_env();
  1166. ERR_FAIL_NULL(env);
  1167. jclass bclass = env->FindClass("java/lang/Class");
  1168. Class_getDeclaredConstructors = env->GetMethodID(bclass, "getDeclaredConstructors", "()[Ljava/lang/reflect/Constructor;");
  1169. Class_getDeclaredMethods = env->GetMethodID(bclass, "getDeclaredMethods", "()[Ljava/lang/reflect/Method;");
  1170. Class_getFields = env->GetMethodID(bclass, "getFields", "()[Ljava/lang/reflect/Field;");
  1171. Class_getName = env->GetMethodID(bclass, "getName", "()Ljava/lang/String;");
  1172. Class_getSuperclass = env->GetMethodID(bclass, "getSuperclass", "()Ljava/lang/Class;");
  1173. env->DeleteLocalRef(bclass);
  1174. bclass = env->FindClass("java/lang/reflect/Constructor");
  1175. Constructor_getParameterTypes = env->GetMethodID(bclass, "getParameterTypes", "()[Ljava/lang/Class;");
  1176. Constructor_getModifiers = env->GetMethodID(bclass, "getModifiers", "()I");
  1177. env->DeleteLocalRef(bclass);
  1178. bclass = env->FindClass("java/lang/reflect/Method");
  1179. Method_getParameterTypes = env->GetMethodID(bclass, "getParameterTypes", "()[Ljava/lang/Class;");
  1180. Method_getReturnType = env->GetMethodID(bclass, "getReturnType", "()Ljava/lang/Class;");
  1181. Method_getName = env->GetMethodID(bclass, "getName", "()Ljava/lang/String;");
  1182. Method_getModifiers = env->GetMethodID(bclass, "getModifiers", "()I");
  1183. env->DeleteLocalRef(bclass);
  1184. bclass = env->FindClass("java/lang/reflect/Field");
  1185. Field_getName = env->GetMethodID(bclass, "getName", "()Ljava/lang/String;");
  1186. Field_getModifiers = env->GetMethodID(bclass, "getModifiers", "()I");
  1187. Field_get = env->GetMethodID(bclass, "get", "(Ljava/lang/Object;)Ljava/lang/Object;");
  1188. env->DeleteLocalRef(bclass);
  1189. bclass = env->FindClass("java/lang/Boolean");
  1190. Boolean_booleanValue = env->GetMethodID(bclass, "booleanValue", "()Z");
  1191. env->DeleteLocalRef(bclass);
  1192. bclass = env->FindClass("java/lang/Byte");
  1193. Byte_byteValue = env->GetMethodID(bclass, "byteValue", "()B");
  1194. env->DeleteLocalRef(bclass);
  1195. bclass = env->FindClass("java/lang/Character");
  1196. Character_characterValue = env->GetMethodID(bclass, "charValue", "()C");
  1197. env->DeleteLocalRef(bclass);
  1198. bclass = env->FindClass("java/lang/Short");
  1199. Short_shortValue = env->GetMethodID(bclass, "shortValue", "()S");
  1200. env->DeleteLocalRef(bclass);
  1201. bclass = env->FindClass("java/lang/Integer");
  1202. Integer_integerValue = env->GetMethodID(bclass, "intValue", "()I");
  1203. env->DeleteLocalRef(bclass);
  1204. bclass = env->FindClass("java/lang/Long");
  1205. Long_longValue = env->GetMethodID(bclass, "longValue", "()J");
  1206. env->DeleteLocalRef(bclass);
  1207. bclass = env->FindClass("java/lang/Float");
  1208. Float_floatValue = env->GetMethodID(bclass, "floatValue", "()F");
  1209. env->DeleteLocalRef(bclass);
  1210. bclass = env->FindClass("java/lang/Double");
  1211. Double_doubleValue = env->GetMethodID(bclass, "doubleValue", "()D");
  1212. env->DeleteLocalRef(bclass);
  1213. }