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