Debian Codegraph @

platform/android/api/java_class_wrapper.h

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