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