java_class_wrapper.h 8.8 KB

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