java_class_wrapper.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /**************************************************************************/
  2. /* java_class_wrapper.h */
  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. #pragma once
  31. #include "core/object/ref_counted.h"
  32. #include "core/variant/typed_array.h"
  33. #ifdef ANDROID_ENABLED
  34. #include "core/templates/rb_map.h"
  35. #include <android/log.h>
  36. #include <jni.h>
  37. #endif
  38. #ifdef ANDROID_ENABLED
  39. class JavaObject;
  40. #endif
  41. class JavaClass : public RefCounted {
  42. GDCLASS(JavaClass, RefCounted);
  43. #ifdef ANDROID_ENABLED
  44. enum ArgumentType {
  45. ARG_TYPE_VOID,
  46. ARG_TYPE_BOOLEAN,
  47. ARG_TYPE_BYTE,
  48. ARG_TYPE_CHAR,
  49. ARG_TYPE_SHORT,
  50. ARG_TYPE_INT,
  51. ARG_TYPE_LONG,
  52. ARG_TYPE_FLOAT,
  53. ARG_TYPE_DOUBLE,
  54. ARG_TYPE_STRING, //special case
  55. ARG_TYPE_CHARSEQUENCE,
  56. ARG_TYPE_CALLABLE,
  57. ARG_TYPE_CLASS,
  58. ARG_ARRAY_BIT = 1 << 16,
  59. ARG_NUMBER_CLASS_BIT = 1 << 17,
  60. ARG_TYPE_MASK = (1 << 16) - 1
  61. };
  62. RBMap<StringName, Variant> constant_map;
  63. struct MethodInfo {
  64. bool _public = false;
  65. bool _static = false;
  66. bool _constructor = false;
  67. Vector<uint32_t> param_types;
  68. Vector<StringName> param_sigs;
  69. uint32_t return_type = 0;
  70. jmethodID method;
  71. };
  72. _FORCE_INLINE_ static void _convert_to_variant_type(int p_sig, Variant::Type &r_type, float &likelihood) {
  73. likelihood = 1.0;
  74. r_type = Variant::NIL;
  75. switch (p_sig) {
  76. case ARG_TYPE_VOID:
  77. r_type = Variant::NIL;
  78. break;
  79. case ARG_TYPE_BOOLEAN | ARG_NUMBER_CLASS_BIT:
  80. case ARG_TYPE_BOOLEAN:
  81. r_type = Variant::BOOL;
  82. break;
  83. case ARG_TYPE_BYTE | ARG_NUMBER_CLASS_BIT:
  84. case ARG_TYPE_BYTE:
  85. r_type = Variant::INT;
  86. likelihood = 0.1;
  87. break;
  88. case ARG_TYPE_CHAR | ARG_NUMBER_CLASS_BIT:
  89. case ARG_TYPE_CHAR:
  90. r_type = Variant::INT;
  91. likelihood = 0.2;
  92. break;
  93. case ARG_TYPE_SHORT | ARG_NUMBER_CLASS_BIT:
  94. case ARG_TYPE_SHORT:
  95. r_type = Variant::INT;
  96. likelihood = 0.3;
  97. break;
  98. case ARG_TYPE_INT | ARG_NUMBER_CLASS_BIT:
  99. case ARG_TYPE_INT:
  100. r_type = Variant::INT;
  101. likelihood = 1.0;
  102. break;
  103. case ARG_TYPE_LONG | ARG_NUMBER_CLASS_BIT:
  104. case ARG_TYPE_LONG:
  105. r_type = Variant::INT;
  106. likelihood = 0.5;
  107. break;
  108. case ARG_TYPE_FLOAT | ARG_NUMBER_CLASS_BIT:
  109. case ARG_TYPE_FLOAT:
  110. r_type = Variant::FLOAT;
  111. likelihood = 1.0;
  112. break;
  113. case ARG_TYPE_DOUBLE | ARG_NUMBER_CLASS_BIT:
  114. case ARG_TYPE_DOUBLE:
  115. r_type = Variant::FLOAT;
  116. likelihood = 0.5;
  117. break;
  118. case ARG_TYPE_STRING:
  119. case ARG_TYPE_CHARSEQUENCE:
  120. r_type = Variant::STRING;
  121. break;
  122. case ARG_TYPE_CALLABLE:
  123. r_type = Variant::CALLABLE;
  124. break;
  125. case ARG_TYPE_CLASS:
  126. r_type = Variant::OBJECT;
  127. break;
  128. case ARG_ARRAY_BIT | ARG_TYPE_VOID:
  129. r_type = Variant::NIL;
  130. break;
  131. case ARG_ARRAY_BIT | ARG_TYPE_BOOLEAN:
  132. r_type = Variant::ARRAY;
  133. break;
  134. case ARG_ARRAY_BIT | ARG_TYPE_BYTE:
  135. r_type = Variant::PACKED_BYTE_ARRAY;
  136. likelihood = 1.0;
  137. break;
  138. case ARG_ARRAY_BIT | ARG_TYPE_CHAR:
  139. r_type = Variant::PACKED_BYTE_ARRAY;
  140. likelihood = 0.5;
  141. break;
  142. case ARG_ARRAY_BIT | ARG_TYPE_SHORT:
  143. r_type = Variant::PACKED_INT32_ARRAY;
  144. likelihood = 0.3;
  145. break;
  146. case ARG_ARRAY_BIT | ARG_TYPE_INT:
  147. r_type = Variant::PACKED_INT32_ARRAY;
  148. likelihood = 1.0;
  149. break;
  150. case ARG_ARRAY_BIT | ARG_TYPE_LONG:
  151. r_type = Variant::PACKED_INT32_ARRAY;
  152. likelihood = 0.5;
  153. break;
  154. case ARG_ARRAY_BIT | ARG_TYPE_FLOAT:
  155. r_type = Variant::PACKED_FLOAT32_ARRAY;
  156. likelihood = 1.0;
  157. break;
  158. case ARG_ARRAY_BIT | ARG_TYPE_DOUBLE:
  159. r_type = Variant::PACKED_FLOAT32_ARRAY;
  160. likelihood = 0.5;
  161. break;
  162. case ARG_ARRAY_BIT | ARG_TYPE_STRING:
  163. case ARG_ARRAY_BIT | ARG_TYPE_CHARSEQUENCE:
  164. r_type = Variant::PACKED_STRING_ARRAY;
  165. break;
  166. case ARG_ARRAY_BIT | ARG_TYPE_CLASS:
  167. case ARG_ARRAY_BIT | ARG_TYPE_CALLABLE:
  168. r_type = Variant::ARRAY;
  169. break;
  170. }
  171. }
  172. _FORCE_INLINE_ static bool _convert_object_to_variant(JNIEnv *env, jobject obj, Variant &var, uint32_t p_sig);
  173. bool _call_method(JavaObject *p_instance, const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error, Variant &ret);
  174. friend class JavaClassWrapper;
  175. friend class JavaObject;
  176. String java_class_name;
  177. String java_constructor_name;
  178. HashMap<StringName, List<MethodInfo>> methods;
  179. jclass _class;
  180. #endif
  181. protected:
  182. static void _bind_methods();
  183. bool _get(const StringName &p_name, Variant &r_ret) const;
  184. public:
  185. virtual Variant callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) override;
  186. String get_java_class_name() const;
  187. TypedArray<Dictionary> get_java_method_list() const;
  188. Ref<JavaClass> get_java_parent_class() const;
  189. bool has_java_method(const StringName &p_method) const;
  190. #ifdef ANDROID_ENABLED
  191. virtual String _to_string() override;
  192. #endif
  193. JavaClass();
  194. ~JavaClass();
  195. };
  196. class JavaObject : public RefCounted {
  197. GDCLASS(JavaObject, RefCounted);
  198. #ifdef ANDROID_ENABLED
  199. Ref<JavaClass> base_class;
  200. friend class JavaClass;
  201. jobject instance = nullptr;
  202. #endif
  203. protected:
  204. static void _bind_methods();
  205. public:
  206. virtual Variant callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) override;
  207. Ref<JavaClass> get_java_class() const;
  208. bool has_java_method(const StringName &p_method) const;
  209. #ifdef ANDROID_ENABLED
  210. virtual String _to_string() override;
  211. jobject get_instance() { return instance; }
  212. JavaObject();
  213. JavaObject(const Ref<JavaClass> &p_base, jobject p_instance);
  214. ~JavaObject();
  215. #endif
  216. };
  217. class JavaClassWrapper : public Object {
  218. GDCLASS(JavaClassWrapper, Object);
  219. #ifdef ANDROID_ENABLED
  220. RBMap<String, Ref<JavaClass>> class_cache;
  221. friend class JavaClass;
  222. jmethodID Class_getConstructors;
  223. jmethodID Class_getDeclaredMethods;
  224. jmethodID Class_getFields;
  225. jmethodID Class_getName;
  226. jmethodID Class_getSuperclass;
  227. jmethodID Constructor_getParameterTypes;
  228. jmethodID Constructor_getModifiers;
  229. jmethodID Method_getParameterTypes;
  230. jmethodID Method_getReturnType;
  231. jmethodID Method_getModifiers;
  232. jmethodID Method_getName;
  233. jmethodID Field_getName;
  234. jmethodID Field_getModifiers;
  235. jmethodID Field_get;
  236. jmethodID Boolean_booleanValue;
  237. jmethodID Byte_byteValue;
  238. jmethodID Character_characterValue;
  239. jmethodID Short_shortValue;
  240. jmethodID Integer_integerValue;
  241. jmethodID Long_longValue;
  242. jmethodID Float_floatValue;
  243. jmethodID Double_doubleValue;
  244. bool _get_type_sig(JNIEnv *env, jobject obj, uint32_t &sig, String &strsig);
  245. #endif
  246. Ref<JavaObject> exception;
  247. Ref<JavaClass> _wrap(const String &p_class, bool p_allow_non_public_methods_access = false);
  248. static JavaClassWrapper *singleton;
  249. protected:
  250. static void _bind_methods();
  251. public:
  252. static JavaClassWrapper *get_singleton() { return singleton; }
  253. Ref<JavaClass> wrap(const String &p_class) {
  254. return _wrap(p_class, false);
  255. }
  256. Ref<JavaObject> get_exception() {
  257. return exception;
  258. }
  259. #ifdef ANDROID_ENABLED
  260. Ref<JavaClass> wrap_jclass(jclass p_class, bool p_allow_non_public_methods_access = false);
  261. #endif
  262. JavaClassWrapper();
  263. };